diff --git a/deno/ts_morph.d.ts b/deno/ts_morph.d.ts index f80e063f8..74040d813 100644 --- a/deno/ts_morph.d.ts +++ b/deno/ts_morph.d.ts @@ -10687,6 +10687,7 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec interface ExportSpecifierSpecificStructure extends KindedStructure { name: string; alias?: string; + isTypeOnly?: boolean; } export interface ImportDeclarationStructure extends Structure, ImportDeclarationSpecificStructure { @@ -10706,6 +10707,7 @@ export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpec interface ImportSpecifierSpecificStructure extends KindedStructure { name: string; + isTypeOnly?: boolean; alias?: string; } diff --git a/deno/ts_morph.js b/deno/ts_morph.js index e0fe16ced..a4368a3ed 100644 --- a/deno/ts_morph.js +++ b/deno/ts_morph.js @@ -8433,6 +8433,8 @@ class NamedImportExportSpecifierStructurePrinter extends NodePrinter { else if (structure instanceof Function) structure(specifierWriter); else { + if (structure.isTypeOnly) + writer.write("type "); specifierWriter.write(structure.name); if (!StringUtils.isNullOrWhitespace(structure.alias)) { if (!specifierWriter.isLastNewLine()) @@ -12167,6 +12169,8 @@ class ExportSpecifier extends ExportSpecifierBase { } set(structure) { callBaseSet(ExportSpecifierBase.prototype, this, structure); + if (structure.isTypeOnly != null) + this.setIsTypeOnly(structure.isTypeOnly); if (structure.name != null) this.setName(structure.name); if (structure.alias != null) @@ -12181,6 +12185,7 @@ class ExportSpecifier extends ExportSpecifierBase { kind: StructureKind.ExportSpecifier, alias: alias ? alias.getText() : undefined, name: this.getNameNode().getText(), + isTypeOnly: this.isTypeOnly(), }); } } @@ -12776,6 +12781,8 @@ class ImportSpecifier extends ImportSpecifierBase { } set(structure) { callBaseSet(ImportSpecifierBase.prototype, this, structure); + if (structure.isTypeOnly != null) + this.setIsTypeOnly(structure.isTypeOnly); if (structure.name != null) this.setName(structure.name); if (structure.alias != null) @@ -12790,6 +12797,7 @@ class ImportSpecifier extends ImportSpecifierBase { kind: StructureKind.ImportSpecifier, name: this.getName(), alias: alias ? alias.getText() : undefined, + isTypeOnly: this.isTypeOnly(), }); } } diff --git a/packages/ts-morph/lib/ts-morph.d.ts b/packages/ts-morph/lib/ts-morph.d.ts index 52bd921fc..bcb9c087d 100644 --- a/packages/ts-morph/lib/ts-morph.d.ts +++ b/packages/ts-morph/lib/ts-morph.d.ts @@ -10687,6 +10687,7 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec interface ExportSpecifierSpecificStructure extends KindedStructure { name: string; alias?: string; + isTypeOnly?: boolean; } export interface ImportDeclarationStructure extends Structure, ImportDeclarationSpecificStructure { @@ -10706,6 +10707,7 @@ export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpec interface ImportSpecifierSpecificStructure extends KindedStructure { name: string; + isTypeOnly?: boolean; alias?: string; } diff --git a/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts b/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts index 22b1cc53f..19498ab89 100644 --- a/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts +++ b/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts @@ -197,6 +197,9 @@ export class ExportSpecifier extends ExportSpecifierBase { set(structure: Partial) { callBaseSet(ExportSpecifierBase.prototype, this, structure); + if (structure.isTypeOnly != null) + this.setIsTypeOnly(structure.isTypeOnly); + if (structure.name != null) this.setName(structure.name); @@ -217,6 +220,7 @@ export class ExportSpecifier extends ExportSpecifierBase { kind: StructureKind.ExportSpecifier, alias: alias ? alias.getText() : undefined, name: this.getNameNode().getText(), + isTypeOnly: this.isTypeOnly(), }); } } diff --git a/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts b/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts index c948785aa..1d056a654 100644 --- a/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts +++ b/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts @@ -173,6 +173,9 @@ export class ImportSpecifier extends ImportSpecifierBase { set(structure: Partial) { callBaseSet(ImportSpecifierBase.prototype, this, structure); + if (structure.isTypeOnly != null) + this.setIsTypeOnly(structure.isTypeOnly); + if (structure.name != null) this.setName(structure.name); @@ -193,6 +196,7 @@ export class ImportSpecifier extends ImportSpecifierBase { kind: StructureKind.ImportSpecifier, name: this.getName(), alias: alias ? alias.getText() : undefined, + isTypeOnly: this.isTypeOnly(), }) as ImportSpecifierStructure; } } diff --git a/packages/ts-morph/src/structurePrinters/module/NamedImportExportSpecifierStructurePrinter.ts b/packages/ts-morph/src/structurePrinters/module/NamedImportExportSpecifierStructurePrinter.ts index 76b4437df..722c223d8 100644 --- a/packages/ts-morph/src/structurePrinters/module/NamedImportExportSpecifierStructurePrinter.ts +++ b/packages/ts-morph/src/structurePrinters/module/NamedImportExportSpecifierStructurePrinter.ts @@ -42,6 +42,8 @@ export class NamedImportExportSpecifierStructurePrinter extends NodePrinter { name: string; alias?: string; + isTypeOnly?: boolean; } diff --git a/packages/ts-morph/src/structures/module/ImportSpecifierStructure.ts b/packages/ts-morph/src/structures/module/ImportSpecifierStructure.ts index b7d9eff79..baa56f7e5 100644 --- a/packages/ts-morph/src/structures/module/ImportSpecifierStructure.ts +++ b/packages/ts-morph/src/structures/module/ImportSpecifierStructure.ts @@ -6,5 +6,6 @@ export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpec export interface ImportSpecifierSpecificStructure extends KindedStructure { name: string; + isTypeOnly?: boolean; alias?: string; } diff --git a/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts b/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts index 5adb6d3a3..e1910cf1f 100644 --- a/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts @@ -40,7 +40,11 @@ describe("ModuledNode", () => { { moduleSpecifier: "./test" }, { defaultImport: "identifier", moduleSpecifier: "./test" }, { defaultImport: "identifier", namespaceImport: "name", moduleSpecifier: "./test" }, - { defaultImport: "identifier", namedImports: ["name1", { name: "name" }, { name: "name", alias: "alias" }], moduleSpecifier: "./test" }, + { + defaultImport: "identifier", + namedImports: ["name1", { name: "name" }, { name: "name", alias: "alias", isTypeOnly: true }], + moduleSpecifier: "./test", + }, { namedImports: ["name"], moduleSpecifier: "./test" }, { namespaceImport: "name", moduleSpecifier: "./test" }, ], @@ -48,7 +52,7 @@ describe("ModuledNode", () => { `import "./test";`, `import identifier from "./test";`, `import identifier, * as name from "./test";`, - `import identifier, { name1, name, name as alias } from "./test";`, + `import identifier, { name1, name, type name as alias } from "./test";`, `import { name } from "./test";`, `import * as name from "./test";`, ].join("\n") + "\n", diff --git a/packages/ts-morph/src/tests/compiler/ast/module/exportDeclarationTests.ts b/packages/ts-morph/src/tests/compiler/ast/module/exportDeclarationTests.ts index dd21c3d86..4517bfedc 100644 --- a/packages/ts-morph/src/tests/compiler/ast/module/exportDeclarationTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/module/exportDeclarationTests.ts @@ -672,7 +672,7 @@ describe("ExportDeclaration", () => { kind: StructureKind.ExportDeclaration, isTypeOnly: false, moduleSpecifier: "./test", - namedExports: [{ kind: StructureKind.ExportSpecifier, alias: undefined, name: "name" }], + namedExports: [{ kind: StructureKind.ExportSpecifier, alias: undefined, name: "name", isTypeOnly: false }], namespaceExport: undefined, assertElements: undefined, }); diff --git a/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts b/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts index 44636115e..8e025cac0 100644 --- a/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts @@ -412,6 +412,15 @@ describe("ExportSpecifier", () => { "name", ); }); + + it("should set if type only", () => { + doTest( + `export { name as alias } from './file';`, + { isTypeOnly: true }, + `export { type name as alias } from './file';`, + "name", + ); + }); }); describe(nameof("getStructure"), () => { @@ -425,6 +434,7 @@ describe("ExportSpecifier", () => { kind: StructureKind.ExportSpecifier, alias: undefined, name: "name", + isTypeOnly: false, }); }); @@ -433,6 +443,16 @@ describe("ExportSpecifier", () => { kind: StructureKind.ExportSpecifier, alias: "alias", name: "name", + isTypeOnly: false, + }); + }); + + it("should get when has type only", () => { + doTest(`export { type a } from 'foo'`, { + kind: StructureKind.ExportSpecifier, + name: "a", + alias: undefined, + isTypeOnly: true, }); }); }); diff --git a/packages/ts-morph/src/tests/compiler/ast/module/importDeclarationTests.ts b/packages/ts-morph/src/tests/compiler/ast/module/importDeclarationTests.ts index 833e49aa3..7c4802a31 100644 --- a/packages/ts-morph/src/tests/compiler/ast/module/importDeclarationTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/module/importDeclarationTests.ts @@ -812,6 +812,7 @@ describe("ImportDeclaration", () => { kind: StructureKind.ImportSpecifier, name: "a", alias: undefined, + isTypeOnly: false, }], namespaceImport: undefined, assertElements: undefined, @@ -852,6 +853,7 @@ describe("ImportDeclaration", () => { kind: StructureKind.ImportSpecifier, name: "test", alias: undefined, + isTypeOnly: false, }], namespaceImport: undefined, assertElements: undefined, diff --git a/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts b/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts index ee6d1bec1..50af0b247 100644 --- a/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts @@ -264,6 +264,14 @@ describe("ImportSpecifier", () => { `import { name2 } from './file'; const t = alias;`, ); }); + + it("should set if type only", () => { + doTest( + `import { name as alias } from './file';`, + { isTypeOnly: true }, + `import { type name as alias } from './file';`, + ); + }); }); describe(nameof("getStructure"), () => { @@ -277,6 +285,7 @@ describe("ImportSpecifier", () => { kind: StructureKind.ImportSpecifier, name: "a", alias: undefined, + isTypeOnly: false, }); }); @@ -285,6 +294,16 @@ describe("ImportSpecifier", () => { kind: StructureKind.ImportSpecifier, name: "a", alias: "alias", + isTypeOnly: false, + }); + }); + + it("should get when has type only", () => { + doTest(`import { type a } from 'foo'`, { + kind: StructureKind.ImportSpecifier, + name: "a", + alias: undefined, + isTypeOnly: true, }); }); }); diff --git a/packages/ts-morph/src/tests/compiler/testHelpers/fillStructureWithDefaults.ts b/packages/ts-morph/src/tests/compiler/testHelpers/fillStructureWithDefaults.ts index ee3155ac1..191f6b436 100644 --- a/packages/ts-morph/src/tests/compiler/testHelpers/fillStructureWithDefaults.ts +++ b/packages/ts-morph/src/tests/compiler/testHelpers/fillStructureWithDefaults.ts @@ -84,6 +84,7 @@ export namespace fillStructures { export function importSpecifier(structure: OptionalKind): ImportSpecifierStructure { setIfNull(structure, "alias", undefined); + setIfNull(structure, "isTypeOnly", false); setIfNull(structure, "kind", StructureKind.ImportSpecifier); return structure as ImportSpecifierStructure; diff --git a/packages/ts-morph/src/tests/structurePrinters/module/namedImportExportSpecifierStructurePrinterTests.ts b/packages/ts-morph/src/tests/structurePrinters/module/namedImportExportSpecifierStructurePrinterTests.ts index d3f6f4ce3..ede221eb3 100644 --- a/packages/ts-morph/src/tests/structurePrinters/module/namedImportExportSpecifierStructurePrinterTests.ts +++ b/packages/ts-morph/src/tests/structurePrinters/module/namedImportExportSpecifierStructurePrinterTests.ts @@ -23,6 +23,10 @@ describe("NamedImportExportSpecifierStructurePrinter", () => { expect(writer.toString()).to.equal(expectedOutput); } + it("should write with type only", () => { + doTest({ name: "test", isTypeOnly: true }, "type test"); + }); + it("should write with alias", () => { doTest({ name: "test", alias: "alias" }, "test as alias"); });