Skip to content

Commit

Permalink
Merge pull request #32965 from ajafff/ts-in-js
Browse files Browse the repository at this point in the history
Detect more TS syntax in JS files
  • Loading branch information
Orta authored Sep 12, 2019
2 parents fd6fbdf + 2c967c4 commit 0cf00fa
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 25 deletions.
17 changes: 8 additions & 9 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1759,13 +1759,12 @@ namespace ts {
switch (parent.kind) {
case SyntaxKind.Parameter:
case SyntaxKind.PropertyDeclaration:
if ((<ParameterDeclaration | PropertyDeclaration>parent).questionToken === node) {
case SyntaxKind.MethodDeclaration:
if ((<ParameterDeclaration | PropertyDeclaration | MethodDeclaration>parent).questionToken === node) {
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_can_only_be_used_in_a_ts_file, "?"));
return;
}
// falls through

case SyntaxKind.MethodDeclaration:
// falls through
case SyntaxKind.MethodSignature:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
Expand Down Expand Up @@ -1835,24 +1834,23 @@ namespace ts {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.FunctionExpression:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ArrowFunction:
// Check type parameters
if (nodes === (<ClassLikeDeclaration | FunctionLikeDeclaration>parent).typeParameters) {
if (nodes === (<DeclarationWithTypeParameterChildren>parent).typeParameters) {
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file));
return;
}
// falls through

case SyntaxKind.VariableStatement:
// Check modifiers
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration | VariableStatement>parent).modifiers) {
return checkModifiers(<NodeArray<Modifier>>nodes, parent.kind === SyntaxKind.VariableStatement);
if (nodes === parent.modifiers) {
return checkModifiers(parent.modifiers, parent.kind === SyntaxKind.VariableStatement);
}
break;
case SyntaxKind.PropertyDeclaration:
Expand All @@ -1878,8 +1876,9 @@ namespace ts {
case SyntaxKind.ExpressionWithTypeArguments:
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.TaggedTemplateExpression:
// Check type arguments
if (nodes === (<CallExpression | NewExpression | ExpressionWithTypeArguments | JsxOpeningLikeElement>parent).typeArguments) {
if (nodes === (<NodeWithTypeArguments>parent).typeArguments) {
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_arguments_can_only_be_used_in_a_ts_file));
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/compiler/a.js(2,8): error TS8009: '?' can only be used in a .ts file.
tests/cases/compiler/a.js(4,8): error TS8009: '?' can only be used in a .ts file.


!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
!!! 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.
==== tests/cases/compiler/a.js (2 errors) ====
class C {
foo?() {
~
!!! error TS8009: '?' can only be used in a .ts file.
}
bar? = 1;
~
!!! error TS8009: '?' can only be used in a .ts file.
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/compiler/a.js(1,5): error TS8011: 'type arguments' can only be used in a .ts file.
tests/cases/compiler/a.jsx(1,5): error TS8011: 'type arguments' can only be used in a .ts file.
tests/cases/compiler/a.jsx(2,5): error TS8011: 'type arguments' can only be used in a .ts file.
tests/cases/compiler/a.jsx(3,6): error TS8011: 'type arguments' can only be used in a .ts file.
tests/cases/compiler/a.jsx(4,6): error TS8011: 'type arguments' can only be used in a .ts file.


!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
!!! 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.
==== tests/cases/compiler/a.js (1 errors) ====
==== tests/cases/compiler/a.jsx (4 errors) ====
Foo<number>();
~~~~~~
!!! error TS8011: 'type arguments' can only be used in a .ts file.
Foo<number>``;
~~~~~~
!!! error TS8011: 'type arguments' can only be used in a .ts file.
<Foo<number>></Foo>;
~~~~~~
!!! error TS8011: 'type arguments' can only be used in a .ts file.
<Foo<number>/>;
~~~~~~
!!! error TS8011: 'type arguments' can only be used in a .ts file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [a.jsx]
Foo<number>();
Foo<number>``;
<Foo<number>></Foo>;
<Foo<number>/>;

//// [a.js]
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
Foo();
Foo(__makeTemplateObject([""], [""]));
<Foo></Foo>;
<Foo />;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @allowJs: true
// @noTypesAndSymbols: true
// @filename: a.js
class C {
foo?() {
}
bar? = 1;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// @allowJs: true
// @filename: a.js
Foo<number>();
// @noTypesAndSymbols: true
// @filename: a.jsx
Foo<number>();
Foo<number>``;
<Foo<number>></Foo>;
<Foo<number>/>;

0 comments on commit 0cf00fa

Please sign in to comment.