Skip to content

Commit 5a1af04

Browse files
committed
Add more part of assertion
1 parent 5f3cea6 commit 5a1af04

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35931,11 +35931,19 @@ namespace ts {
3593135931
}
3593235932
}
3593335933

35934+
function checkGrammarImportAssertion(declaration: ImportDeclaration | ExportDeclaration) {
35935+
const target = getEmitScriptTarget(compilerOptions);
35936+
if (target < ScriptTarget.ESNext && declaration.assertClause) {
35937+
grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_not_available_when_targeting_lower_than_esnext);
35938+
}
35939+
}
35940+
3593435941
function checkImportDeclaration(node: ImportDeclaration) {
3593535942
if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
3593635943
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
3593735944
return;
3593835945
}
35946+
checkGrammarImportAssertion(node);
3593935947
if (!checkGrammarDecoratorsAndModifiers(node) && hasEffectiveModifiers(node)) {
3594035948
grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers);
3594135949
}
@@ -36007,6 +36015,7 @@ namespace ts {
3600736015
return;
3600836016
}
3600936017

36018+
checkGrammarImportAssertion(node);
3601036019
if (!checkGrammarDecoratorsAndModifiers(node) && hasEffectiveModifiers(node)) {
3601136020
grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
3601236021
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,6 +3055,10 @@
30553055
"category": "Error",
30563056
"code": 2795
30573057
},
3058+
"Import assertions are not available when targeting lower than esnext.": {
3059+
"category": "Error",
3060+
"code": 2796
3061+
},
30583062

30593063
"Import declaration '{0}' is using private name '{1}'.": {
30603064
"category": "Error",

src/compiler/emitter.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,10 @@ namespace ts {
14721472
return emitImportDeclaration(<ImportDeclaration>node);
14731473
case SyntaxKind.ImportClause:
14741474
return emitImportClause(<ImportClause>node);
1475+
case SyntaxKind.AssertClause:
1476+
return emitAssertClause(<AssertClause>node);
1477+
case SyntaxKind.AssertEntry:
1478+
return emitAssertEntry(<AssertEntry>node);
14751479
case SyntaxKind.NamespaceImport:
14761480
return emitNamespaceImport(<NamespaceImport>node);
14771481
case SyntaxKind.NamespaceExport:
@@ -3182,9 +3186,32 @@ namespace ts {
31823186
writeSpace();
31833187
}
31843188
emitExpression(node.moduleSpecifier);
3189+
if (node.assertClause) {
3190+
writeSpace();
3191+
emit(node.assertClause)
3192+
}
31853193
writeTrailingSemicolon();
31863194
}
31873195

3196+
function emitAssertClause(node: AssertClause) {
3197+
const elements = node.elements;
3198+
emitExpressionList(node, elements, ListFormat.ImportClauseEntries);
3199+
}
3200+
3201+
function emitAssertEntry(node: AssertEntry) {
3202+
emit(node.name);
3203+
writePunctuation(":");
3204+
writeSpace();
3205+
3206+
const value = node.value;
3207+
/** @see emitPropertyAssignment */
3208+
if (emitTrailingCommentsOfPosition && (getEmitFlags(value) & EmitFlags.NoLeadingComments) === 0) {
3209+
const commentRange = getCommentRange(value);
3210+
emitTrailingCommentsOfPosition(commentRange.pos);
3211+
}
3212+
emit(value);
3213+
}
3214+
31883215
function emitImportClause(node: ImportClause) {
31893216
if (node.isTypeOnly) {
31903217
emitTokenWithComment(SyntaxKind.TypeKeyword, node.pos, writeKeyword, node);

src/compiler/factory/nodeTests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ namespace ts {
525525
return node.kind === SyntaxKind.AssertEntry;
526526
}
527527

528+
export function isAssertionKey(node: Node): node is AssertionKey {
529+
return isStringLiteral(node) || isIdentifier(node);
530+
}
531+
528532
export function isNamespaceImport(node: Node): node is NamespaceImport {
529533
return node.kind === SyntaxKind.NamespaceImport;
530534
}

src/compiler/parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ namespace ts {
398398
case SyntaxKind.ImportClause:
399399
return visitNode(cbNode, (<ImportClause>node).name) ||
400400
visitNode(cbNode, (<ImportClause>node).namedBindings);
401+
case SyntaxKind.AssertClause:
402+
return visitNodes(cbNode, cbNodes, (<AssertClause>node).elements);
403+
case SyntaxKind.AssertEntry:
404+
return visitNode(cbNode, (<AssertEntry>node).name) ||
405+
visitNode(cbNode, (<AssertEntry>node).value);
401406
case SyntaxKind.NamespaceExportDeclaration:
402407
return visitNode(cbNode, (<NamespaceExportDeclaration>node).name);
403408

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7984,6 +7984,7 @@ namespace ts {
79847984
ObjectBindingPatternElements = SingleLine | AllowTrailingComma | SpaceBetweenBraces | CommaDelimited | SpaceBetweenSiblings | NoSpaceIfEmpty,
79857985
ArrayBindingPatternElements = SingleLine | AllowTrailingComma | CommaDelimited | SpaceBetweenSiblings | NoSpaceIfEmpty,
79867986
ObjectLiteralExpressionProperties = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces | NoSpaceIfEmpty,
7987+
ImportClauseEntries = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces | NoSpaceIfEmpty,
79877988
ArrayLiteralExpressionElements = PreserveLines | CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | Indented | SquareBrackets,
79887989
CommaListElements = CommaDelimited | SpaceBetweenSiblings | SingleLine,
79897990
CallExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis,

src/compiler/visitorPublic.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,15 @@ namespace ts {
944944
nodeVisitor((<ImportClause>node).name, visitor, isIdentifier),
945945
nodeVisitor((<ImportClause>node).namedBindings, visitor, isNamedImportBindings));
946946

947+
case SyntaxKind.AssertClause:
948+
return factory.updateAssertClause(<AssertClause>node,
949+
nodesVisitor((node as AssertClause).elements, visitor, isAssertEntry));
950+
951+
case SyntaxKind.AssertEntry:
952+
return factory.updateAssertEntry(<AssertEntry>node,
953+
nodeVisitor((<AssertEntry>node).name, visitor, isAssertionKey),
954+
nodeVisitor((<AssertEntry>node).value, visitor, isStringLiteral));
955+
947956
case SyntaxKind.NamespaceImport:
948957
return factory.updateNamespaceImport(<NamespaceImport>node,
949958
nodeVisitor((<NamespaceImport>node).name, visitor, isIdentifier));

0 commit comments

Comments
 (0)