-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to report errors in js files #14496
Conversation
This means that it'll be possible to opt-in to have types that can be inferred from the environment ( Sounds pretty great for progressively migrating a codebase to typescript 🎉 I wonder how this interacts with |
correct. this is the intention.
I do not know what is the best solution here. We were discussing this topic today, and we are doing some additional inferences/modifications to types in a .js file that we do not do in a .ts file. some of these involve |
tests/cases/compiler/checkJsFiles.ts
Outdated
@@ -0,0 +1,6 @@ | |||
// @allowJs: true | |||
// @checkJsFiles: true |
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.
You might want to add @noEmit: true
or @outFile
to suppress all the Cannot write file... because it would overwrite
errors in the baselines.
src/compiler/commandLineParser.ts
Outdated
@@ -481,6 +481,12 @@ namespace ts { | |||
name: "plugin", | |||
type: "object" | |||
} | |||
}, | |||
{ | |||
name: "checkJsFiles", |
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.
Since we have allowJs
, shouldn't this be checkJs
? Having 'Files' on just one of them seems inconsistent.
src/compiler/program.ts
Outdated
// Instead, we'll report errors for using TypeScript-only constructs from within a | ||
// JavaScript file when we get syntactic diagnostics for the file. | ||
const checkDiagnostics = isSourceFileJavaScript(sourceFile) ? [] : typeChecker.getDiagnostics(sourceFile, cancellationToken); | ||
// For JavaScript files, we don't want to report semantic errors unless ecplicitlly requested. |
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.
Spelling
src/compiler/parser.ts
Outdated
@@ -5878,13 +5879,24 @@ namespace ts { | |||
amdDependencies.push(amdDependency); | |||
} | |||
} | |||
|
|||
const checkJsDirectiveRegEx = /^\/\/\/?\s*@check(\s+(true|false))?/gim; |
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.
Or maybe an @nocheck directive?
…' into checkJSFiles_QuickFixes
Allow skipping diagnostics in .js file using comments and quick fixes to add them
function shouldReportDiagnostic(diagnostic: Diagnostic) { | ||
const { file, start } = diagnostic; | ||
const lineStarts = getLineStarts(file); | ||
let { line } = computeLineAndCharacterOfPosition(lineStarts, start); |
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 line have more than one error where only one should be suppresed
- seems that
ts-suppress
should be located immediately before the line to be suppressed so if user will put a comment betweents-suppress
and target line it will stop working. Is it intended?
.map(d => allDiagnostcs[d].code); | ||
} | ||
|
||
function shouldCheckJsFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) { |
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.
this is the same snipped with this one. Can we share this?
@@ -5883,13 +5884,24 @@ namespace ts { | |||
amdDependencies.push(amdDependency); | |||
} | |||
} | |||
|
|||
const checkJsDirectiveRegEx = /^\/\/\/?\s*(@ts-check|@ts-nocheck)\s*$/gim; |
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 would recommend you pull this out of the function and remove the g
option. g
is only really useful if you want to preserve lastIndex
when using a regular expression in a loop. Also I would remove the m
option as it should be unnecessary.
Consider this instead:
const checkJsDirectiveRegEx = /^\/\/\/?\s*@ts-(no)?check\s*$/i;
...
const checkJsDirectiveMatchResult = checkJsDirectiveRegEx.exec(comment);
if (checkJsDirectiveMatchResult) {
checkJsDirective = {
enabled: !!checkJsDirectiveMatchResult[1],
end: range.end,
pos: range.pos
};
}
Capturing only the no
allows you to avoid the additional case-insensitive string comparison.
Does this close #7661 ? |
Nope. |
Very nice option. But it will be more powerful if allows to suppression several errors |
--checkJsFiles
--checkJs
flag to enable error reporting in all .js files in a project (used in conjunction with--allowJs
)./// @check
to enable the error reporting per file.