ImportAttributes should go through the same emit phases when in an ImportTypeNode (#56395)

This commit is contained in:
Titian Cernicova-Dragomir
2023-12-08 00:45:29 +00:00
committed by GitHub
parent 96bef671de
commit fd9aba66bf
4 changed files with 51 additions and 16 deletions
+15 -9
View File
@@ -223,6 +223,7 @@ import {
isGeneratedIdentifier,
isGeneratedPrivateIdentifier,
isIdentifier,
isImportAttributes,
isIncrementalCompilation,
isInJsonFile,
isInternalDeclaration,
@@ -1840,6 +1841,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.ImportTypeNodeAttributes) return emitImportTypeNodeAttributes(cast(node, isImportAttributes));
if (hint === EmitHint.EmbeddedStatement) {
Debug.assertNode(node, isEmptyStatement);
return emitEmptyStatement(/*isEmbeddedStatement*/ true);
@@ -2944,15 +2946,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.ImportTypeNodeAttributes, node.attributes);
}
writePunctuation(")");
if (node.qualifier) {
@@ -4077,6 +4071,18 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
writeTrailingSemicolon();
}
function emitImportTypeNodeAttributes(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();
+8 -7
View File
@@ -8143,13 +8143,14 @@ export const enum ExternalEmitHelpers {
// dprint-ignore
export const enum EmitHint {
SourceFile, // Emitting a SourceFile
Expression, // Emitting an Expression
IdentifierName, // Emitting an IdentifierName
MappedTypeParameter, // Emitting a TypeParameterDeclaration inside of a MappedTypeNode
Unspecified, // Emitting an otherwise unspecified node
EmbeddedStatement, // Emitting an embedded statement
JsxAttributeValue, // Emitting a JSX attribute value
SourceFile, // Emitting a SourceFile
Expression, // Emitting an Expression
IdentifierName, // Emitting an IdentifierName
MappedTypeParameter, // Emitting a TypeParameterDeclaration inside of a MappedTypeNode
Unspecified, // Emitting an otherwise unspecified node
EmbeddedStatement, // Emitting an embedded statement
JsxAttributeValue, // Emitting a JSX attribute value
ImportTypeNodeAttributes,// Emitting attributes as part of an ImportTypeNode
}
/** @internal */
+1
View File
@@ -7896,6 +7896,7 @@ declare namespace ts {
Unspecified = 4,
EmbeddedStatement = 5,
JsxAttributeValue = 6,
ImportTypeNodeAttributes = 7,
}
enum OuterExpressionKinds {
Parentheses = 1,
@@ -0,0 +1,27 @@
/// <reference path="fourslash.ts" />
// @module: NodeNext
// @traceResolution: true
// @filename: node_modules/inner/index.d.mts
//// export const esm = true;
// @filename: node_modules/inner/package.json
//// { "name": "inner", "exports": { "./mjs": "./index.mjs" } }
// @filename: foo.ts
//// export /*a*/function/*b*/ fn() {
//// return import("inner/mjs")
//// }
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Infer function return type",
actionName: "Infer function return type",
actionDescription: "Infer function return type",
newContent:
`export function fn(): Promise<typeof import("/tests/cases/fourslash/node_modules/inner/index", { with: { "resolution-mode": "import" } })> {
return import("inner/mjs")
}`
});