From b43300e04d28dfd4b04d80d6f9a4e47adc446591 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Wed, 20 Apr 2022 22:30:32 +0300 Subject: [PATCH] fix(48759): throw an error on using import expression with type arguments --- src/compiler/checker.ts | 5 ++++- src/compiler/diagnosticMessages.json | 2 +- .../importCallExpressionWithTypeArgument.errors.txt | 8 ++++---- .../reference/importWithTypeArguments.errors.txt | 12 ++++++++++++ tests/baselines/reference/importWithTypeArguments.js | 8 ++++++++ .../reference/importWithTypeArguments.symbols | 7 +++++++ .../reference/importWithTypeArguments.types | 5 +++++ .../types/import/importWithTypeArguments.ts | 2 ++ 8 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/importWithTypeArguments.errors.txt create mode 100644 tests/baselines/reference/importWithTypeArguments.js create mode 100644 tests/baselines/reference/importWithTypeArguments.symbols create mode 100644 tests/baselines/reference/importWithTypeArguments.types create mode 100644 tests/cases/conformance/types/import/importWithTypeArguments.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3f5f51acd7179..eb75e014eab4f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -43862,6 +43862,9 @@ namespace ts { } function checkGrammarExpressionWithTypeArguments(node: ExpressionWithTypeArguments | TypeQueryNode) { + if (isExpressionWithTypeArguments(node) && isImportKeyword(node.expression) && node.typeArguments) { + return grammarErrorOnNode(node, Diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments); + } return checkGrammarTypeArguments(node, node.typeArguments); } @@ -44912,7 +44915,7 @@ namespace ts { } if (node.typeArguments) { - return grammarErrorOnNode(node, Diagnostics.Dynamic_import_cannot_have_type_arguments); + return grammarErrorOnNode(node, Diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments); } const nodeArguments = node.arguments; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index e8a765e514cb5..9e2c345ba702d 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -956,7 +956,7 @@ "category": "Error", "code": 1325 }, - "Dynamic import cannot have type arguments.": { + "This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.": { "category": "Error", "code": 1326 }, diff --git a/tests/baselines/reference/importCallExpressionWithTypeArgument.errors.txt b/tests/baselines/reference/importCallExpressionWithTypeArgument.errors.txt index 65b8a2d631ef3..53920e4654019 100644 --- a/tests/baselines/reference/importCallExpressionWithTypeArgument.errors.txt +++ b/tests/baselines/reference/importCallExpressionWithTypeArgument.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/dynamicImport/1.ts(2,10): error TS1326: Dynamic import cannot have type arguments. -tests/cases/conformance/dynamicImport/1.ts(3,10): error TS1326: Dynamic import cannot have type arguments. +tests/cases/conformance/dynamicImport/1.ts(2,10): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. +tests/cases/conformance/dynamicImport/1.ts(3,10): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. ==== tests/cases/conformance/dynamicImport/0.ts (0 errors) ==== @@ -9,7 +9,7 @@ tests/cases/conformance/dynamicImport/1.ts(3,10): error TS1326: Dynamic import c "use strict" var p1 = import>("./0"); // error ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1326: Dynamic import cannot have type arguments. +!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. var p2 = import<>("./0"); // error ~~~~~~~~~~~~~~~ -!!! error TS1326: Dynamic import cannot have type arguments. \ No newline at end of file +!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. \ No newline at end of file diff --git a/tests/baselines/reference/importWithTypeArguments.errors.txt b/tests/baselines/reference/importWithTypeArguments.errors.txt new file mode 100644 index 0000000000000..2981a63bec737 --- /dev/null +++ b/tests/baselines/reference/importWithTypeArguments.errors.txt @@ -0,0 +1,12 @@ +tests/cases/conformance/types/import/importWithTypeArguments.ts(1,1): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. +tests/cases/conformance/types/import/importWithTypeArguments.ts(2,11): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. + + +==== tests/cases/conformance/types/import/importWithTypeArguments.ts (2 errors) ==== + import + ~~~~~~~~~ +!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. + const a = import + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments. + \ No newline at end of file diff --git a/tests/baselines/reference/importWithTypeArguments.js b/tests/baselines/reference/importWithTypeArguments.js new file mode 100644 index 0000000000000..9a54a823050cb --- /dev/null +++ b/tests/baselines/reference/importWithTypeArguments.js @@ -0,0 +1,8 @@ +//// [importWithTypeArguments.ts] +import +const a = import + + +//// [importWithTypeArguments.js] +import; +var a = (import); diff --git a/tests/baselines/reference/importWithTypeArguments.symbols b/tests/baselines/reference/importWithTypeArguments.symbols new file mode 100644 index 0000000000000..5f769bb1978e7 --- /dev/null +++ b/tests/baselines/reference/importWithTypeArguments.symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/types/import/importWithTypeArguments.ts === +import +>T : Symbol(T) + +const a = import +>a : Symbol(a, Decl(importWithTypeArguments.ts, 1, 5)) + diff --git a/tests/baselines/reference/importWithTypeArguments.types b/tests/baselines/reference/importWithTypeArguments.types new file mode 100644 index 0000000000000..442b3cc9ed993 --- /dev/null +++ b/tests/baselines/reference/importWithTypeArguments.types @@ -0,0 +1,5 @@ +=== tests/cases/conformance/types/import/importWithTypeArguments.ts === +import +const a = import +>a : any + diff --git a/tests/cases/conformance/types/import/importWithTypeArguments.ts b/tests/cases/conformance/types/import/importWithTypeArguments.ts new file mode 100644 index 0000000000000..684260429c2b0 --- /dev/null +++ b/tests/cases/conformance/types/import/importWithTypeArguments.ts @@ -0,0 +1,2 @@ +import +const a = import