ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
125 lines (123 loc) • 7.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const main_1 = require("./../../../main");
const testHelpers_1 = require("./../testHelpers");
describe("ExportSpecifier", () => {
describe("getName", () => {
function doTest(text, name) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
const namedExport = firstChild.getNamedExports()[0];
chai_1.expect(namedExport.getName().getText()).to.equal(name);
}
it("should get the name when there is no alias", () => {
doTest(`export {name} from "./test";`, "name");
});
it("should get the name when there is an alias", () => {
doTest(`export {name as alias} from "./test";`, "name");
});
it("should get the identifier when it's a default keyword", () => {
doTest(`export {default as alias} from "./test";`, "default");
});
});
describe("setName", () => {
it("should change what's imported, but not change anything in the other files", () => {
const ast = new main_1.default();
const myClassFile = ast.addSourceFileFromStructure("MyClass.ts", {
classes: [{ name: "MyClass", isExported: true }]
});
const exportsFile = ast.addSourceFileFromStructure("Exports.ts", {
exports: [{ namedExports: [{ name: "MyClass" }], moduleSpecifier: "./MyClass" }]
});
const mainFile = ast.addSourceFileFromText("Main.ts", `import {MyClass} from "./Exports";\n\nconst t = MyClass;\n`);
exportsFile.getExports()[0].getNamedExports()[0].setName("MyNewName");
chai_1.expect(myClassFile.getFullText()).to.equal(`export class MyClass {\n}\n`);
chai_1.expect(exportsFile.getFullText()).to.equal(`export {MyNewName} from "./MyClass";\n`);
chai_1.expect(mainFile.getFullText()).to.equal(`import {MyClass} from "./Exports";\n\nconst t = MyClass;\n`);
});
it("should change it when there's an alias", () => {
const ast = new main_1.default();
const exportsFile = ast.addSourceFileFromStructure("Exports.ts", {
exports: [{ namedExports: [{ name: "MyClass", alias: "MyAlias" }], moduleSpecifier: "./MyClass" }]
});
exportsFile.getExports()[0].getNamedExports()[0].setName("MyNewName");
chai_1.expect(exportsFile.getFullText()).to.equal(`export {MyNewName as MyAlias} from "./MyClass";\n`);
});
it("should rename in current file if exporting from current file", () => {
const ast = new main_1.default();
const myClassFile = ast.addSourceFileFromStructure("MyClass.ts", {
classes: [{ name: "MyClass" }],
exports: [{ namedExports: [{ name: "MyClass" }] }]
});
myClassFile.getExports()[0].getNamedExports()[0].setName("Identifier");
chai_1.expect(myClassFile.getFullText()).to.equal(`class MyClass {\n}\n\nexport {Identifier};\n`);
});
});
describe("renameName", () => {
it("should rename in current file if exporting from current file", () => {
const ast = new main_1.default();
const myClassFile = ast.addSourceFileFromStructure("MyClass.ts", {
classes: [{ name: "MyClass" }],
exports: [{ namedExports: [{ name: "MyClass" }] }]
});
myClassFile.getExports()[0].getNamedExports()[0].renameName("Identifier");
chai_1.expect(myClassFile.getFullText()).to.equal(`class Identifier {\n}\n\nexport {Identifier};\n`);
});
});
describe("getAlias", () => {
function doTest(text, alias) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
const namedExport = firstChild.getNamedExports()[0];
if (alias == null)
chai_1.expect(namedExport.getAlias()).to.equal(undefined);
else
chai_1.expect(namedExport.getAlias().getText()).to.equal(alias);
}
it("should be undefined there is no alias", () => {
doTest(`export {name} from "./test";`, undefined);
});
it("should get the alias when there is an alias", () => {
doTest(`export {name as alias} from "./test";`, "alias");
});
it("should get the alias when there is a default keyword", () => {
doTest(`export {default as alias} from "./test";`, "alias");
});
});
describe("setAlias", () => {
it("should rename existing alias", () => {
const ast = new main_1.default();
const myClassFile = ast.addSourceFileFromStructure("MyClass.ts", {
classes: [{ name: "MyClass", isExported: true }]
});
const exportsFile = ast.addSourceFileFromStructure("Exports.ts", {
exports: [{ namedExports: [{ name: "MyClass", alias: "MyAlias" }], moduleSpecifier: "./MyClass" }]
});
const mainFile = ast.addSourceFileFromText("Main.ts", `import {MyAlias} from "./Exports";\n\nconst t = MyAlias;\n`);
exportsFile.getExports()[0].getNamedExports()[0].setAlias("MyNewAlias");
chai_1.expect(exportsFile.getFullText()).to.equal(`export {MyClass as MyNewAlias} from "./MyClass";\n`);
chai_1.expect(mainFile.getFullText()).to.equal(`import {MyNewAlias} from "./Exports";\n\nconst t = MyNewAlias;\n`);
});
it("should add new alias and update all usages to the new alias", () => {
const ast = new main_1.default();
const myClassFile = ast.addSourceFileFromStructure("MyClass.ts", {
classes: [{ name: "MyClass", isExported: true }]
});
const exportsFile = ast.addSourceFileFromStructure("Exports.ts", {
exports: [{ namedExports: [{ name: "MyClass" }], moduleSpecifier: "./MyClass" }]
});
const mainFile = ast.addSourceFileFromText("Main.ts", `import {MyClass} from "./Exports";\n\nconst t = MyClass;\n`);
exportsFile.getExports()[0].getNamedExports()[0].setAlias("MyNewAlias");
chai_1.expect(myClassFile.getFullText()).to.equal(`export class MyClass {\n}\n`);
chai_1.expect(exportsFile.getFullText()).to.equal(`export {MyClass as MyNewAlias} from "./MyClass";\n`);
chai_1.expect(mainFile.getFullText()).to.equal(`import {MyNewAlias} from "./Exports";\n\nconst t = MyNewAlias;\n`);
});
});
describe("getExportDeclaration", () => {
it("should get the parent export declaration", () => {
const { firstChild } = testHelpers_1.getInfoFromText(`export {name} from "./test";`);
const namedExport = firstChild.getNamedExports()[0];
chai_1.expect(namedExport.getExportDeclaration()).to.equal(firstChild);
});
});
});
//# sourceMappingURL=exportSpecifierTests.js.map