Skip to content

Commit 0cf00fa

Browse files
author
Orta
authored
Merge pull request #32965 from ajafff/ts-in-js
Detect more TS syntax in JS files
2 parents fd6fbdf + 2c967c4 commit 0cf00fa

8 files changed

+69
-25
lines changed

src/compiler/program.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,13 +1759,12 @@ namespace ts {
17591759
switch (parent.kind) {
17601760
case SyntaxKind.Parameter:
17611761
case SyntaxKind.PropertyDeclaration:
1762-
if ((<ParameterDeclaration | PropertyDeclaration>parent).questionToken === node) {
1762+
case SyntaxKind.MethodDeclaration:
1763+
if ((<ParameterDeclaration | PropertyDeclaration | MethodDeclaration>parent).questionToken === node) {
17631764
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_can_only_be_used_in_a_ts_file, "?"));
17641765
return;
17651766
}
1766-
// falls through
1767-
1768-
case SyntaxKind.MethodDeclaration:
1767+
// falls through
17691768
case SyntaxKind.MethodSignature:
17701769
case SyntaxKind.Constructor:
17711770
case SyntaxKind.GetAccessor:
@@ -1835,24 +1834,23 @@ namespace ts {
18351834
case SyntaxKind.ClassDeclaration:
18361835
case SyntaxKind.ClassExpression:
18371836
case SyntaxKind.MethodDeclaration:
1838-
case SyntaxKind.MethodSignature:
18391837
case SyntaxKind.Constructor:
18401838
case SyntaxKind.GetAccessor:
18411839
case SyntaxKind.SetAccessor:
18421840
case SyntaxKind.FunctionExpression:
18431841
case SyntaxKind.FunctionDeclaration:
18441842
case SyntaxKind.ArrowFunction:
18451843
// Check type parameters
1846-
if (nodes === (<ClassLikeDeclaration | FunctionLikeDeclaration>parent).typeParameters) {
1844+
if (nodes === (<DeclarationWithTypeParameterChildren>parent).typeParameters) {
18471845
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file));
18481846
return;
18491847
}
18501848
// falls through
18511849

18521850
case SyntaxKind.VariableStatement:
18531851
// Check modifiers
1854-
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration | VariableStatement>parent).modifiers) {
1855-
return checkModifiers(<NodeArray<Modifier>>nodes, parent.kind === SyntaxKind.VariableStatement);
1852+
if (nodes === parent.modifiers) {
1853+
return checkModifiers(parent.modifiers, parent.kind === SyntaxKind.VariableStatement);
18561854
}
18571855
break;
18581856
case SyntaxKind.PropertyDeclaration:
@@ -1878,8 +1876,9 @@ namespace ts {
18781876
case SyntaxKind.ExpressionWithTypeArguments:
18791877
case SyntaxKind.JsxSelfClosingElement:
18801878
case SyntaxKind.JsxOpeningElement:
1879+
case SyntaxKind.TaggedTemplateExpression:
18811880
// Check type arguments
1882-
if (nodes === (<CallExpression | NewExpression | ExpressionWithTypeArguments | JsxOpeningLikeElement>parent).typeArguments) {
1881+
if (nodes === (<NodeWithTypeArguments>parent).typeArguments) {
18831882
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_arguments_can_only_be_used_in_a_ts_file));
18841883
return;
18851884
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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(2,8): error TS8009: '?' can only be used in a .ts file.
4+
tests/cases/compiler/a.js(4,8): error TS8009: '?' 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+
class C {
11+
foo?() {
12+
~
13+
!!! error TS8009: '?' can only be used in a .ts file.
14+
}
15+
bar? = 1;
16+
~
17+
!!! error TS8009: '?' can only be used in a .ts file.
18+
}
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
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,5): error TS8011: 'type arguments' can only be used in a .ts file.
1+
tests/cases/compiler/a.jsx(1,5): error TS8011: 'type arguments' can only be used in a .ts file.
2+
tests/cases/compiler/a.jsx(2,5): error TS8011: 'type arguments' can only be used in a .ts file.
3+
tests/cases/compiler/a.jsx(3,6): error TS8011: 'type arguments' can only be used in a .ts file.
4+
tests/cases/compiler/a.jsx(4,6): error TS8011: 'type arguments' can only be used in a .ts file.
45

56

6-
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7-
!!! 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.
8-
==== tests/cases/compiler/a.js (1 errors) ====
7+
==== tests/cases/compiler/a.jsx (4 errors) ====
98
Foo<number>();
109
~~~~~~
10+
!!! error TS8011: 'type arguments' can only be used in a .ts file.
11+
Foo<number>``;
12+
~~~~~~
13+
!!! error TS8011: 'type arguments' can only be used in a .ts file.
14+
<Foo<number>></Foo>;
15+
~~~~~~
16+
!!! error TS8011: 'type arguments' can only be used in a .ts file.
17+
<Foo<number>/>;
18+
~~~~~~
1119
!!! error TS8011: 'type arguments' can only be used in a .ts file.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [a.jsx]
2+
Foo<number>();
3+
Foo<number>``;
4+
<Foo<number>></Foo>;
5+
<Foo<number>/>;
6+
7+
//// [a.js]
8+
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
9+
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
10+
return cooked;
11+
};
12+
Foo();
13+
Foo(__makeTemplateObject([""], [""]));
14+
<Foo></Foo>;
15+
<Foo />;

tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.symbols

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

tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.types

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @allowJs: true
2+
// @noTypesAndSymbols: true
3+
// @filename: a.js
4+
class C {
5+
foo?() {
6+
}
7+
bar? = 1;
8+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// @allowJs: true
2-
// @filename: a.js
3-
Foo<number>();
2+
// @noTypesAndSymbols: true
3+
// @filename: a.jsx
4+
Foo<number>();
5+
Foo<number>``;
6+
<Foo<number>></Foo>;
7+
<Foo<number>/>;

0 commit comments

Comments
 (0)