diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 275acb64641..68eccdff307 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1759,13 +1759,12 @@ namespace ts { switch (parent.kind) { case SyntaxKind.Parameter: case SyntaxKind.PropertyDeclaration: - if ((parent).questionToken === node) { + case SyntaxKind.MethodDeclaration: + if ((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 === (parent).typeParameters) { + if (nodes === (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 === (parent).modifiers) { - return checkModifiers(>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 === (parent).typeArguments) { + if (nodes === (parent).typeArguments) { diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_arguments_can_only_be_used_in_a_ts_file)); return; } diff --git a/tests/baselines/reference/jsFileCompilationOptionalClassElementSyntaxOfClass.errors.txt b/tests/baselines/reference/jsFileCompilationOptionalClassElementSyntaxOfClass.errors.txt new file mode 100644 index 00000000000..b4ebcf41983 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationOptionalClassElementSyntaxOfClass.errors.txt @@ -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. + } \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.errors.txt b/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.errors.txt index 8d8a1a1a83e..5b47b12c534 100644 --- a/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.errors.txt +++ b/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.errors.txt @@ -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(); ~~~~~~ +!!! error TS8011: 'type arguments' can only be used in a .ts file. + Foo``; + ~~~~~~ +!!! error TS8011: 'type arguments' can only be used in a .ts file. + >; + ~~~~~~ +!!! error TS8011: 'type arguments' can only be used in a .ts file. + />; + ~~~~~~ !!! error TS8011: 'type arguments' can only be used in a .ts file. \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.js b/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.js new file mode 100644 index 00000000000..f51afa0a6db --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.js @@ -0,0 +1,15 @@ +//// [a.jsx] +Foo(); +Foo``; +>; +/>; + +//// [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([""], [""])); +; +; diff --git a/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.symbols b/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.symbols deleted file mode 100644 index e0926bb6748..00000000000 --- a/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.symbols +++ /dev/null @@ -1,3 +0,0 @@ -=== tests/cases/compiler/a.js === -Foo(); -No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.types b/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.types deleted file mode 100644 index ac42dd32d4c..00000000000 --- a/tests/baselines/reference/jsFileCompilationTypeArgumentSyntaxOfCall.types +++ /dev/null @@ -1,5 +0,0 @@ -=== tests/cases/compiler/a.js === -Foo(); ->Foo() : any ->Foo : any - diff --git a/tests/cases/compiler/jsFileCompilationOptionalClassElementSyntaxOfClass.ts b/tests/cases/compiler/jsFileCompilationOptionalClassElementSyntaxOfClass.ts new file mode 100644 index 00000000000..27739f58d6c --- /dev/null +++ b/tests/cases/compiler/jsFileCompilationOptionalClassElementSyntaxOfClass.ts @@ -0,0 +1,8 @@ +// @allowJs: true +// @noTypesAndSymbols: true +// @filename: a.js +class C { + foo?() { + } + bar? = 1; +} \ No newline at end of file diff --git a/tests/cases/compiler/jsFileCompilationTypeArgumentSyntaxOfCall.ts b/tests/cases/compiler/jsFileCompilationTypeArgumentSyntaxOfCall.ts index a5bcdae904b..008714f0df7 100644 --- a/tests/cases/compiler/jsFileCompilationTypeArgumentSyntaxOfCall.ts +++ b/tests/cases/compiler/jsFileCompilationTypeArgumentSyntaxOfCall.ts @@ -1,3 +1,7 @@ // @allowJs: true -// @filename: a.js -Foo(); \ No newline at end of file +// @noTypesAndSymbols: true +// @filename: a.jsx +Foo(); +Foo``; +>; +/>; \ No newline at end of file