-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import assertion #40698
Import assertion #40698
Changes from 3 commits
e1b3b78
66d2273
5f3cea6
5a1af04
4b51ca9
375b433
41a881c
46a3eb1
a484ea2
d64f7ba
68c8c5d
29fe0d3
adcfd1b
fb01eb3
4f22a60
f5e594d
60434d1
ff87d3e
43b67b9
c69a05b
d1c48b5
d14f93a
aa8b856
7a5ec34
9cea9cf
6337a7e
eee2cab
2857d69
88e39d5
f941d92
106ff06
7df4964
6ea3c55
fc51c42
bf7ca71
463138a
b07c6e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -39000,11 +39000,11 @@ namespace ts { | |||||||||||||||||||
function checkAssertClause(declaration: ImportDeclaration | ExportDeclaration) { | ||||||||||||||||||||
if (declaration.assertClause) { | ||||||||||||||||||||
if (moduleKind !== ModuleKind.ESNext) { | ||||||||||||||||||||
error(declaration.assertClause, Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext); | ||||||||||||||||||||
return grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (isImportDeclaration(declaration) ? declaration.importClause?.isTypeOnly : declaration.isTypeOnly) { | ||||||||||||||||||||
error(declaration.assertClause, Diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports); | ||||||||||||||||||||
return grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
@@ -43162,7 +43162,16 @@ namespace ts { | |||||||||||||||||||
return false; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
function checkGrammarImportCallArguments(node: ImportCall, nodeArguments: NodeArray<Expression>): boolean { | ||||||||||||||||||||
function checkGrammarImportCallExpression(node: ImportCall): boolean { | ||||||||||||||||||||
if (moduleKind === ModuleKind.ES2015) { | ||||||||||||||||||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_esnext_commonjs_amd_system_or_umd); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (node.typeArguments) { | ||||||||||||||||||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_cannot_have_type_arguments); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const nodeArguments = node.arguments; | ||||||||||||||||||||
if (moduleKind !== ModuleKind.ESNext) { | ||||||||||||||||||||
// We are allowed trailing comma after proposal-import-assertions. | ||||||||||||||||||||
checkGrammarForDisallowedTrailingComma(nodeArguments); | ||||||||||||||||||||
|
@@ -43172,28 +43181,15 @@ namespace ts { | |||||||||||||||||||
return grammarErrorOnNode(assertionArgument, Diagnostics.Dynamic_import_only_supports_a_second_argument_when_the_module_option_is_set_to_esnext); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
if (nodeArguments.length !== 1) { | ||||||||||||||||||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_only_have_a_specifier_and_an_optional_assertion_as_arguments); | ||||||||||||||||||||
} | ||||||||||||||||||||
return false; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
function checkGrammarImportCallExpression(node: ImportCall): boolean { | ||||||||||||||||||||
if (moduleKind === ModuleKind.ES2015) { | ||||||||||||||||||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_esnext_commonjs_amd_system_or_umd); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (node.typeArguments) { | ||||||||||||||||||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_cannot_have_type_arguments); | ||||||||||||||||||||
if (nodeArguments.length === 0 || nodeArguments.length > 2) { | ||||||||||||||||||||
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_only_have_a_specifier_and_an_optional_assertion_as_arguments); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const nodeArguments = node.arguments; | ||||||||||||||||||||
if (checkGrammarImportCallArguments(node, nodeArguments)) { | ||||||||||||||||||||
return true; | ||||||||||||||||||||
} | ||||||||||||||||||||
// see: parseArgumentOrArrayLiteralElement...we use this function which parse arguments of callExpression to parse specifier for dynamic import. | ||||||||||||||||||||
// parseArgumentOrArrayLiteralElement allows spread element to be in an argument list which is not allowed as specifier in dynamic import. | ||||||||||||||||||||
if (nodeArguments.length && isSpreadElement(nodeArguments[0])) { | ||||||||||||||||||||
const spreadElement = forEach(nodeArguments, isSpreadElement); | ||||||||||||||||||||
if (spreadElement) { | ||||||||||||||||||||
return grammarErrorOnNode(nodeArguments[0], Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element); | ||||||||||||||||||||
} | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. This doesn't quite do what we want. We want to report the error on the spread element, not the first argument (which might not be the spread element). I think we want this instead:
Suggested change
|
||||||||||||||||||||
return false; | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1372,9 +1372,25 @@ | |||||
"category": "Error", | ||||||
"code": 1443 | ||||||
}, | ||||||
"'{0}' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { | ||||||
"category": "Error", | ||||||
"code": 1444 | ||||||
}, | ||||||
"'{0}' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { | ||||||
"category": "Error", | ||||||
"code": 1446 | ||||||
}, | ||||||
"'{0}' resolves to a type-only declaration and must be re-exported using a type-only re-export when 'isolatedModules' is enabled.": { | ||||||
"category": "Error", | ||||||
"code": 1448 | ||||||
}, | ||||||
"Preserve unused imported values in the JavaScript output that would otherwise be removed.": { | ||||||
"category": "Message", | ||||||
"code": 1449 | ||||||
}, | ||||||
"Dynamic import must only have a specifier and an optional assertion as arguments": { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, but I think |
||||||
"category": "Message", | ||||||
"code": 1444 | ||||||
"code": 1450 | ||||||
}, | ||||||
|
||||||
"The types of '{0}' are incompatible between these types.": { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7341,8 +7341,8 @@ namespace ts { | |
updateNamespaceExportDeclaration(node: NamespaceExportDeclaration, name: Identifier): NamespaceExportDeclaration; | ||
createImportEqualsDeclaration(decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, isTypeOnly: boolean, name: string | Identifier, moduleReference: ModuleReference): ImportEqualsDeclaration; | ||
updateImportEqualsDeclaration(node: ImportEqualsDeclaration, decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, isTypeOnly: boolean, name: Identifier, moduleReference: ModuleReference): ImportEqualsDeclaration; | ||
createImportDeclaration(decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, assertClause: AssertClause | undefined): ImportDeclaration; | ||
updateImportDeclaration(node: ImportDeclaration, decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, assertClause: AssertClause | undefined): ImportDeclaration; | ||
createImportDeclaration(decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, assertClause?: AssertClause): ImportDeclaration; | ||
updateImportDeclaration(node: ImportDeclaration, decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, assertClause?: AssertClause): ImportDeclaration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would leave |
||
createImportClause(isTypeOnly: boolean, name: Identifier | undefined, namedBindings: NamedImportBindings | undefined): ImportClause; | ||
updateImportClause(node: ImportClause, isTypeOnly: boolean, name: Identifier | undefined, namedBindings: NamedImportBindings | undefined): ImportClause; | ||
createAssertClause(elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause; | ||
|
@@ -7360,7 +7360,7 @@ namespace ts { | |
createExportAssignment(decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, isExportEquals: boolean | undefined, expression: Expression): ExportAssignment; | ||
updateExportAssignment(node: ExportAssignment, decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, expression: Expression): ExportAssignment; | ||
createExportDeclaration(decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier?: Expression, assertClause?: AssertClause): ExportDeclaration; | ||
updateExportDeclaration(node: ExportDeclaration, decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier: Expression | undefined, assertClause: AssertClause | undefined): ExportDeclaration; | ||
updateExportDeclaration(node: ExportDeclaration, decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, isTypeOnly: boolean, exportClause: NamedExportBindings | undefined, moduleSpecifier: Expression | undefined, assertClause?: AssertClause): ExportDeclaration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also would leave |
||
createNamedExports(elements: readonly ExportSpecifier[]): NamedExports; | ||
updateNamedExports(node: NamedExports, elements: readonly ExportSpecifier[]): NamedExports; | ||
createExportSpecifier(propertyName: string | Identifier | undefined, name: string | Identifier): ExportSpecifier; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,26 @@ | ||
tests/cases/conformance/importAssertion/1.ts(1,27): error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
tests/cases/conformance/importAssertion/1.ts(1,27): error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
tests/cases/conformance/importAssertion/1.ts(2,30): error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
tests/cases/conformance/importAssertion/1.ts(2,30): error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
tests/cases/conformance/importAssertion/2.ts(1,31): error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
tests/cases/conformance/importAssertion/2.ts(1,31): error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
tests/cases/conformance/importAssertion/2.ts(2,33): error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
tests/cases/conformance/importAssertion/2.ts(2,33): error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
|
||
|
||
==== tests/cases/conformance/importAssertion/0.ts (0 errors) ==== | ||
export interface I { } | ||
|
||
==== tests/cases/conformance/importAssertion/1.ts (4 errors) ==== | ||
==== tests/cases/conformance/importAssertion/1.ts (2 errors) ==== | ||
export type {} from './0' assert { type: "json" } | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
export type { I } from './0' assert { type: "json" } | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
|
||
==== tests/cases/conformance/importAssertion/2.ts (4 errors) ==== | ||
==== tests/cases/conformance/importAssertion/2.ts (2 errors) ==== | ||
import type { I } from './0' assert { type: "json" } | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
import type * as foo from './0' assert { type: "json" } | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2820: Import assertions are only supported when the '--module' option is set to 'esnext'. | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2821: Import assertions cannot be used with type-only imports or exports. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: remove extra space