From dcb763f62435ebb015e7fa405eb067de3254f217 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Fri, 16 Aug 2019 06:49:50 +0800 Subject: [PATCH] Report error when using generic class in js file (#31723) * Report error when using generic class in js file * Replace "ClassDeclaration | ClassExpression" to ClassLikeDeclaration Co-Authored-By: Klaus Meinhardt * add noEmit:true --- src/compiler/program.ts | 3 ++- ...ilationTypeParameterSyntaxOfClassExpression.errors.txt | 8 ++++++++ ...ompilationTypeParameterSyntaxOfClassExpression.symbols | 5 +++++ ...eCompilationTypeParameterSyntaxOfClassExpression.types | 5 +++++ ...FileCompilationTypeParameterSyntaxOfClassExpression.ts | 4 ++++ 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.errors.txt create mode 100644 tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.symbols create mode 100644 tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.types create mode 100644 tests/cases/compiler/jsFileCompilationTypeParameterSyntaxOfClassExpression.ts diff --git a/src/compiler/program.ts b/src/compiler/program.ts index c3fdad71e11..76c92ad74bb 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1833,6 +1833,7 @@ namespace ts { switch (parent.kind) { case SyntaxKind.ClassDeclaration: + case SyntaxKind.ClassExpression: case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: case SyntaxKind.Constructor: @@ -1842,7 +1843,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; } diff --git a/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.errors.txt b/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.errors.txt new file mode 100644 index 00000000000..61bf225dddc --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.errors.txt @@ -0,0 +1,8 @@ +tests/cases/compiler/a.js(1,19): error TS8004: 'type parameter declarations' can only be used in a .ts file. + + +==== tests/cases/compiler/a.js (1 errors) ==== + const Bar = class {}; + ~ +!!! error TS8004: 'type parameter declarations' can only be used in a .ts file. + \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.symbols b/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.symbols new file mode 100644 index 00000000000..f3aa9cd5926 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.symbols @@ -0,0 +1,5 @@ +=== tests/cases/compiler/a.js === +const Bar = class {}; +>Bar : Symbol(Bar, Decl(a.js, 0, 5)) +>T : Symbol(T, Decl(a.js, 0, 18)) + diff --git a/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.types b/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.types new file mode 100644 index 00000000000..5538564ec86 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationTypeParameterSyntaxOfClassExpression.types @@ -0,0 +1,5 @@ +=== tests/cases/compiler/a.js === +const Bar = class {}; +>Bar : typeof Bar +>class {} : typeof Bar + diff --git a/tests/cases/compiler/jsFileCompilationTypeParameterSyntaxOfClassExpression.ts b/tests/cases/compiler/jsFileCompilationTypeParameterSyntaxOfClassExpression.ts new file mode 100644 index 00000000000..81b07637422 --- /dev/null +++ b/tests/cases/compiler/jsFileCompilationTypeParameterSyntaxOfClassExpression.ts @@ -0,0 +1,4 @@ +// @allowJs: true +// @noEmit: true +// @filename: a.js +const Bar = class {};