UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

98 lines (96 loc) 5.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("ImportSpecifier", () => { describe("setName", () => { it("should only change what's imported", () => { const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText("import {name} from './file'; const t = name;"); const namedImport = firstChild.getNamedImports()[0]; const otherSourceFile = tsSimpleAst.addSourceFileFromText("file.ts", "export class name {}\nexport class newName {}"); namedImport.setName("newName"); chai_1.expect(sourceFile.getText()).to.equal("import {newName} from './file'; const t = name;"); chai_1.expect(otherSourceFile.getText()).to.equal("export class name {}\nexport class newName {}"); }); it("should set only the identifier when an alias already exists", () => { const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText("import {name as alias} from './file'; const t = alias;"); const namedImport = firstChild.getNamedImports()[0]; const otherSourceFile = tsSimpleAst.addSourceFileFromText("file.ts", "export class name {}\nexport class newName {}"); namedImport.setName("newName"); chai_1.expect(sourceFile.getText()).to.equal("import {newName as alias} from './file'; const t = alias;"); chai_1.expect(otherSourceFile.getText()).to.equal("export class name {}\nexport class newName {}"); }); }); describe("renameName", () => { it("should rename what's being imported", () => { const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText("import {name} from './file'; const t = name;"); const namedImport = firstChild.getNamedImports()[0]; const otherSourceFile = tsSimpleAst.addSourceFileFromText("file.ts", "export class name {}"); namedImport.renameName("newName"); chai_1.expect(sourceFile.getText()).to.equal("import {newName} from './file'; const t = newName;"); chai_1.expect(otherSourceFile.getText()).to.equal("export class newName {}"); }); }); describe("getName", () => { function doTest(text, name) { const { firstChild } = testHelpers_1.getInfoFromText(text); const namedImport = firstChild.getNamedImports()[0]; chai_1.expect(namedImport.getName().getText()).to.equal(name); } it("should get the name when there is no alias", () => { doTest(`import {name} from "./test";`, "name"); }); it("should get the name when there is an alias", () => { doTest(`import {name as alias} from "./test";`, "name"); }); it("should get the identifier when it's a default keyword", () => { doTest(`import {default as alias} from "./test";`, "default"); }); }); describe("setAlias", () => { function doTest(text, alias, expected) { const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText(text); const otherSourceFile = tsSimpleAst.addSourceFileFromText("file.ts", "export class name {}"); const namedImport = firstChild.getNamedImports()[0]; namedImport.setAlias(alias); chai_1.expect(sourceFile.getText()).to.equal(expected); chai_1.expect(otherSourceFile.getText()).to.equal("export class name {}"); } it("should be set when there is no alias", () => { doTest("import {name} from './file';", "alias", "import {name as alias} from './file';"); }); it("should be set and rename anything in the current file to the alias", () => { doTest("import {name} from './file'; const t = name;", "alias", "import {name as alias} from './file'; const t = alias;"); }); it("should rename when there is an alias", () => { doTest("import {name as alias} from './file'; const t = alias;", "newAlias", "import {name as newAlias} from './file'; const t = newAlias;"); }); }); describe("getAlias", () => { function doTest(text, alias) { const { firstChild } = testHelpers_1.getInfoFromText(text); const namedImport = firstChild.getNamedImports()[0]; if (alias == null) chai_1.expect(namedImport.getAlias()).to.equal(undefined); else chai_1.expect(namedImport.getAlias().getText()).to.equal(alias); } it("should be undefined there is no alias", () => { doTest(`import {name} from "./test";`, undefined); }); it("should get the alias when there is an alias", () => { doTest(`import {name as alias} from "./test";`, "alias"); }); it("should get the alias when there is a default keyword", () => { doTest(`import {default as alias} from "./test";`, "alias"); }); }); describe("getImportDeclaration", () => { it("should get the parent import declaration", () => { const { firstChild } = testHelpers_1.getInfoFromText(`import {name} from "./test";`); const namedImport = firstChild.getNamedImports()[0]; chai_1.expect(namedImport.getImportDeclaration()).to.equal(firstChild); }); }); }); //# sourceMappingURL=importSpecifierTests.js.map