From fa4f2e5148ac8843fd8e6632483f85fb1279ffca Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 25 Jun 2024 07:31:32 -0700 Subject: [PATCH] Allow dynamic import attributes in `--module preserve` (#59005) --- src/compiler/checker.ts | 4 +-- src/compiler/diagnosticMessages.json | 2 +- ...portAssertion1(module=commonjs).errors.txt | 28 +++++++++---------- ...ortAttributes1(module=commonjs).errors.txt | 28 +++++++++---------- ...mportCallExpressionGrammarError.errors.txt | 4 +-- tests/cases/compiler/modulePreserve5.ts | 12 ++++++++ 6 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 tests/cases/compiler/modulePreserve5.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b6c8309f9e6..146bbc540b0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -51914,13 +51914,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } const nodeArguments = node.arguments; - if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.NodeNext && moduleKind !== ModuleKind.Node16) { + if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.NodeNext && moduleKind !== ModuleKind.Node16 && moduleKind !== ModuleKind.Preserve) { // We are allowed trailing comma after proposal-import-assertions. checkGrammarForDisallowedTrailingComma(nodeArguments); if (nodeArguments.length > 1) { const importAttributesArgument = nodeArguments[1]; - return grammarErrorOnNode(importAttributesArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_node16_or_nodenext); + return grammarErrorOnNode(importAttributesArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_node16_nodenext_or_preserve); } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 3ee47484968..ee05ca893c7 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1028,7 +1028,7 @@ "category": "Error", "code": 1323 }, - "Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'.": { + "Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'.": { "category": "Error", "code": 1324 }, diff --git a/tests/baselines/reference/importAssertion1(module=commonjs).errors.txt b/tests/baselines/reference/importAssertion1(module=commonjs).errors.txt index f82c3d583d8..60c1ebb002d 100644 --- a/tests/baselines/reference/importAssertion1(module=commonjs).errors.txt +++ b/tests/baselines/reference/importAssertion1(module=commonjs).errors.txt @@ -3,14 +3,14 @@ 1.ts(3,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'. 2.ts(1,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'. 2.ts(2,38): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'. -3.ts(2,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(3,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(4,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(5,26): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(7,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +3.ts(2,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(3,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(4,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(5,26): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(7,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. 3.ts(8,11): message TS1450: Dynamic imports can only accept a module specifier and an optional set of attributes as arguments -3.ts(9,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(10,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +3.ts(9,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(10,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. 3.ts(10,52): error TS1009: Trailing comma not allowed. @@ -49,29 +49,29 @@ const a = import('./0') const b = import('./0', { assert: { type: "json" } }) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const c = import('./0', { assert: { type: "json", ttype: "typo" } }) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const d = import('./0', { assert: {} }) ~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const dd = import('./0', {}) ~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. declare function foo(): any; const e = import('./0', foo()) ~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const f = import() ~~~~~~~~ !!! message TS1450: Dynamic imports can only accept a module specifier and an optional set of attributes as arguments const g = import('./0', {}, {}) ~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const h = import('./0', { assert: { type: "json" }},) ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. ~ !!! error TS1009: Trailing comma not allowed. diff --git a/tests/baselines/reference/importAttributes1(module=commonjs).errors.txt b/tests/baselines/reference/importAttributes1(module=commonjs).errors.txt index 41f8ab32df2..8a9be458977 100644 --- a/tests/baselines/reference/importAttributes1(module=commonjs).errors.txt +++ b/tests/baselines/reference/importAttributes1(module=commonjs).errors.txt @@ -3,14 +3,14 @@ 1.ts(3,28): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'. 2.ts(1,28): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'. 2.ts(2,38): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'. -3.ts(2,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(3,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(4,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(5,26): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(7,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +3.ts(2,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(3,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(4,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(5,26): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(7,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. 3.ts(8,11): message TS1450: Dynamic imports can only accept a module specifier and an optional set of attributes as arguments -3.ts(9,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. -3.ts(10,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +3.ts(9,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. +3.ts(10,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. 3.ts(10,50): error TS1009: Trailing comma not allowed. @@ -47,29 +47,29 @@ const a = import('./0') const b = import('./0', { with: { type: "json" } }) ~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const c = import('./0', { with: { type: "json", ttype: "typo" } }) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const d = import('./0', { with: {} }) ~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const dd = import('./0', {}) ~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. declare function foo(): any; const e = import('./0', foo()) ~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const f = import() ~~~~~~~~ !!! message TS1450: Dynamic imports can only accept a module specifier and an optional set of attributes as arguments const g = import('./0', {}, {}) ~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. const h = import('./0', { with: { type: "json" }},) ~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. ~ !!! error TS1009: Trailing comma not allowed. \ No newline at end of file diff --git a/tests/baselines/reference/importCallExpressionGrammarError.errors.txt b/tests/baselines/reference/importCallExpressionGrammarError.errors.txt index a79891a2fc6..d8424c5423e 100644 --- a/tests/baselines/reference/importCallExpressionGrammarError.errors.txt +++ b/tests/baselines/reference/importCallExpressionGrammarError.errors.txt @@ -2,7 +2,7 @@ importCallExpressionGrammarError.ts(5,8): error TS1325: Argument of dynamic impo importCallExpressionGrammarError.ts(7,17): error TS1325: Argument of dynamic import cannot be spread element. importCallExpressionGrammarError.ts(8,12): message TS1450: Dynamic imports can only accept a module specifier and an optional set of attributes as arguments importCallExpressionGrammarError.ts(9,19): error TS2307: Cannot find module 'pathToModule' or its corresponding type declarations. -importCallExpressionGrammarError.ts(9,35): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +importCallExpressionGrammarError.ts(9,35): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. importCallExpressionGrammarError.ts(9,35): error TS2559: Type '"secondModule"' has no properties in common with type 'ImportCallOptions'. @@ -25,6 +25,6 @@ importCallExpressionGrammarError.ts(9,35): error TS2559: Type '"secondModule"' h ~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'pathToModule' or its corresponding type declarations. ~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. +!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'. ~~~~~~~~~~~~~~ !!! error TS2559: Type '"secondModule"' has no properties in common with type 'ImportCallOptions'. \ No newline at end of file diff --git a/tests/cases/compiler/modulePreserve5.ts b/tests/cases/compiler/modulePreserve5.ts new file mode 100644 index 00000000000..576adc183ce --- /dev/null +++ b/tests/cases/compiler/modulePreserve5.ts @@ -0,0 +1,12 @@ +// @module: preserve +// @target: esnext +// @resolveJsonModule: true +// @noEmit: true +// @noTypesAndSymbols: true + +// @Filename: data.json +{} + +// @Filename: main.ts +import data1 from "./data.json" with { type: "json" }; +const data2 = await import("./data.json", { with: { type: "json" } });