Skip to content

Commit a61b9ae

Browse files
ImportAttributes should go through the same emit phases even when they are part of an ImportTypeNode.
1 parent b970fa4 commit a61b9ae

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

src/compiler/emitter.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
18401840
if (hint === EmitHint.IdentifierName) return emitIdentifier(cast(node, isIdentifier));
18411841
if (hint === EmitHint.JsxAttributeValue) return emitLiteral(cast(node, isStringLiteral), /*jsxAttributeEscape*/ true);
18421842
if (hint === EmitHint.MappedTypeParameter) return emitMappedTypeParameter(cast(node, isTypeParameterDeclaration));
1843+
if (hint === EmitHint.TypeImportAttributes) return emitTypeImportAttributes(cast(node, ts.isImportAttributes));
18431844
if (hint === EmitHint.EmbeddedStatement) {
18441845
Debug.assertNode(node, isEmptyStatement);
18451846
return emitEmptyStatement(/*isEmbeddedStatement*/ true);
@@ -2944,15 +2945,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
29442945
if (node.attributes) {
29452946
writePunctuation(",");
29462947
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("}");
2948+
pipelineEmit(EmitHint.TypeImportAttributes, node.attributes);
29562949
}
29572950
writePunctuation(")");
29582951
if (node.qualifier) {
@@ -4077,6 +4070,18 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
40774070
writeTrailingSemicolon();
40784071
}
40794072

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

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8148,6 +8148,7 @@ export const enum EmitHint {
81488148
Unspecified, // Emitting an otherwise unspecified node
81498149
EmbeddedStatement, // Emitting an embedded statement
81508150
JsxAttributeValue, // Emitting a JSX attribute value
8151+
TypeImportAttributes,// Emitting attributes as part of an ImportTypeNode
81518152
}
81528153

81538154
/** @internal */

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

+1
Original file line numberDiff line numberDiff line change
@@ -7892,6 +7892,7 @@ declare namespace ts {
78927892
Unspecified = 4,
78937893
EmbeddedStatement = 5,
78947894
JsxAttributeValue = 6,
7895+
TypeImportAttributes = 7,
78957896
}
78967897
enum OuterExpressionKinds {
78977898
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)