Skip to content

Commit 6b88a0b

Browse files
authoredNov 19, 2022
fix: named import/export specifier structures were missing isTypeOnly (#1347)
1 parent f927d01 commit 6b88a0b

15 files changed

+77
-3
lines changed
 

‎deno/ts_morph.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10687,6 +10687,7 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec
1068710687
interface ExportSpecifierSpecificStructure extends KindedStructure<StructureKind.ExportSpecifier> {
1068810688
name: string;
1068910689
alias?: string;
10690+
isTypeOnly?: boolean;
1069010691
}
1069110692

1069210693
export interface ImportDeclarationStructure extends Structure, ImportDeclarationSpecificStructure {
@@ -10706,6 +10707,7 @@ export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpec
1070610707

1070710708
interface ImportSpecifierSpecificStructure extends KindedStructure<StructureKind.ImportSpecifier> {
1070810709
name: string;
10710+
isTypeOnly?: boolean;
1070910711
alias?: string;
1071010712
}
1071110713

‎deno/ts_morph.js

+8
Original file line numberDiff line numberDiff line change
@@ -8433,6 +8433,8 @@ class NamedImportExportSpecifierStructurePrinter extends NodePrinter {
84338433
else if (structure instanceof Function)
84348434
structure(specifierWriter);
84358435
else {
8436+
if (structure.isTypeOnly)
8437+
writer.write("type ");
84368438
specifierWriter.write(structure.name);
84378439
if (!StringUtils.isNullOrWhitespace(structure.alias)) {
84388440
if (!specifierWriter.isLastNewLine())
@@ -12167,6 +12169,8 @@ class ExportSpecifier extends ExportSpecifierBase {
1216712169
}
1216812170
set(structure) {
1216912171
callBaseSet(ExportSpecifierBase.prototype, this, structure);
12172+
if (structure.isTypeOnly != null)
12173+
this.setIsTypeOnly(structure.isTypeOnly);
1217012174
if (structure.name != null)
1217112175
this.setName(structure.name);
1217212176
if (structure.alias != null)
@@ -12181,6 +12185,7 @@ class ExportSpecifier extends ExportSpecifierBase {
1218112185
kind: StructureKind.ExportSpecifier,
1218212186
alias: alias ? alias.getText() : undefined,
1218312187
name: this.getNameNode().getText(),
12188+
isTypeOnly: this.isTypeOnly(),
1218412189
});
1218512190
}
1218612191
}
@@ -12776,6 +12781,8 @@ class ImportSpecifier extends ImportSpecifierBase {
1277612781
}
1277712782
set(structure) {
1277812783
callBaseSet(ImportSpecifierBase.prototype, this, structure);
12784+
if (structure.isTypeOnly != null)
12785+
this.setIsTypeOnly(structure.isTypeOnly);
1277912786
if (structure.name != null)
1278012787
this.setName(structure.name);
1278112788
if (structure.alias != null)
@@ -12790,6 +12797,7 @@ class ImportSpecifier extends ImportSpecifierBase {
1279012797
kind: StructureKind.ImportSpecifier,
1279112798
name: this.getName(),
1279212799
alias: alias ? alias.getText() : undefined,
12800+
isTypeOnly: this.isTypeOnly(),
1279312801
});
1279412802
}
1279512803
}

‎packages/ts-morph/lib/ts-morph.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10687,6 +10687,7 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec
1068710687
interface ExportSpecifierSpecificStructure extends KindedStructure<StructureKind.ExportSpecifier> {
1068810688
name: string;
1068910689
alias?: string;
10690+
isTypeOnly?: boolean;
1069010691
}
1069110692

1069210693
export interface ImportDeclarationStructure extends Structure, ImportDeclarationSpecificStructure {
@@ -10706,6 +10707,7 @@ export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpec
1070610707

1070710708
interface ImportSpecifierSpecificStructure extends KindedStructure<StructureKind.ImportSpecifier> {
1070810709
name: string;
10710+
isTypeOnly?: boolean;
1070910711
alias?: string;
1071010712
}
1071110713

‎packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts

+4
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
197197
set(structure: Partial<ExportSpecifierStructure>) {
198198
callBaseSet(ExportSpecifierBase.prototype, this, structure);
199199

200+
if (structure.isTypeOnly != null)
201+
this.setIsTypeOnly(structure.isTypeOnly);
202+
200203
if (structure.name != null)
201204
this.setName(structure.name);
202205

@@ -217,6 +220,7 @@ export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
217220
kind: StructureKind.ExportSpecifier,
218221
alias: alias ? alias.getText() : undefined,
219222
name: this.getNameNode().getText(),
223+
isTypeOnly: this.isTypeOnly(),
220224
});
221225
}
222226
}

‎packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts

+4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ export class ImportSpecifier extends ImportSpecifierBase<ts.ImportSpecifier> {
173173
set(structure: Partial<ImportSpecifierStructure>) {
174174
callBaseSet(ImportSpecifierBase.prototype, this, structure);
175175

176+
if (structure.isTypeOnly != null)
177+
this.setIsTypeOnly(structure.isTypeOnly);
178+
176179
if (structure.name != null)
177180
this.setName(structure.name);
178181

@@ -193,6 +196,7 @@ export class ImportSpecifier extends ImportSpecifierBase<ts.ImportSpecifier> {
193196
kind: StructureKind.ImportSpecifier,
194197
name: this.getName(),
195198
alias: alias ? alias.getText() : undefined,
199+
isTypeOnly: this.isTypeOnly(),
196200
}) as ImportSpecifierStructure;
197201
}
198202
}

‎packages/ts-morph/src/structurePrinters/module/NamedImportExportSpecifierStructurePrinter.ts

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export class NamedImportExportSpecifierStructurePrinter extends NodePrinter<Name
4242
else if (structure instanceof Function)
4343
structure(specifierWriter);
4444
else {
45+
if (structure.isTypeOnly)
46+
writer.write("type ");
4547
specifierWriter.write(structure.name);
4648

4749
if (!StringUtils.isNullOrWhitespace(structure.alias)) {

‎packages/ts-morph/src/structures/module/ExportSpecifierStructure.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec
77
export interface ExportSpecifierSpecificStructure extends KindedStructure<StructureKind.ExportSpecifier> {
88
name: string;
99
alias?: string;
10+
isTypeOnly?: boolean;
1011
}

‎packages/ts-morph/src/structures/module/ImportSpecifierStructure.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpec
66

77
export interface ImportSpecifierSpecificStructure extends KindedStructure<StructureKind.ImportSpecifier> {
88
name: string;
9+
isTypeOnly?: boolean;
910
alias?: string;
1011
}

‎packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ describe("ModuledNode", () => {
4040
{ moduleSpecifier: "./test" },
4141
{ defaultImport: "identifier", moduleSpecifier: "./test" },
4242
{ defaultImport: "identifier", namespaceImport: "name", moduleSpecifier: "./test" },
43-
{ defaultImport: "identifier", namedImports: ["name1", { name: "name" }, { name: "name", alias: "alias" }], moduleSpecifier: "./test" },
43+
{
44+
defaultImport: "identifier",
45+
namedImports: ["name1", { name: "name" }, { name: "name", alias: "alias", isTypeOnly: true }],
46+
moduleSpecifier: "./test",
47+
},
4448
{ namedImports: ["name"], moduleSpecifier: "./test" },
4549
{ namespaceImport: "name", moduleSpecifier: "./test" },
4650
],
4751
[
4852
`import "./test";`,
4953
`import identifier from "./test";`,
5054
`import identifier, * as name from "./test";`,
51-
`import identifier, { name1, name, name as alias } from "./test";`,
55+
`import identifier, { name1, name, type name as alias } from "./test";`,
5256
`import { name } from "./test";`,
5357
`import * as name from "./test";`,
5458
].join("\n") + "\n",

‎packages/ts-morph/src/tests/compiler/ast/module/exportDeclarationTests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ describe("ExportDeclaration", () => {
672672
kind: StructureKind.ExportDeclaration,
673673
isTypeOnly: false,
674674
moduleSpecifier: "./test",
675-
namedExports: [{ kind: StructureKind.ExportSpecifier, alias: undefined, name: "name" }],
675+
namedExports: [{ kind: StructureKind.ExportSpecifier, alias: undefined, name: "name", isTypeOnly: false }],
676676
namespaceExport: undefined,
677677
assertElements: undefined,
678678
});

‎packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts

+20
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,15 @@ describe("ExportSpecifier", () => {
412412
"name",
413413
);
414414
});
415+
416+
it("should set if type only", () => {
417+
doTest(
418+
`export { name as alias } from './file';`,
419+
{ isTypeOnly: true },
420+
`export { type name as alias } from './file';`,
421+
"name",
422+
);
423+
});
415424
});
416425

417426
describe(nameof<ExportSpecifier>("getStructure"), () => {
@@ -425,6 +434,7 @@ describe("ExportSpecifier", () => {
425434
kind: StructureKind.ExportSpecifier,
426435
alias: undefined,
427436
name: "name",
437+
isTypeOnly: false,
428438
});
429439
});
430440

@@ -433,6 +443,16 @@ describe("ExportSpecifier", () => {
433443
kind: StructureKind.ExportSpecifier,
434444
alias: "alias",
435445
name: "name",
446+
isTypeOnly: false,
447+
});
448+
});
449+
450+
it("should get when has type only", () => {
451+
doTest(`export { type a } from 'foo'`, {
452+
kind: StructureKind.ExportSpecifier,
453+
name: "a",
454+
alias: undefined,
455+
isTypeOnly: true,
436456
});
437457
});
438458
});

‎packages/ts-morph/src/tests/compiler/ast/module/importDeclarationTests.ts

+2
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ describe("ImportDeclaration", () => {
812812
kind: StructureKind.ImportSpecifier,
813813
name: "a",
814814
alias: undefined,
815+
isTypeOnly: false,
815816
}],
816817
namespaceImport: undefined,
817818
assertElements: undefined,
@@ -852,6 +853,7 @@ describe("ImportDeclaration", () => {
852853
kind: StructureKind.ImportSpecifier,
853854
name: "test",
854855
alias: undefined,
856+
isTypeOnly: false,
855857
}],
856858
namespaceImport: undefined,
857859
assertElements: undefined,

‎packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts

+19
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ describe("ImportSpecifier", () => {
264264
`import { name2 } from './file'; const t = alias;`,
265265
);
266266
});
267+
268+
it("should set if type only", () => {
269+
doTest(
270+
`import { name as alias } from './file';`,
271+
{ isTypeOnly: true },
272+
`import { type name as alias } from './file';`,
273+
);
274+
});
267275
});
268276

269277
describe(nameof<ImportSpecifier>("getStructure"), () => {
@@ -277,6 +285,7 @@ describe("ImportSpecifier", () => {
277285
kind: StructureKind.ImportSpecifier,
278286
name: "a",
279287
alias: undefined,
288+
isTypeOnly: false,
280289
});
281290
});
282291

@@ -285,6 +294,16 @@ describe("ImportSpecifier", () => {
285294
kind: StructureKind.ImportSpecifier,
286295
name: "a",
287296
alias: "alias",
297+
isTypeOnly: false,
298+
});
299+
});
300+
301+
it("should get when has type only", () => {
302+
doTest(`import { type a } from 'foo'`, {
303+
kind: StructureKind.ImportSpecifier,
304+
name: "a",
305+
alias: undefined,
306+
isTypeOnly: true,
288307
});
289308
});
290309
});

‎packages/ts-morph/src/tests/compiler/testHelpers/fillStructureWithDefaults.ts

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export namespace fillStructures {
8484

8585
export function importSpecifier(structure: OptionalKind<ImportSpecifierStructure>): ImportSpecifierStructure {
8686
setIfNull(structure, "alias", undefined);
87+
setIfNull(structure, "isTypeOnly", false);
8788

8889
setIfNull(structure, "kind", StructureKind.ImportSpecifier);
8990
return structure as ImportSpecifierStructure;

‎packages/ts-morph/src/tests/structurePrinters/module/namedImportExportSpecifierStructurePrinterTests.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ describe("NamedImportExportSpecifierStructurePrinter", () => {
2323
expect(writer.toString()).to.equal(expectedOutput);
2424
}
2525

26+
it("should write with type only", () => {
27+
doTest({ name: "test", isTypeOnly: true }, "type test");
28+
});
29+
2630
it("should write with alias", () => {
2731
doTest({ name: "test", alias: "alias" }, "test as alias");
2832
});

0 commit comments

Comments
 (0)
Please sign in to comment.