Skip to content
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

Merged
merged 37 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e1b3b78
Add parsing
Kingwl Sep 22, 2020
66d2273
fix all api
Kingwl Sep 22, 2020
5f3cea6
check gramma of import call
Kingwl Sep 22, 2020
5a1af04
Add more part of assertion
Kingwl Sep 22, 2020
4b51ca9
Add some case
Kingwl Sep 22, 2020
375b433
Add baseline
Kingwl Sep 22, 2020
41a881c
use module insted of target
Kingwl Sep 23, 2020
46a3eb1
strip assertion in d.ts
Kingwl Sep 23, 2020
a484ea2
Merge branch 'master' into import_assertion
Kingwl Sep 23, 2020
d64f7ba
Merge branch 'master' into import_assertion
Kingwl Nov 12, 2020
68c8c5d
Merge branch 'master' into import_assertion
Kingwl Dec 31, 2020
29fe0d3
Merge branch 'master' into import_assertion
Kingwl Jan 7, 2021
adcfd1b
Update new baseline
Kingwl Jan 7, 2021
fb01eb3
Merge branch 'master' into import_assertion
Kingwl Mar 8, 2021
4f22a60
accept baseline
Kingwl Mar 8, 2021
f5e594d
Revert error number changes
Kingwl Mar 9, 2021
60434d1
Update diagnostic message
Kingwl Mar 9, 2021
ff87d3e
Accept baseline
Kingwl Mar 9, 2021
43b67b9
Merge branch 'master' into import_assertion
Kingwl Mar 15, 2021
c69a05b
rename path
Kingwl Mar 15, 2021
d1c48b5
Fix cr issues
Kingwl Mar 15, 2021
d14f93a
Accept baseline
Kingwl Mar 15, 2021
aa8b856
Accept baseline
Kingwl Mar 15, 2021
7a5ec34
Merge branch 'master' into import_assertion
Kingwl Mar 18, 2021
9cea9cf
Error if assertion and typeonly import
Kingwl Mar 18, 2021
6337a7e
Merge branch 'main' into import_assertion
Kingwl Jun 17, 2021
eee2cab
Accept baseline
Kingwl Jun 17, 2021
2857d69
Merge branch 'main' into import_assertion
Kingwl Jul 9, 2021
88e39d5
Make lint happy
Kingwl Jul 9, 2021
f941d92
Merge branch 'main' into import_assertion
Kingwl Aug 13, 2021
106ff06
Add some comment
Kingwl Aug 13, 2021
7df4964
Merge branch 'main' into import_assertion
Kingwl Sep 14, 2021
6ea3c55
Fix cr issues
Kingwl Sep 14, 2021
fc51c42
Fix more issue
Kingwl Sep 14, 2021
bf7ca71
Incorporate PR feedback, fix module resolution for import()
rbuckton Sep 17, 2021
463138a
Merge branch 'main' into import_assertion
rbuckton Sep 17, 2021
b07c6e9
Add contextual type and completions for ImportCall options argument
rbuckton Sep 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compat/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ namespace ts {
exportClause: NamedExportBindings | undefined,
moduleSpecifier: Expression | undefined,
isTypeOnly: boolean) {
return factory.updateExportDeclaration(node, decorators, modifiers, isTypeOnly, exportClause, moduleSpecifier);
return factory.updateExportDeclaration(node, decorators, modifiers, isTypeOnly, exportClause, moduleSpecifier, /*assertClause*/ undefined);
}, factoryDeprecation);

/** @deprecated Use `factory.createJSDocParameterTag` or the factory supplied by your transformation context instead. */
Expand Down
46 changes: 37 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6130,7 +6130,7 @@ namespace ts {

function inlineExportModifiers(statements: Statement[]) {
// Pass 3: Move all `export {}`'s to `export` modifiers where possible
const index = findIndex(statements, d => isExportDeclaration(d) && !d.moduleSpecifier && !!d.exportClause && isNamedExports(d.exportClause));
const index = findIndex(statements, d => isExportDeclaration(d) && !d.moduleSpecifier && !d.assertClause && !!d.exportClause && isNamedExports(d.exportClause));
if (index >= 0) {
const exportDecl = statements[index] as ExportDeclaration & { readonly exportClause: NamedExports };
const replacements = mapDefined(exportDecl.exportClause.elements, e => {
Expand Down Expand Up @@ -6162,7 +6162,8 @@ namespace ts {
exportDecl.exportClause,
replacements
),
exportDecl.moduleSpecifier
exportDecl.moduleSpecifier,
exportDecl.assertClause
);
}
}
Expand Down Expand Up @@ -6798,15 +6799,17 @@ namespace ts {
// We use `target.parent || target` below as `target.parent` is unset when the target is a module which has been export assigned
// And then made into a default by the `esModuleInterop` or `allowSyntheticDefaultImports` flag
// In such cases, the `target` refers to the module itself already
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
/*assertClause*/ undefined
), ModifierFlags.None);
break;
case SyntaxKind.NamespaceImport:
addResult(factory.createImportDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
factory.createImportClause(/*isTypeOnly*/ false, /*importClause*/ undefined, factory.createNamespaceImport(factory.createIdentifier(localName))),
factory.createStringLiteral(getSpecifierForModuleSymbol(target, context))
factory.createStringLiteral(getSpecifierForModuleSymbol(target, context)),
/*assertClause*/ undefined
), ModifierFlags.None);
break;
case SyntaxKind.NamespaceExport:
Expand All @@ -6831,7 +6834,8 @@ namespace ts {
factory.createIdentifier(localName)
)
])),
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
/*assertClause*/ undefined
), ModifierFlags.None);
break;
case SyntaxKind.ExportSpecifier:
Expand Down Expand Up @@ -35927,11 +35931,19 @@ namespace ts {
}
}

function checkGrammarImportAssertion(declaration: ImportDeclaration | ExportDeclaration) {
const target = getEmitScriptTarget(compilerOptions);
if (target < ScriptTarget.ESNext && declaration.assertClause) {
grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_not_available_when_targeting_lower_than_esnext);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the current specification of Import Assertion, the import clause cannot affect the representation of the source text. Does it mean it can be safely ignored (when downlevel, drop the whole assert clause instead of emitting an error)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well. IMO, downlevel means something could map to another. Ignore is not a good idea.

But maybe we could control the error by module options rather than target.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import Assertions are an enrichment to the import statement, provides the runtime with extra information of which it can make a decision about. I could see the need to allow an error by default, but allow the error to be ignored and the node to be dropped from the emit.

The biggest use case is supporting JSON modules, and there are several runtimes that already support JSON modules without the assertion, but I can see people wanting to start writing the assertions in the code for the runtimes that will only support it with the assertion clause.

}
}

function checkImportDeclaration(node: ImportDeclaration) {
if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
return;
}
checkGrammarImportAssertion(node);
if (!checkGrammarDecoratorsAndModifiers(node) && hasEffectiveModifiers(node)) {
grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers);
}
Expand Down Expand Up @@ -36003,6 +36015,7 @@ namespace ts {
return;
}

checkGrammarImportAssertion(node);
if (!checkGrammarDecoratorsAndModifiers(node) && hasEffectiveModifiers(node)) {
grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
}
Expand Down Expand Up @@ -39860,6 +39873,22 @@ namespace ts {
return false;
}

function checkGrammarImportCallArguments(node: ImportCall, nodeArguments: NodeArray<Expression>): boolean {
const target = getEmitScriptTarget(compilerOptions);
if (target < ScriptTarget.ESNext) {
if (nodeArguments.length !== 1) {
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_have_one_specifier_as_an_argument);
}
checkGrammarForDisallowedTrailingComma(nodeArguments);
}
else {
if (nodeArguments.length !== 1 && nodeArguments.length !== 2) {
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_have_a_specifier_as_arguments_and_an_optional_assertion);
}
}
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);
Expand All @@ -39870,13 +39899,12 @@ namespace ts {
}

const nodeArguments = node.arguments;
if (nodeArguments.length !== 1) {
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_have_one_specifier_as_an_argument);
if (checkGrammarImportCallArguments(node, nodeArguments)) {
return true;
}
checkGrammarForDisallowedTrailingComma(nodeArguments);
// 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 (isSpreadElement(nodeArguments[0])) {
if (nodeArguments.length && isSpreadElement(nodeArguments[0])) {
return grammarErrorOnNode(nodeArguments[0], Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element);
}
return false;
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,10 @@
"category": "Error",
"code": 1391
},
"Dynamic import must have a specifier as arguments and an optional assertion": {
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
"category": "Error",
"code": 1392
},
"The types of '{0}' are incompatible between these types.": {
"category": "Error",
"code": 2200
Expand Down Expand Up @@ -3051,6 +3055,10 @@
"category": "Error",
"code": 2795
},
"Import assertions are not available when targeting lower than esnext.": {
"category": "Error",
"code": 2796
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
33 changes: 33 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,10 @@ namespace ts {
return emitImportDeclaration(<ImportDeclaration>node);
case SyntaxKind.ImportClause:
return emitImportClause(<ImportClause>node);
case SyntaxKind.AssertClause:
return emitAssertClause(<AssertClause>node);
case SyntaxKind.AssertEntry:
return emitAssertEntry(<AssertEntry>node);
case SyntaxKind.NamespaceImport:
return emitNamespaceImport(<NamespaceImport>node);
case SyntaxKind.NamespaceExport:
Expand Down Expand Up @@ -3182,9 +3186,34 @@ namespace ts {
writeSpace();
}
emitExpression(node.moduleSpecifier);
if (node.assertClause) {
writeSpace();
emit(node.assertClause);
}
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
writeTrailingSemicolon();
}

function emitAssertClause(node: AssertClause) {
emitTokenWithComment(SyntaxKind.AssertKeyword, node.pos, writeKeyword, node);
writeSpace();
const elements = node.elements;
emitNodeList(emitAssertEntry, node, elements, ListFormat.ImportClauseEntries);
}

function emitAssertEntry(node: AssertEntry) {
emit(node.name);
writePunctuation(":");
writeSpace();

const value = node.value;
/** @see emitPropertyAssignment */
if (emitTrailingCommentsOfPosition && (getEmitFlags(value) & EmitFlags.NoLeadingComments) === 0) {
const commentRange = getCommentRange(value);
emitTrailingCommentsOfPosition(commentRange.pos);
}
emit(value);
}

function emitImportClause(node: ImportClause) {
if (node.isTypeOnly) {
emitTokenWithComment(SyntaxKind.TypeKeyword, node.pos, writeKeyword, node);
Expand Down Expand Up @@ -3248,6 +3277,10 @@ namespace ts {
writeSpace();
emitExpression(node.moduleSpecifier);
}
if (node.assertClause) {
writeSpace();
emit(node.assertClause);
}
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
writeTrailingSemicolon();
}

Expand Down
62 changes: 54 additions & 8 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ namespace ts {
updateImportDeclaration,
createImportClause,
updateImportClause,
createAssertClause,
updateAssertClause,
createAssertEntry,
updateAssertEntry,
createNamespaceImport,
updateNamespaceImport,
createNamespaceExport,
Expand Down Expand Up @@ -3790,7 +3794,8 @@ namespace ts {
decorators: readonly Decorator[] | undefined,
modifiers: readonly Modifier[] | undefined,
importClause: ImportClause | undefined,
moduleSpecifier: Expression
moduleSpecifier: Expression,
assertClause: AssertClause | undefined
): ImportDeclaration {
const node = createBaseDeclaration<ImportDeclaration>(
SyntaxKind.ImportDeclaration,
Expand All @@ -3799,6 +3804,7 @@ namespace ts {
);
node.importClause = importClause;
node.moduleSpecifier = moduleSpecifier;
node.assertClause = assertClause;
node.transformFlags |=
propagateChildFlags(node.importClause) |
propagateChildFlags(node.moduleSpecifier);
Expand All @@ -3812,13 +3818,15 @@ namespace ts {
decorators: readonly Decorator[] | undefined,
modifiers: readonly Modifier[] | undefined,
importClause: ImportClause | undefined,
moduleSpecifier: Expression
moduleSpecifier: Expression,
assertClause: AssertClause | undefined
) {
return node.decorators !== decorators
|| node.modifiers !== modifiers
|| node.importClause !== importClause
|| node.moduleSpecifier !== moduleSpecifier
? update(createImportDeclaration(decorators, modifiers, importClause, moduleSpecifier), node)
|| node.assertClause !== assertClause
? update(createImportDeclaration(decorators, modifiers, importClause, moduleSpecifier, assertClause), node)
: node;
}

Expand Down Expand Up @@ -3847,6 +3855,40 @@ namespace ts {
: node;
}

// @api
function createAssertClause(elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause {
const node = createBaseNode<AssertClause>(SyntaxKind.AssertClause);
node.elements = elements;
node.multiLine = multiLine;
node.transformFlags |= TransformFlags.ContainsESNext;
return node;
}

// @api
function updateAssertClause(node: AssertClause, elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause {
return node.elements !== elements
|| node.multiLine !== multiLine
? update(createAssertClause(elements, multiLine), node)
: node;
}

// @api
function createAssertEntry(name: AssertionKey, value: StringLiteral): AssertEntry {
const node = createBaseNode<AssertEntry>(SyntaxKind.AssertEntry);
node.name = name;
node.value = value;
node.transformFlags |= TransformFlags.ContainsESNext;
return node;
}

// @api
function updateAssertEntry(node: AssertEntry, name: AssertionKey, value: StringLiteral): AssertEntry {
return node.name !== name
|| node.value !== value
? update(createAssertEntry(name, value), node)
: node;
}

// @api
function createNamespaceImport(name: Identifier): NamespaceImport {
const node = createBaseNode<NamespaceImport>(SyntaxKind.NamespaceImport);
Expand Down Expand Up @@ -3958,7 +4000,8 @@ namespace ts {
modifiers: readonly Modifier[] | undefined,
isTypeOnly: boolean,
exportClause: NamedExportBindings | undefined,
moduleSpecifier?: Expression
moduleSpecifier?: Expression,
assertClause?: AssertClause
) {
const node = createBaseDeclaration<ExportDeclaration>(
SyntaxKind.ExportDeclaration,
Expand All @@ -3968,6 +4011,7 @@ namespace ts {
node.isTypeOnly = isTypeOnly;
node.exportClause = exportClause;
node.moduleSpecifier = moduleSpecifier;
node.assertClause = assertClause;
node.transformFlags |=
propagateChildFlags(node.exportClause) |
propagateChildFlags(node.moduleSpecifier);
Expand All @@ -3982,14 +4026,16 @@ namespace ts {
modifiers: readonly Modifier[] | undefined,
isTypeOnly: boolean,
exportClause: NamedExportBindings | undefined,
moduleSpecifier: Expression | undefined
moduleSpecifier: Expression | undefined,
assertClause: AssertClause | undefined
) {
return node.decorators !== decorators
|| node.modifiers !== modifiers
|| node.isTypeOnly !== isTypeOnly
|| node.exportClause !== exportClause
|| node.moduleSpecifier !== moduleSpecifier
? update(createExportDeclaration(decorators, modifiers, isTypeOnly, exportClause, moduleSpecifier), node)
|| node.assertClause !== assertClause
? update(createExportDeclaration(decorators, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause), node)
: node;
}

Expand Down Expand Up @@ -5776,9 +5822,9 @@ namespace ts {
isEnumDeclaration(node) ? updateEnumDeclaration(node, node.decorators, modifiers, node.name, node.members) :
isModuleDeclaration(node) ? updateModuleDeclaration(node, node.decorators, modifiers, node.name, node.body) :
isImportEqualsDeclaration(node) ? updateImportEqualsDeclaration(node, node.decorators, modifiers, node.name, node.moduleReference) :
isImportDeclaration(node) ? updateImportDeclaration(node, node.decorators, modifiers, node.importClause, node.moduleSpecifier) :
isImportDeclaration(node) ? updateImportDeclaration(node, node.decorators, modifiers, node.importClause, node.moduleSpecifier, node.assertClause) :
isExportAssignment(node) ? updateExportAssignment(node, node.decorators, modifiers, node.expression) :
isExportDeclaration(node) ? updateExportDeclaration(node, node.decorators, modifiers, node.isTypeOnly, node.exportClause, node.moduleSpecifier) :
isExportDeclaration(node) ? updateExportDeclaration(node, node.decorators, modifiers, node.isTypeOnly, node.exportClause, node.moduleSpecifier, node.assertClause) :
Debug.assertNever(node);
}

Expand Down
12 changes: 12 additions & 0 deletions src/compiler/factory/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,18 @@ namespace ts {
return node.kind === SyntaxKind.ImportClause;
}

export function isAssertClause(node: Node): node is AssertClause {
return node.kind === SyntaxKind.AssertClause;
}

export function isAssertEntry(node: Node): node is AssertEntry {
return node.kind === SyntaxKind.AssertEntry;
}

export function isAssertionKey(node: Node): node is AssertionKey {
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
return isStringLiteral(node) || isIdentifier(node);
}

export function isNamespaceImport(node: Node): node is NamespaceImport {
return node.kind === SyntaxKind.NamespaceImport;
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ namespace ts {
/*decorators*/ undefined,
/*modifiers*/ undefined,
nodeFactory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, namedBindings),
nodeFactory.createStringLiteral(externalHelpersModuleNameText)
nodeFactory.createStringLiteral(externalHelpersModuleNameText),
/*assertClause*/ undefined
);
addEmitFlags(externalHelpersImportDeclaration, EmitFlags.NeverApplyImportHelper);
return externalHelpersImportDeclaration;
Expand Down
Loading