Skip to content

Commit

Permalink
ImportAttributes should go through the same emit phases even when the…
Browse files Browse the repository at this point in the history
…y are part of an ImportTypeNode.
  • Loading branch information
dragomirtitian committed Nov 14, 2023
1 parent b970fa4 commit 53db3dd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
if (hint === EmitHint.IdentifierName) return emitIdentifier(cast(node, isIdentifier));
if (hint === EmitHint.JsxAttributeValue) return emitLiteral(cast(node, isStringLiteral), /*jsxAttributeEscape*/ true);
if (hint === EmitHint.MappedTypeParameter) return emitMappedTypeParameter(cast(node, isTypeParameterDeclaration));
if (hint === EmitHint.TypeImportAttributes) return emitTypeImportAttributes(cast(node, ts.isImportAttributes));
if (hint === EmitHint.EmbeddedStatement) {
Debug.assertNode(node, isEmptyStatement);
return emitEmptyStatement(/*isEmbeddedStatement*/ true);
Expand Down Expand Up @@ -2944,15 +2945,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
if (node.attributes) {
writePunctuation(",");
writeSpace();
writePunctuation("{");
writeSpace();
writeKeyword(node.attributes.token === SyntaxKind.AssertKeyword ? "assert" : "with");
writePunctuation(":");
writeSpace();
const elements = node.attributes.elements;
emitList(node.attributes, elements, ListFormat.ImportAttributes);
writeSpace();
writePunctuation("}");
pipelineEmit(EmitHint.TypeImportAttributes, node.attributes);
}
writePunctuation(")");
if (node.qualifier) {
Expand Down Expand Up @@ -4077,6 +4070,18 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
writeTrailingSemicolon();
}

function emitTypeImportAttributes(node: ImportAttributes) {
writePunctuation("{");
writeSpace();
writeKeyword(node.token === SyntaxKind.AssertKeyword ? "assert" : "with");
writePunctuation(":");
writeSpace();
const elements = node.elements;
emitList(node, elements, ListFormat.ImportAttributes);
writeSpace();
writePunctuation("}");
}

function emitImportAttributes(node: ImportAttributes) {
emitTokenWithComment(node.token, node.pos, writeKeyword, node);
writeSpace();
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8148,6 +8148,7 @@ export const enum EmitHint {
Unspecified, // Emitting an otherwise unspecified node
EmbeddedStatement, // Emitting an embedded statement
JsxAttributeValue, // Emitting a JSX attribute value
TypeImportAttributes,// Emitting attributes as part of an ImportTypeNode
}

/** @internal */
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7892,6 +7892,7 @@ declare namespace ts {
Unspecified = 4,
EmbeddedStatement = 5,
JsxAttributeValue = 6,
TypeImportAttributes = 7,
}
enum OuterExpressionKinds {
Parentheses = 1,
Expand Down

0 comments on commit 53db3dd

Please sign in to comment.