Skip to content

Commit f945b26

Browse files
author
Andy
authored
Forbid type assertions in '.js' files (#17503)
1 parent b0435d8 commit f945b26

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16384,10 +16384,6 @@ namespace ts {
1638416384
}
1638516385

1638616386
function checkNonNullAssertion(node: NonNullExpression) {
16387-
if (isInJavaScriptFile(node)) {
16388-
error(node, Diagnostics.non_null_assertions_can_only_be_used_in_a_ts_file);
16389-
}
16390-
1639116387
return getNonNullableType(checkExpression(node.expression));
1639216388
}
1639316389

src/compiler/program.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,10 +1196,14 @@ namespace ts {
11961196
case SyntaxKind.EnumDeclaration:
11971197
diagnostics.push(createDiagnosticForNode(node, Diagnostics.enum_declarations_can_only_be_used_in_a_ts_file));
11981198
return;
1199-
case SyntaxKind.TypeAssertionExpression:
1200-
const typeAssertionExpression = <TypeAssertion>node;
1201-
diagnostics.push(createDiagnosticForNode(typeAssertionExpression.type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file));
1199+
case SyntaxKind.NonNullExpression:
1200+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.non_null_assertions_can_only_be_used_in_a_ts_file));
1201+
return;
1202+
case SyntaxKind.AsExpression:
1203+
diagnostics.push(createDiagnosticForNode((node as AsExpression).type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file));
12021204
return;
1205+
case SyntaxKind.TypeAssertionExpression:
1206+
Debug.fail(); // Won't parse these in a JS file anyway, as they are interpreted as JSX.
12031207
}
12041208

12051209
const prevParent = parent;
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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,10): error TS17008: JSX element 'string' has no corresponding closing tag.
4-
tests/cases/compiler/a.js(1,27): error TS1005: '</' expected.
1+
/src/a.js(1,6): error TS8016: 'type assertion expressions' can only be used in a .ts file.
2+
/src/a.js(2,10): error TS17008: JSX element 'string' has no corresponding closing tag.
3+
/src/a.js(3,1): error TS1005: '</' expected.
54

65

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) ====
6+
==== /src/a.js (3 errors) ====
7+
0 as number;
8+
~~~~~~
9+
!!! error TS8016: 'type assertion expressions' can only be used in a .ts file.
1010
var v = <string>undefined;
1111
~~~~~~
1212
!!! error TS17008: JSX element 'string' has no corresponding closing tag.
13-
13+
14+
1415
!!! error TS1005: '</' expected.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [a.js]
2+
0 as number;
3+
var v = <string>undefined;
4+
5+
6+
//// [a.js]
7+
0;
8+
var v = <string>undefined;
9+
</>;
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// @allowJs: true
2-
// @checkJs: true
32
// @filename: /src/a.js
43
// @out: /bin/a.js
54
0!
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// @allowJs: true
2-
// @filename: a.js
3-
var v = <string>undefined;
2+
// @filename: /src/a.js
3+
// @out: /lib/a.js
4+
0 as number;
5+
var v = <string>undefined;

0 commit comments

Comments
 (0)