-
Notifications
You must be signed in to change notification settings - Fork 889
Improve an error message on invalid source file #1480
Conversation
0719a43
to
776c49d
Compare
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); |
There was a problem hiding this comment.
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.`);
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😄
776c49d
to
a6fa4fc
Compare
I fixed the point you commented 😄 |
a6fa4fc
to
78fef53
Compare
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`.
thanks @rhysd! |
Partially addresses palantir#516.
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.It raised below error:
I improved this error message by checking returned value from
getSourceFile
is valid or not. Improved error message is below:I didn't write a test for this yet because I couldn't find where I should add tests for
src/tslint.ts
.