Skip to content
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

Merged
merged 29 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
6 changes: 6 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ namespace ts {
name: "plugin",
type: "object"
}
},
{
name: "checkJs",
type: "boolean",
experimental: true,
description: Diagnostics.Report_errors_in_js_files
}
];

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3362,5 +3362,9 @@
"Octal literals are not allowed in enums members initializer. Use the syntax '{0}'.": {
"category": "Error",
"code": 8018
},
"Report errors in .js files.": {
"category": "Message",
"code": 8019
}
}
12 changes: 12 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5817,6 +5817,7 @@ namespace ts {
const typeReferenceDirectives: FileReference[] = [];
const amdDependencies: { path: string; name: string }[] = [];
let amdModuleName: string;
let checkJsDirective: CheckJsDirective = undefined;

// Keep scanning all the leading trivia in the file until we get to something that
// isn't trivia. Any single line comment will be analyzed to see if it is a
Expand Down Expand Up @@ -5878,13 +5879,24 @@ namespace ts {
amdDependencies.push(amdDependency);
}
}

const checkJsDirectiveRegEx = /^\/\/\/?\s*@check(\s+(true|false))?/gim;
Copy link
Member

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?

const checkJsDirectiveMatchResult = checkJsDirectiveRegEx.exec(comment);
if (checkJsDirectiveMatchResult) {
checkJsDirective = {
enabled: compareStrings(checkJsDirectiveMatchResult[2], "false", /*ignoreCase*/ true) !== Comparison.EqualTo,
end: range.end,
pos: range.pos
};
}
}
}

sourceFile.referencedFiles = referencedFiles;
sourceFile.typeReferenceDirectives = typeReferenceDirectives;
sourceFile.amdDependencies = amdDependencies;
sourceFile.moduleName = amdModuleName;
sourceFile.checkJsDirective = checkJsDirective;
}

function setExternalModuleIndicator(sourceFile: SourceFile) {
Expand Down
12 changes: 8 additions & 4 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,10 @@ namespace ts {

Debug.assert(!!sourceFile.bindDiagnostics);
const bindDiagnostics = sourceFile.bindDiagnostics;
// For JavaScript files, we don't want to report semantic errors.
// 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.
Copy link
Member

Choose a reason for hiding this comment

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

Spelling

const includeCheckDiagnostics = !isSourceFileJavaScript(sourceFile) ||
(sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : options.checkJs);
const checkDiagnostics = includeCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : [];
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);

Expand Down Expand Up @@ -1685,6 +1685,10 @@ namespace ts {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "allowJs", "declaration"));
}

if (options.checkJs && !options.allowJs) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "checkJs", "allowJs"));
}

if (options.emitDecoratorMetadata &&
!options.experimentalDecorators) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "emitDecoratorMetadata", "experimentalDecorators"));
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,10 @@
fileName: string;
}

export interface CheckJsDirective extends TextRange {
enabled: boolean;
}

export type CommentKind = SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia;

export interface CommentRange extends TextRange {
Expand Down Expand Up @@ -2272,6 +2276,7 @@
/* @internal */ moduleAugmentations: LiteralExpression[];
/* @internal */ patternAmbientModules?: PatternAmbientModule[];
/* @internal */ ambientModuleNames: string[];
/* @internal */ checkJsDirective: CheckJsDirective | undefined;
}

export interface Bundle extends Node {
Expand Down Expand Up @@ -3312,6 +3317,7 @@
alwaysStrict?: boolean; // Always combine with strict property
baseUrl?: string;
charset?: string;
checkJs?: boolean;
/* @internal */ configFilePath?: string;
declaration?: boolean;
declarationDir?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ namespace Utils {
for (const childName in node) {
if (childName === "parent" || childName === "nextContainer" || childName === "modifiers" || childName === "externalModuleIndicator" ||
// for now ignore jsdoc comments
childName === "jsDocComment") {
childName === "jsDocComment" || childName === "checkJsDirective") {
continue;
}
const child = (<any>node)[childName];
Expand Down
1 change: 1 addition & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ namespace ts {
public moduleAugmentations: LiteralExpression[];
private namedDeclarations: Map<Declaration[]>;
public ambientModuleNames: string[];
public checkJsDirective: CheckJsDirective | undefined;

constructor(kind: SyntaxKind, pos: number, end: number) {
super(kind, pos, end);
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/checkJsFiles.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/cases/compiler/a.js(3,1): error TS2322: Type '0' is not assignable to type 'string'.


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

var x = "string";
x = 0;
~
!!! error TS2322: Type '0' is not assignable to type 'string'.
10 changes: 10 additions & 0 deletions tests/baselines/reference/checkJsFiles2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type 'string'.


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

// @check
var x = "string";
x = 0;
~
!!! error TS2322: Type '0' is not assignable to type 'string'.
10 changes: 10 additions & 0 deletions tests/baselines/reference/checkJsFiles3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type 'string'.


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

// @check
var x = "string";
x = 0;
~
!!! error TS2322: Type '0' is not assignable to type 'string'.
10 changes: 10 additions & 0 deletions tests/baselines/reference/checkJsFiles4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type 'string'.


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

// @check true
var x = "string";
x = 0;
~
!!! error TS2322: Type '0' is not assignable to type 'string'.
9 changes: 9 additions & 0 deletions tests/baselines/reference/checkJsFiles5.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/a.js ===

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

x = 0;
>x : Symbol(x, Decl(a.js, 2, 3))

12 changes: 12 additions & 0 deletions tests/baselines/reference/checkJsFiles5.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/a.js ===

// @check false
var x = "string";
>x : string
>"string" : "string"

x = 0;
>x = 0 : 0
>x : string
>0 : 0

9 changes: 9 additions & 0 deletions tests/baselines/reference/checkJsFiles6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error TS5052: Option 'checkJs' cannot be specified without specifying option 'allowJs'.
error TS6054: File 'tests/cases/compiler/a.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.


!!! error TS5052: Option 'checkJs' cannot be specified without specifying option 'allowJs'.
!!! error TS6054: File 'tests/cases/compiler/a.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
==== tests/cases/compiler/a.js (0 errors) ====

var x;
7 changes: 7 additions & 0 deletions tests/cases/compiler/checkJsFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true

// @fileName: a.js
var x = "string";
x = 0;
8 changes: 8 additions & 0 deletions tests/cases/compiler/checkJsFiles2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @allowJs: true
// @checkJs: false
// @noEmit: true

// @fileName: a.js
// @check
var x = "string";
x = 0;
7 changes: 7 additions & 0 deletions tests/cases/compiler/checkJsFiles3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @allowJs: true
// @noEmit: true

// @fileName: a.js
// @check
var x = "string";
x = 0;
8 changes: 8 additions & 0 deletions tests/cases/compiler/checkJsFiles4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @allowJs: true
// @checkJs: false
// @noEmit: true

// @fileName: a.js
// @check true
var x = "string";
x = 0;
8 changes: 8 additions & 0 deletions tests/cases/compiler/checkJsFiles5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true

// @fileName: a.js
// @check false
var x = "string";
x = 0;
6 changes: 6 additions & 0 deletions tests/cases/compiler/checkJsFiles6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @allowJs: false
// @checkJs: true
// @noEmit: true

// @fileName: a.js
var x;