Skip to content

Commit c87bce1

Browse files
authored
Merge pull request #11978 from Microsoft/errorReportingInJsFile
Report all the js file errors and skip only the nodes that are not allowed in JS file
2 parents c458576 + 2eba10a commit c87bce1

12 files changed

+211
-112
lines changed

src/compiler/program.ts

Lines changed: 115 additions & 112 deletions
Large diffs are not rendered by default.

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ namespace ts {
514514

515515
export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic {
516516
const sourceFile = getSourceFileOfNode(node);
517+
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2);
518+
}
519+
520+
export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic {
517521
const span = getErrorSpanForNode(sourceFile, node);
518522
return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2);
519523
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/a.js(4,12): error TS8010: 'types' can only be used in a .ts file.
2+
3+
4+
==== tests/cases/compiler/a.js (1 errors) ====
5+
6+
@SomeDecorator
7+
class SomeClass {
8+
foo(x: number) {
9+
~~~~~~
10+
!!! error TS8010: 'types' can only be used in a .ts file.
11+
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/a.js(3,7): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
2+
tests/cases/compiler/a.js(4,12): error TS8010: 'types' can only be used in a .ts file.
3+
4+
5+
==== tests/cases/compiler/a.js (2 errors) ====
6+
7+
@SomeDecorator
8+
class SomeClass {
9+
~~~~~~~~~
10+
!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
11+
foo(x: number) {
12+
~~~~~~
13+
!!! error TS8010: 'types' can only be used in a .ts file.
14+
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
3+
tests/cases/compiler/a.js(1,1): error TS8009: 'abstract' can only be used in a .ts file.
4+
tests/cases/compiler/a.js(2,5): error TS8009: 'abstract' can only be used in a .ts file.
5+
6+
7+
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
8+
!!! 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
9+
==== tests/cases/compiler/a.js (2 errors) ====
10+
abstract class c {
11+
~~~~~~~~
12+
!!! error TS8009: 'abstract' can only be used in a .ts file.
13+
abstract x;
14+
~~~~~~~~
15+
!!! error TS8009: 'abstract' can only be used in a .ts file.
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/a.js ===
2+
const c = 10;
3+
>c : Symbol(c, Decl(a.js, 0, 5))
4+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/a.js ===
2+
const c = 10;
3+
>c : 10
4+
>10 : 10
5+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @experimentaldecorators: true
2+
// @emitdecoratormetadata: true
3+
// @allowjs: true
4+
// @noEmit: true
5+
6+
// @filename: a.js
7+
@SomeDecorator
8+
class SomeClass {
9+
foo(x: number) {
10+
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @allowjs: true
2+
// @noEmit: true
3+
4+
// @filename: a.js
5+
@SomeDecorator
6+
class SomeClass {
7+
foo(x: number) {
8+
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @allowJs: true
2+
// @filename: a.js
3+
abstract class c {
4+
abstract x;
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @allowJs: true
2+
// @filename: a.js
3+
// @noEmit: true
4+
const c = 10;

tests/cases/fourslash/server/getJavaScriptSyntacticDiagnostics02.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ verify.getSyntacticDiagnostics(`[
1515
"category": "error",
1616
"code": 8010
1717
},
18+
{
19+
"message": "\'types\' can only be used in a .ts file.",
20+
"start": 52,
21+
"length": 6,
22+
"category": "error",
23+
"code": 8010
24+
},
1825
{
1926
"message": "Variable declaration expected.",
2027
"start": 67,

0 commit comments

Comments
 (0)