Skip to content

Commit

Permalink
fix: named import/export specifier structures were missing isTypeOnly (
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Nov 19, 2022
1 parent f927d01 commit 6b88a0b
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 3 deletions.
2 changes: 2 additions & 0 deletions deno/ts_morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10687,6 +10687,7 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec
interface ExportSpecifierSpecificStructure extends KindedStructure<StructureKind.ExportSpecifier> {
name: string;
alias?: string;
isTypeOnly?: boolean;
}

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

interface ImportSpecifierSpecificStructure extends KindedStructure<StructureKind.ImportSpecifier> {
name: string;
isTypeOnly?: boolean;
alias?: string;
}

Expand Down
8 changes: 8 additions & 0 deletions deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -12181,6 +12185,7 @@ class ExportSpecifier extends ExportSpecifierBase {
kind: StructureKind.ExportSpecifier,
alias: alias ? alias.getText() : undefined,
name: this.getNameNode().getText(),
isTypeOnly: this.isTypeOnly(),
});
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -12790,6 +12797,7 @@ class ImportSpecifier extends ImportSpecifierBase {
kind: StructureKind.ImportSpecifier,
name: this.getName(),
alias: alias ? alias.getText() : undefined,
isTypeOnly: this.isTypeOnly(),
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10687,6 +10687,7 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec
interface ExportSpecifierSpecificStructure extends KindedStructure<StructureKind.ExportSpecifier> {
name: string;
alias?: string;
isTypeOnly?: boolean;
}

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

interface ImportSpecifierSpecificStructure extends KindedStructure<StructureKind.ImportSpecifier> {
name: string;
isTypeOnly?: boolean;
alias?: string;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
set(structure: Partial<ExportSpecifierStructure>) {
callBaseSet(ExportSpecifierBase.prototype, this, structure);

if (structure.isTypeOnly != null)
this.setIsTypeOnly(structure.isTypeOnly);

if (structure.name != null)
this.setName(structure.name);

Expand All @@ -217,6 +220,7 @@ export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
kind: StructureKind.ExportSpecifier,
alias: alias ? alias.getText() : undefined,
name: this.getNameNode().getText(),
isTypeOnly: this.isTypeOnly(),
});
}
}
4 changes: 4 additions & 0 deletions packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ export class ImportSpecifier extends ImportSpecifierBase<ts.ImportSpecifier> {
set(structure: Partial<ImportSpecifierStructure>) {
callBaseSet(ImportSpecifierBase.prototype, this, structure);

if (structure.isTypeOnly != null)
this.setIsTypeOnly(structure.isTypeOnly);

if (structure.name != null)
this.setName(structure.name);

Expand All @@ -193,6 +196,7 @@ export class ImportSpecifier extends ImportSpecifierBase<ts.ImportSpecifier> {
kind: StructureKind.ImportSpecifier,
name: this.getName(),
alias: alias ? alias.getText() : undefined,
isTypeOnly: this.isTypeOnly(),
}) as ImportSpecifierStructure;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class NamedImportExportSpecifierStructurePrinter extends NodePrinter<Name
else if (structure instanceof Function)
structure(specifierWriter);
else {
if (structure.isTypeOnly)
writer.write("type ");
specifierWriter.write(structure.name);

if (!StringUtils.isNullOrWhitespace(structure.alias)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpec
export interface ExportSpecifierSpecificStructure extends KindedStructure<StructureKind.ExportSpecifier> {
name: string;
alias?: string;
isTypeOnly?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpec

export interface ImportSpecifierSpecificStructure extends KindedStructure<StructureKind.ImportSpecifier> {
name: string;
isTypeOnly?: boolean;
alias?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ 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" },
],
[
`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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExportSpecifier>("getStructure"), () => {
Expand All @@ -425,6 +434,7 @@ describe("ExportSpecifier", () => {
kind: StructureKind.ExportSpecifier,
alias: undefined,
name: "name",
isTypeOnly: false,
});
});

Expand All @@ -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,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ describe("ImportDeclaration", () => {
kind: StructureKind.ImportSpecifier,
name: "a",
alias: undefined,
isTypeOnly: false,
}],
namespaceImport: undefined,
assertElements: undefined,
Expand Down Expand Up @@ -852,6 +853,7 @@ describe("ImportDeclaration", () => {
kind: StructureKind.ImportSpecifier,
name: "test",
alias: undefined,
isTypeOnly: false,
}],
namespaceImport: undefined,
assertElements: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImportSpecifier>("getStructure"), () => {
Expand All @@ -277,6 +285,7 @@ describe("ImportSpecifier", () => {
kind: StructureKind.ImportSpecifier,
name: "a",
alias: undefined,
isTypeOnly: false,
});
});

Expand All @@ -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,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export namespace fillStructures {

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

setIfNull(structure, "kind", StructureKind.ImportSpecifier);
return structure as ImportSpecifierStructure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down

0 comments on commit 6b88a0b

Please sign in to comment.