10000 Add support to report errors in js files by mhegazy · Pull Request #14496 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

Add support to report errors in js files #14496

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

Merged
merged 29 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
91571f0
Add support for handeling .js file correctelly in fixAddMissingMember…
mhegazy Feb 23, 2017
0b1fff7
Add `--checkJsFiles`
mhegazy Jan 7, 2017
9f0c5ce
Add support for `//@check` directives
mhegazy Jan 7, 2017
0b247b1
Add tests
mhegazy Mar 6, 2017
1f9bb69
Add --noEmit to tests
mhegazy Mar 7, 2017
b015c1d
Allow @check directives to switch on/off checking in a file
mhegazy Mar 7, 2017
9305d4d
Change flag name to `checkJs`
mhegazy Mar 7, 2017
fb218b7
Error if `--checkJs` is used without `--allowJs`
mhegazy Mar 7, 2017
e9f8214
Code review comments
mhegazy Mar 8, 2017
a202fa4
add es6 to buildProtocol
mhegazy Mar 9, 2017
3d03f8d
Merge branch 'fixBuildBreak' into checkJSFiles
mhegazy Mar 9, 2017
fe7719f
Disable check diagnostics per line
mhegazy Mar 8, 2017
706acdf
Add quick fix to disable error checking in a .js file
mhegazy Mar 7, 2017
13e80b9
Fix building webTestServer
mhegazy Mar 7, 2017
936a91d
Add comment
mhegazy Mar 10, 2017
cc6affa
Merge remote-tracking branch 'origin/updateCodeFixForAddMissingMember…
mhegazy Mar 14, 2017
6e86596
Add debugging utilities
mhegazy Mar 14, 2017
fd9fb8f
Support static properties
mhegazy Mar 14, 2017
509b2dc
Add disableJsDiagnostics codefixes to harnes
mhegazy Mar 14, 2017
1fbbead
Merge pull request #14568 from Microsoft/checkJSFiles_QuickFixes
mhegazy Mar 14, 2017
7980629
Code review comments
mhegazy Mar 15, 2017
0dac29f
Merge branch 'master' into checkJSFiles
mhegazy Mar 15, 2017
3b57b5d
Refactor checking for checkJs value in a common helper
mhegazy Mar 15, 2017
e408cad
Merge branch 'master' into checkJSFiles
mhegazy Mar 22, 2017
db6c969
Change ingore diagonstic comment to `// @ts-ignore`
mhegazy Mar 22, 2017
3378f5c
Merge branch 'master' into checkJSFiles
mhegazy Mar 27, 2017
e630ab1
Report semantic errors for JS files if checkJs is enabled
mhegazy Mar 27, 2017
0637f24
Merge remote-tracking branch 'origin/master' into checkJSFiles
mhegazy Mar 28, 2017
8ea9617
Merge remote-tracking branch 'origin/master' into checkJSFiles
mhegazy Mar 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code review comments
  • Loading branch information
mhegazy committed Mar 9, 2017
commit e9f82145b781f68aa243fb078b8c898552759798
4 changes: 2 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5880,11 +5880,11 @@ namespace ts {
}
}

const checkJsDirectiveRegEx = /^\/\/\/?\s*@check(\s+(true|false))?/gim;
const checkJsDirectiveRegEx = /^\/\/\/?\s*(@ts-check|@ts-nocheck)\s*$/gim;
Copy link
Contributor

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.

const checkJsDirectiveMatchResult = checkJsDirectiveRegEx.exec(comment);
if (checkJsDirectiveMatchResult) {
checkJsDirective = {
enabled: compareStrings(checkJsDirectiveMatchResult[2], "false", /*ignoreCase*/ true) !== Comparison.EqualTo,
enabled: compareStrings(checkJsDirectiveMatchResult[1], "@ts-check", /*ignoreCase*/ true) === Comparison.EqualTo,
end: range.end,
pos: range.pos
};
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ namespace ts {

Debug.assert(!!sourceFile.bindDiagnostics);
const bindDiagnostics = sourceFile.bindDiagnostics;
// For JavaScript files, we don't want to report semantic errors unless ecplicitlly requested.
// For JavaScript files, we don't want to report semantic errors unless explicitly requested.
const includeCheckDiagnostics = !isSourceFileJavaScript(sourceFile) ||
(sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : options.checkJs);
const checkDiagnostics = includeCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : [];
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/checkJsFiles2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type

==== tests/cases/compiler/a.js (1 errors) ====

// @check
// @ts-check
var x = "string";
x = 0;
~
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/checkJsFiles3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type

==== tests/cases/compiler/a.js (1 errors) ====

// @check
// @ts-check
var x = "string";
x = 0;
~
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/checkJsFiles4.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type

==== tests/cases/compiler/a.js (1 errors) ====

// @check true
// @ts-check
var x = "string";
x = 0;
~
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/checkJsFiles5.symbols
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/a.js ===

// @check false
// @ts-nocheck
var x = "string";
>x : Symbol(x, Decl(a.js, 2, 3))

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/checkJsFiles5.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/a.js ===

// @check false
// @ts-nocheck
var x = "string";
>x : string
>"string" : "string"
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/compiler/checkJsFiles2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// @noEmit: true

// @fileName: a.js
// @check
// @ts-check
var x = "string";
x = 0;
2 changes: 1 addition & 1 deletion tests/cases/compiler/checkJsFiles3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// @noEmit: true

// @fileName: a.js
// @check
// @ts-check
var x = "string";
x = 0;
2 changes: 1 addition & 1 deletion tests/cases/compiler/checkJsFiles4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// @noEmit: true

// @fileName: a.js
// @check true
// @ts-check
var x = "string";
x = 0;
2 changes: 1 addition & 1 deletion tests/cases/compiler/checkJsFiles5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// @noEmit: true

// @fileName: a.js
// @check false
// @ts-nocheck
var x = "string";
x = 0;
0