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 15 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
47 changes: 37 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6352,7 +6352,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 @@ -6384,7 +6384,8 @@ namespace ts {
exportDecl.exportClause,
replacements
),
exportDecl.moduleSpecifier
exportDecl.moduleSpecifier,
exportDecl.assertClause
);
}
}
Expand Down Expand Up @@ -7029,7 +7030,8 @@ namespace ts {
propertyName && isIdentifier(propertyName) ? factory.createIdentifier(idText(propertyName)) : undefined,
factory.createIdentifier(localName)
)])),
factory.createStringLiteral(specifier)
factory.createStringLiteral(specifier),
/*importClause*/ undefined
), ModifierFlags.None);
break;
}
Expand Down Expand Up @@ -7105,15 +7107,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 @@ -7138,7 +7142,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 @@ -37344,11 +37349,18 @@ namespace ts {
}
}

function checkGrammarImportAssertion(declaration: ImportDeclaration | ExportDeclaration) {
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
if (declaration.assertClause && moduleKind !== ModuleKind.ESNext) {
grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_only_supported_when_the_module_flag_is_set_to_esnext);
}
}

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);
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
if (!checkGrammarDecoratorsAndModifiers(node) && hasEffectiveModifiers(node)) {
grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers);
}
Expand Down Expand Up @@ -37423,6 +37435,7 @@ namespace ts {
return;
}

checkGrammarImportAssertion(node);
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
if (!checkGrammarDecoratorsAndModifiers(node) && hasEffectiveModifiers(node)) {
grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
}
Expand Down Expand Up @@ -41313,6 +41326,21 @@ namespace ts {
return false;
}

function checkGrammarImportCallArguments(node: ImportCall, nodeArguments: NodeArray<Expression>): boolean {
if (moduleKind !== ModuleKind.ESNext) {
if (nodeArguments.length !== 1) {
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_must_have_one_specifier_as_an_argument);
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
}
checkGrammarForDisallowedTrailingComma(nodeArguments);
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -41323,13 +41351,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
24 changes: 16 additions & 8 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1352,14 +1352,18 @@
"category": "Message",
"code": 1430
},
"'for await' loops are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.": {
"Dynamic import must have a specifier as arguments and an optional assertion": {
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
"category": "Error",
"code": 1431
},
"Top-level 'for await' loops are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.": {
"'for await' loops are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.": {
"category": "Error",
"code": 1432
},
"Top-level 'for await' loops are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.": {
"category": "Error",
"code": 1433
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down Expand Up @@ -3240,30 +3244,34 @@
"category": "Error",
"code": 2796
},
"A mixin class that extends from a type variable containing an abstract construct signature must also be declared 'abstract'.": {
"Import assertions are only supported when the '--module' flag is set to 'esnext'.": {
"category": "Error",
"code": 2797
},
"The declaration was marked as deprecated here.": {
"A mixin class that extends from a type variable containing an abstract construct signature must also be declared 'abstract'.": {
"category": "Error",
"code": 2798
},
"Type produces a tuple type that is too large to represent.": {
"The declaration was marked as deprecated here.": {
"category": "Error",
"code": 2799
},
"Expression produces a tuple type that is too large to represent.": {
"Type produces a tuple type that is too large to represent.": {
"category": "Error",
"code": 2800
},
"This condition will always return true since the Promise is always truthy.": {
"Expression produces a tuple type that is too large to represent.": {
"category": "Error",
"code": 2801
},
"Type '{0}' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.": {
"This condition will always return true since the Promise is always truthy.": {
"category": "Error",
"code": 2802
},
"Type '{0}' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.": {
"category": "Error",
"code": 2803
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
51 changes: 49 additions & 2 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,10 @@ namespace ts {
return emitExportAssignment(<ExportAssignment>node);
case SyntaxKind.ExportDeclaration:
return emitExportDeclaration(<ExportDeclaration>node);
case SyntaxKind.AssertClause:
return emitAssertClause(<AssertClause>node);
case SyntaxKind.AssertEntry:
return emitAssertEntry(<AssertEntry>node);
case SyntaxKind.NamedExports:
return emitNamedExports(<NamedExports>node);
case SyntaxKind.NamespaceExport:
Expand Down Expand Up @@ -3212,6 +3216,10 @@ namespace ts {
writeSpace();
}
emit(node.moduleSpecifier);
if (node.assertClause) {
writeSpace();
emit(node.assertClause);
}
Kingwl marked this conversation as resolved.
Show resolved Hide resolved
writeTrailingSemicolon();
}

Expand Down Expand Up @@ -3278,9 +3286,34 @@ namespace ts {
writeSpace();
emit(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;
emitList(node, elements, ListFormat.ImportClauseEntries);
}

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

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

function emitNamespaceExportDeclaration(node: NamespaceExportDeclaration) {
let nextPos = emitTokenWithComment(SyntaxKind.ExportKeyword, node.pos, writeKeyword, node);
writeSpace();
Expand Down Expand Up @@ -6194,7 +6227,8 @@ namespace ts {
visitList(node.decorators, isDecorator),
visitList(node.modifiers, isModifier),
visit(node.importClause, isImportClause),
visitExpression(node.moduleSpecifier));
visitExpression(node.moduleSpecifier),
visit(node.assertClause, isAssertClause));

case SyntaxKind.ImportClause:
Debug.type<ImportClause>(node);
Expand Down Expand Up @@ -6238,7 +6272,20 @@ namespace ts {
visitList(node.modifiers, isModifier),
node.isTypeOnly,
visit(node.exportClause, isNamedExportBindings),
visitExpression(node.moduleSpecifier));
visitExpression(node.moduleSpecifier),
visit(node.assertClause, isAssertClause));

case SyntaxKind.AssertClause:
Debug.type<AssertClause>(node);
return factory.updateAssertClause(node,
visitList(node.elements, isAssertEntry),
node.multiLine);

case SyntaxKind.AssertEntry:
Debug.type<AssertEntry>(node);
return factory.updateAssertEntry(node,
visit(node.name, isAssertionKey),
visit(node.value, isStringLiteral));

case SyntaxKind.NamedExports:
Debug.type<NamedExports>(node);
Expand Down
Loading