8000 Merge pull request #5839 from Microsoft/jsBinderErrors · ukyo/TypeScript@0ab538f · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 0ab538f

Browse files
committed
Merge pull request microsoft#5839 from Microsoft/jsBinderErrors
Report binder diagnostics, program diagnostics as well as file pre processing diagnostics in javascript file
2 parents 3bb5dc6 + 4fcb53b commit 0ab538f

16 files changed

+299
-56
lines changed

src/compiler/program.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,17 @@ namespace ts {
661661
}
662662

663663
function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
664-
// For JavaScript files, we don't want to report the normal typescript semantic errors.
665-
// Instead, we just report errors for using TypeScript-only constructs from within a
666-
// JavaScript file.
667-
if (isSourceFileJavaScript(sourceFile)) {
668-
return getJavaScriptSemanticDiagnosticsForFile(sourceFile, cancellationToken);
669-
}
670-
671664
return runWithCancellationToken(() => {
672665
const typeChecker = getDiagnosticsProducingTypeChecker();
673666

674667
Debug.assert(!!sourceFile.bindDiagnostics);
675668
const bindDiagnostics = sourceFile.bindDiagnostics;
676-
const checkDiagnostics = typeChecker.getDiagnostics(sourceFile, cancellationToken);
669+
// For JavaScript files, we don't want to report the normal typescript semantic errors.
670+
// Instead, we just report errors for using TypeScript-only constructs from within a
671+
// JavaScript file.
672+
const checkDiagnostics = isSourceFileJavaScript(sourceFile) ?
673+
getJavaScriptSemanticDiagnosticsForFile(sourceFile, cancellationToken) :
674+
typeChecker.getDiagnostics(sourceFile, cancellationToken);
677675
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
678676
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
679677

@@ -1079,6 +1077,8 @@ namespace ts {
10791077
const importedFile = findSourceFile(resolution.resolvedFileName, toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName), /*isDefaultLib*/ false, file, skipTrivia(file.text, file.imports[i].pos), file.imports[i].end);
10801078

10811079
if (importedFile && resolution.isExternalLibraryImport) {
1080+
// Since currently irrespective of allowJs, we only look for supportedTypeScript extension external module files,
1081+
// this check is ok. Otherwise this would be never true for javascript file
10821082
if (!isExternalModule(importedFile)) {
10831083
const start = getTokenPosOfNode(file.imports[i], file);
10841084
fileProcessingDiagnostics.add(createFileDiagnostic(file, start, file.imports[i].end - start, Diagnostics.Exported_external_package_typings_file_0_is_not_a_module_Please_contact_the_package_author_to_update_the_package_definition, importedFile.fileName));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/a.js(1,5): error TS2300: Duplicate identifier 'a'.
2+
tests/cases/compiler/a.js(2,7): error TS2300: Duplicate identifier 'a'.
3+
4+
5+
==== tests/cases/compiler/a.js (2 errors) ====
6+
var a = 10;
7+
~
8+
!!! error TS2300: Duplicate identifier 'a'.
9+
class a {
10+
~
11+
!!! error TS2300: Duplicate identifier 'a'.
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/a.js(1,5): error TS2451: Cannot redeclare block-scoped variable 'C'.
2+
tests/cases/compiler/a.js(2,5): error TS2451: Cannot redeclare block-scoped variable 'C'.
3+
tests/cases/compiler/a.js(6,5): error TS7027: Unreachable code detected.
4+
tests/cases/compiler/a.js(11,9): error TS1100: Invalid use of 'arguments' in strict mode.
5+
6+
7+
==== tests/cases/compiler/a.js (4 errors) ====
8+
let C = "sss";
9+
~
10+
!!! error TS2451: Cannot redeclare block-scoped variable 'C'.
11+
let C = 0; // Error: Cannot redeclare block-scoped variable 'C'.
12+
~
13+
!!! error TS2451: Cannot redeclare block-scoped variable 'C'.
14+
15+
function f() {
16+
return;
17+
return; // Error: Unreachable code detected.
18+
~~~~~~
19+
!!! error TS7027: Unreachable code detected.
20+
}
21+
22+
function b() {
23+
"use strict";
24+
var arguments = 0; // Error: Invalid use of 'arguments' in strict mode.
25+
~~~~~~~~~
26+
!!! error TS1100: Invalid use of 'arguments' in strict mode.
27+
}

tests/baselines/reference/jsFileCompilationBindErrors.symbols

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/baselines/reference/jsFileCompilationBindErrors.types

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests/cases/compiler/a.js(1,22): error TS2528: A module cannot have multiple default exports.
2+
tests/cases/compiler/a.js(3,1): error TS2528: A module cannot have multiple default exports.
3+
tests/cases/compiler/a.js(3,1): error TS8003: 'export=' can only be used in a .ts file.
4+
tests/cases/compiler/a.js(3,16): error 10000 TS1109: Expression expected.
5+
6+
7+
==== tests/cases/compiler/a.js (4 errors) ====
8+
export default class a {
9+
~
10+
!!! error TS2528: A module cannot have multiple default exports.
11+
}
12+
export default var a = 10;
13+
~~~~~~~~~~~~~~
14+
!!! error TS2528: A module cannot have multiple default exports.
15+
~~~~~~~~~~~~~~
16+
!!! error TS8003: 'export=' can only be used in a .ts file.
17+
~~~
18+
!!! error TS1109: Expression expected.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/compiler/a.js(3,9): error TS7029: Fallthrough case in switch.
2+
tests/cases/compiler/a.js(16,5): error TS7027: Unreachable code detected.
3+
tests/cases/compiler/a.js(19,1): error TS7028: Unused label.
4+
5+
6+
==== tests/cases/compiler/a.js (3 errors) ====
7+
function foo(a, b) {
8+
switch (a) {
9+
case 10:
10+
~~~~
11+
!!! error TS7029: Fallthrough case in switch.
12+
if (b) {
13+
return b;
14+
}
15+
case 20:
16+
return a;
17+
}
18+
}
19+
20+
function bar() {
21+
return x;
22+
function bar2() {
23+
}
24+
var x = 10; // error
25+
~~~
26+
!!! error TS7027: Unreachable code detected.
27+
}
28+
29+
label1: var x2 = 10;
30+
~~~~~~
31+
!!! error TS7028: Unused label.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
tests/cases/compiler/a.js(3,5): error TS2300: Duplicate identifier 'a'.
2+
tests/cases/compiler/a.js(5,5): error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
3+
tests/cases/compiler/a.js(5,5): error TS2300: Duplicate identifier 'a'.
4+
tests/cases/compiler/a.js(7,5): error TS1212: Identifier expected. 'let' is a reserved word in strict mode
5+
tests/cases/compiler/a.js(8,8): error TS1102: 'delete' cannot be called on an identifier in strict mode.
6+
tests/cases/compiler/a.js(10,10): error TS1100: Invalid use of 'eval' in strict mode.
7+
tests/cases/compiler/a.js(12,10): error TS1100: Invalid use of 'arguments' in strict mode.
8+
tests/cases/compiler/a.js(15,1): error TS1101: 'with' statements are not allowed in strict mode.
9+
tests/cases/compiler/b.js(3,7): error TS1210: Invalid use of 'eval'. Class definitions are automatically in strict mode.
10+
tests/cases/compiler/b.js(6,13): error TS1213: Identifier expected. 'let' is a reserved word in strict mode. Class definitions are automatically in strict mode.
11+
tests/cases/compiler/c.js(1,12): error TS1214: Identifier expected. 'let' is a reserved word in strict mode. Modules are automatically in strict mode.
12+
tests/cases/compiler/c.js(2,5): error TS1215: Invalid use of 'eval'. Modules are automatically in strict mode.
13+
tests/cases/compiler/d.js(2,9): error TS1121: Octal literals are not allowed in strict mode.
14+
tests/cases/compiler/d.js(2,11): error TS1005: ',' expected.
15+
16+
17+
==== tests/cases/compiler/a.js (8 errors) ====
18+
"use strict";
19+
var a = {
20+
a: "hello", // error
21+
~
22+
!!! error TS2300: Duplicate identifier 'a'.
23+
b: 10,
24+
a: 10 // error
25+
~
26+
!!! error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
27+
~
28+
!!! error TS2300: Duplicate identifier 'a'.
29+
};
30+
var let = 10; // error
31+
~~~
32+
!!! error TS1212: Identifier expected. 'let' is a reserved word in strict mode
33+
delete a; // error
34+
~
35+
!!! error TS1102: 'delete' cannot be called on an identifier in strict mode.
36+
try {
37+
} catch (eval) { // error
38+
~~~~
39+
!!! error TS1100: Invalid use of 'eval' in strict mode.
40+
}
41+
function arguments() { // error
42+
~~~~~~~~~
43+
!!! error TS1100: Invalid use of 'arguments' in strict mode.
44+
}
45+
46+
with (a) {
47+
~~~~
48+
!!! error TS1101: 'with' statements are not allowed in strict mode.
49+
b = 10;
50+
}
51+
52+
==== tests/cases/compiler/b.js (2 errors) ====
53+
// this is not in strict mode but class definitions are always in strict mode
54+
class c {
55+
a(eval) { //error
56+
~~~~
57+
!!! error TS1210: Invalid use of 'eval'. Class definitions are automatically in strict mode.
58+
}
59+
method() {
60+
var let = 10; // error
61+
~~~
62+
!!! error TS1213: Identifier expected. 'let' is a reserved word in strict mode. Class definitions are automatically in strict mode.
63+
}
64+
}
65+
66+
==== tests/cases/compiler/c.js (2 errors) ====
67+
export var let = 10; // external modules are automatically in strict mode
68+
~~~
69+
!!! error TS1214: Identifier expected. 'let' is a reserved word in strict mode. Modules are automatically in strict mode.
70+
var eval = function () {
71+
~~~~
72+
!!! error TS1215: Invalid use of 'eval'. Modules are automatically in strict mode.
73+
};
74+
75+
==== tests/cases/compiler/d.js (2 errors) ====
76+
"use strict";
77+
var x = 009; // error
78+
~~
79+
!!! error TS1121: Octal literals are not allowed in strict mode.
80+
~
81+
!!! error TS1005: ',' expected.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
tests/cases/compiler/a.js(1,1): error TS1148: Cannot compile modules unless the '--module' flag is provided.
23
tests/cases/compiler/a.js(1,1): error TS8003: 'export=' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
6-
==== tests/cases/compiler/a.js (1 errors) ====
7+
==== tests/cases/compiler/a.js (2 errors) ====
78
export = b;
89
~~~~~~~~~~~
10+
!!! error TS1148: Cannot compile modules unless the '--module' flag is provided.
11+
~~~~~~~~~~~
912
!!! error TS8003: 'export=' can only be used in a .ts file.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/compiler/moduleA/a.js(2,17): error TS2656: Exported external package typings file 'tests/cases/compiler/node_modules/b.ts' is not a module. Please contact the package author to update the package definition.
2+
3+
4+
==== tests/cases/compiler/moduleA/a.js (1 errors) ====
5+
6+
import {a} from "b";
7+
~~~
8+
!!! error TS2656: Exported external package typings file 'b.ts' is not a module. Please contact the package author to update the package definition.
9+
a++;
10+
import {c} from "c";
11+
c++;
12+
13+
==== tests/cases/compiler/node_modules/b.ts (0 errors) ====
14+
var a = 10;
15+
16+
==== tests/cases/compiler/node_modules/c.js (0 errors) ====
17+
exports.a = 10;
18+
c = 10;
19+

0 commit comments

Comments
 (0)
0