Skip to content

Commit fd9aba6

Browse files
ImportAttributes should go through the same emit phases when in an ImportTypeNode (#56395)
1 parent 96bef67 commit fd9aba6

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

src/compiler/emitter.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ import {
223223
isGeneratedIdentifier,
224224
isGeneratedPrivateIdentifier,
225225
isIdentifier,
226+
isImportAttributes,
226227
isIncrementalCompilation,
227228
isInJsonFile,
228229
isInternalDeclaration,
@@ -1840,6 +1841,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
18401841
if (hint === EmitHint.IdentifierName) return emitIdentifier(cast(node, isIdentifier));
18411842
if (hint === EmitHint.JsxAttributeValue) return emitLiteral(cast(node, isStringLiteral), /*jsxAttributeEscape*/ true);
18421843
if (hint === EmitHint.MappedTypeParameter) return emitMappedTypeParameter(cast(node, isTypeParameterDeclaration));
1844+
if (hint === EmitHint.ImportTypeNodeAttributes) return emitImportTypeNodeAttributes(cast(node, isImportAttributes));
18431845
if (hint === EmitHint.EmbeddedStatement) {
18441846
Debug.assertNode(node, isEmptyStatement);
18451847
return emitEmptyStatement(/*isEmbeddedStatement*/ true);
@@ -2944,15 +2946,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
29442946
if (node.attributes) {
29452947
writePunctuation(",");
29462948
writeSpace();
2947-
writePunctuation("{");
2948-
writeSpace();
2949-
writeKeyword(node.attributes.token === SyntaxKind.AssertKeyword ? "assert" : "with");
2950-
writePunctuation(":");
2951-
writeSpace();
2952-
const elements = node.attributes.elements;
2953-
emitList(node.attributes, elements, ListFormat.ImportAttributes);
2954-
writeSpace();
2955-
writePunctuation("}");
2949+
pipelineEmit(EmitHint.ImportTypeNodeAttributes, node.attributes);
29562950
}
29572951
writePunctuation(")");
29582952
if (node.qualifier) {
@@ -4077,6 +4071,18 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
40774071
writeTrailingSemicolon();
40784072
}
40794073

4074+
function emitImportTypeNodeAttributes(node: ImportAttributes) {
4075+
writePunctuation("{");
4076+
writeSpace();
4077+
writeKeyword(node.token === SyntaxKind.AssertKeyword ? "assert" : "with");
4078+
writePunctuation(":");
4079+
writeSpace();
4080+
const elements = node.elements;
4081+
emitList(node, elements, ListFormat.ImportAttributes);
4082+
writeSpace();
4083+
writePunctuation("}");
4084+
}
4085+
40804086
function emitImportAttributes(node: ImportAttributes) {
40814087
emitTokenWithComment(node.token, node.pos, writeKeyword, node);
40824088
writeSpace();

src/compiler/types.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -8143,13 +8143,14 @@ export const enum ExternalEmitHelpers {
81438143

81448144
// dprint-ignore
81458145
export const enum EmitHint {
8146-
SourceFile, // Emitting a SourceFile
8147-
Expression, // Emitting an Expression
8148-
IdentifierName, // Emitting an IdentifierName
8149-
MappedTypeParameter, // Emitting a TypeParameterDeclaration inside of a MappedTypeNode
8150-
Unspecified, // Emitting an otherwise unspecified node
8151-
EmbeddedStatement, // Emitting an embedded statement
8152-
JsxAttributeValue, // Emitting a JSX attribute value
8146+
SourceFile, // Emitting a SourceFile
8147+
Expression, // Emitting an Expression
8148+
IdentifierName, // Emitting an IdentifierName
8149+
MappedTypeParameter, // Emitting a TypeParameterDeclaration inside of a MappedTypeNode
8150+
Unspecified, // Emitting an otherwise unspecified node
8151+
EmbeddedStatement, // Emitting an embedded statement
8152+
JsxAttributeValue, // Emitting a JSX attribute value
8153+
ImportTypeNodeAttributes,// Emitting attributes as part of an ImportTypeNode
81538154
}
81548155

81558156
/** @internal */

tests/baselines/reference/api/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7896,6 +7896,7 @@ declare namespace ts {
78967896
Unspecified = 4,
78977897
EmbeddedStatement = 5,
78987898
JsxAttributeValue = 6,
7899+
ImportTypeNodeAttributes = 7,
78997900
}
79007901
enum OuterExpressionKinds {
79017902
Parentheses = 1,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: NodeNext
4+
// @traceResolution: true
5+
6+
// @filename: node_modules/inner/index.d.mts
7+
//// export const esm = true;
8+
9+
// @filename: node_modules/inner/package.json
10+
//// { "name": "inner", "exports": { "./mjs": "./index.mjs" } }
11+
12+
// @filename: foo.ts
13+
//// export /*a*/function/*b*/ fn() {
14+
//// return import("inner/mjs")
15+
//// }
16+
17+
goTo.select("a", "b");
18+
edit.applyRefactor({
19+
refactorName: "Infer function return type",
20+
actionName: "Infer function return type",
21+
actionDescription: "Infer function return type",
22+
newContent:
23+
`export function fn(): Promise<typeof import("/tests/cases/fourslash/node_modules/inner/index", { with: { "resolution-mode": "import" } })> {
24+
return import("inner/mjs")
25+
}`
26+
});
27+

0 commit comments

Comments
 (0)