mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
Merge pull request #32965 from ajafff/ts-in-js
Detect more TS syntax in JS files
This commit is contained in:
@@ -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:
|
||||
@@ -1835,7 +1834,6 @@ namespace ts {
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
case SyntaxKind.ClassExpression:
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.MethodSignature:
|
||||
case SyntaxKind.Constructor:
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
@@ -1843,7 +1841,7 @@ namespace ts {
|
||||
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;
|
||||
}
|
||||
@@ -1851,8 +1849,8 @@ namespace ts {
|
||||
|
||||
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:
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
@@ -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.
|
||||
@@ -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 />;
|
||||
@@ -1,3 +0,0 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
Foo<number>();
|
||||
No type information for this code.
|
||||
@@ -1,5 +0,0 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
Foo<number>();
|
||||
>Foo<number>() : any
|
||||
>Foo : any
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// @allowJs: true
|
||||
// @noTypesAndSymbols: true
|
||||
// @filename: a.js
|
||||
class C {
|
||||
foo?() {
|
||||
}
|
||||
bar? = 1;
|
||||
}
|
||||
@@ -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>/>;
|
||||
Reference in New Issue
Block a user