Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Improve an error message on invalid source file #1480

Merged
merged 1 commit into from
Aug 15, 2016

Conversation

rhysd
Copy link
Contributor

@rhysd rhysd commented Aug 13, 2016

When input file is not a TypeScript code, getSourceFile() returnsundefined but tslint doesn't handle it properly. As the result, tslint raises an error which is not easy to understand when invalid soruce file is passed.

$ tslint mocha.opts

It raised below error:

/path/to/node_modules/tslint/lib/language/walker/ruleWalker.js:18
        this.limit = this.sourceFile.getFullWidth();

I improved this error message by checking returned value from getSourceFile is valid or not. Improved error message is below:

Error: Invalid source code: test/mocha.opts

I didn't write a test for this yet because I couldn't find where I should add tests for src/tslint.ts.

@adidahiya
Copy link
Contributor

nice, this partially addresses #516

@@ -91,6 +91,10 @@ class Linter {
sourceFile = getSourceFile(this.fileName, this.source);
}

if (sourceFile === undefined) {
throw new Error("Invalid source code: " + this.fileName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we be more descriptive here and suggest why the input is considered invalid? how about:

throw new Error(`Invalid source file "${this.fileName}". Ensure that the files supplied to lint have a .ts or .tsx extension.`);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that tsc only checks file extension. sourceFile may be undefined because of other than file extension.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last time I touched this code IIRC tsc does care about the file extension. having the wrong extension is a common error I see in issue reports so that's why I suggested it for a small improvement to the error message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood. I'll update the commit soon. Thanks 😄

@rhysd
Copy link
Contributor Author

rhysd commented Aug 15, 2016

@adidahiya

I fixed the point you commented 😄

When input file is not a TypeScript code, `getSourceFile()` returns
`undefined` but tslint doesn't handle it properly.  As the result,
tslint raises an error which is not easy to understand when invalid
soruce file is passed.

```
$ tslint mocha.opts
```

It raised below error:

```
/path/to/node_modules/tslint/lib/language/walker/ruleWalker.js:18
        this.limit = this.sourceFile.getFullWidth();
```

I improved this error message by checking returned value from
`getSourceFile` is valid or not.
Improved error message is below:

```
Error: Invalid source code: test/mocha.opts
```

I didn't write a test for this yet because I couldn't find where I
should add tests for `src/tslint.ts`.
@adidahiya adidahiya merged commit 81890b6 into palantir:master Aug 15, 2016
@adidahiya
Copy link
Contributor

thanks @rhysd!

soniro pushed a commit to soniro/tslint that referenced this pull request Aug 31, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants