Skip to content

Commit 1d942f4

Browse files
committed
Detect more TS syntax in JS files
* optional class methods * type arguments on tagged template expressions
1 parent b57b5fe commit 1d942f4

6 files changed

+68
-16
lines changed

src/compiler/program.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,13 +1760,12 @@ namespace ts {
17601760
switch (parent.kind) {
17611761
case SyntaxKind.Parameter:
17621762
case SyntaxKind.PropertyDeclaration:
1763-
if ((<ParameterDeclaration | PropertyDeclaration>parent).questionToken === node) {
1763+
case SyntaxKind.MethodDeclaration:
1764+
if ((<ParameterDeclaration | PropertyDeclaration | MethodDeclaration>parent).questionToken === node) {
17641765
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_can_only_be_used_in_a_ts_file, "?"));
17651766
return;
17661767
}
17671768
// falls through
1768-
case SyntaxKind.MethodDeclaration:
1769-
case SyntaxKind.MethodSignature:
17701769
case SyntaxKind.Constructor:
17711770
case SyntaxKind.GetAccessor:
17721771
case SyntaxKind.SetAccessor:
@@ -1835,23 +1834,22 @@ 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
case SyntaxKind.VariableStatement:
18521850
// Check modifiers
1853-
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration | VariableStatement>parent).modifiers) {
1854-
return checkModifiers(<NodeArray<Modifier>>nodes, parent.kind === SyntaxKind.VariableStatement);
1851+
if (nodes === parent.modifiers) {
1852+
return checkModifiers(parent.modifiers, parent.kind === SyntaxKind.VariableStatement);
18551853
}
18561854
break;
18571855
case SyntaxKind.PropertyDeclaration:
@@ -1877,8 +1875,9 @@ namespace ts {
18771875
case SyntaxKind.ExpressionWithTypeArguments:
18781876
case SyntaxKind.JsxSelfClosingElement:
18791877
case SyntaxKind.JsxOpeningElement:
1878+
case SyntaxKind.TaggedTemplateExpression:
18801879
// Check type arguments
1881-
if (nodes === (<CallExpression | NewExpression | ExpressionWithTypeArguments | JsxOpeningLikeElement>parent).typeArguments) {
1880+
if (nodes === (<NodeWithTypeArguments>parent).typeArguments) {
18821881
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_arguments_can_only_be_used_in_a_ts_file));
18831882
return;
18841883
}
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 />;
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)