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 3 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: "checkJsFiles",
Copy link
Member

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.

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
}
}
5 changes: 5 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 hasCheckDirective = false;

// 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,17 @@ namespace ts {
amdDependencies.push(amdDependency);
}
}

const checkDirectiveRegEx = /^\/\/\s*@check\s*/gim;
hasCheckDirective = hasCheckDirective || !!checkDirectiveRegEx.exec(comment);
}
}

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

function setExternalModuleIndicator(sourceFile: SourceFile) {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,8 @@ namespace ts {
// 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);
const includeCheckDiagnostics = options.checkJsFiles || sourceFile.hasCheckDirective || !isSourceFileJavaScript(sourceFile);
const checkDiagnostics = includeCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : [];
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);

Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,7 @@
/* @internal */ moduleAugmentations: LiteralExpression[];
/* @internal */ patternAmbientModules?: PatternAmbientModule[];
/* @internal */ ambientModuleNames: string[];
/* @internal */ hasCheckDirective: boolean;
}

export interface Bundle extends Node {
Expand Down Expand Up @@ -3312,6 +3313,7 @@
alwaysStrict?: boolean; // Always combine with strict property
baseUrl?: string;
charset?: string;
checkJsFiles?: boolean;
/* @internal */ configFilePath?: string;
declaration?: boolean;
declarationDir?: string;
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 hasCheckDirective: boolean;

constructor(kind: SyntaxKind, pos: number, end: number) {
super(kind, pos, end);
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/checkJsFiles.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/compiler/a.js(3,1): error TS2322: Type '0' is not assignable to type 'string'.


!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
==== tests/cases/compiler/a.js (1 errors) ====

var x = "string";
x = 0;
~
!!! error TS2322: Type '0' is not assignable to type 'string'.
14 changes: 14 additions & 0 deletions tests/baselines/reference/checkJsFiles2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type 'string'.


!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
==== tests/cases/compiler/a.js (1 errors) ====

// @check
var x = "string";
x = 0;
~
!!! error TS2322: Type '0' is not assignable to type 'string'.
14 changes: 14 additions & 0 deletions tests/baselines/reference/checkJsFiles3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/compiler/a.js(4,1): error TS2322: Type '0' is not assignable to type 'string'.


!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
==== tests/cases/compiler/a.js (1 errors) ====

// @check
var x = "string";
x = 0;
~
!!! error TS2322: Type '0' is not assignable to type 'string'.
6 changes: 6 additions & 0 deletions tests/cases/compiler/checkJsFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @allowJs: true
// @checkJsFiles: true
Copy link
Member

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.


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

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

// @fileName: a.js
// @check
var x = "string";
x = 0;