UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

231 lines (229 loc) 12.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("ImportDeclaration", () => { describe("setModuleSpecifier", () => { function doTest(text, newModuleSpecifier, expected) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text); firstChild.setModuleSpecifier(newModuleSpecifier); chai_1.expect(sourceFile.getText()).to.equal(expected); } it("should set the module specifier when using single quotes", () => { doTest(`import test from './test';`, "./new-test", `import test from './new-test';`); }); it("should set the module specifier when using double quotes", () => { doTest(`import test from "./test";`, "./new-test", `import test from "./new-test";`); }); it("should set the module specifier when it's empty", () => { doTest(`import test from "";`, "./new-test", `import test from "./new-test";`); }); }); describe("getModuleSpecifier", () => { function doTest(text, expected) { const { firstChild } = testHelpers_1.getInfoFromText(text); chai_1.expect(firstChild.getModuleSpecifier()).to.equal(expected); } it("should get the module specifier when using single quotes", () => { doTest("import * as test from './test'", "./test"); }); it("should get the module specifier when using double quotes", () => { doTest(`import defaultExport, {test} from "./test"`, "./test"); }); it("should get the module specifier when importing for side effects", () => { doTest(`import "./test"`, "./test"); }); }); describe("setDefaultImport", () => { function doTest(text, newDefaultImport, expected) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text); firstChild.setDefaultImport(newDefaultImport); chai_1.expect(sourceFile.getText()).to.equal(expected); } it("should rename when exists", () => { doTest(`import identifier from './file'; const t = identifier;`, "newName", `import newName from './file'; const t = newName;`); }); it("should set the default import when importing for side effects", () => { doTest(`import './file';`, "identifier", `import identifier from './file';`); }); it("should set the default import when named import exists", () => { doTest(`import {named} from './file';`, "identifier", `import identifier, {named} from './file';`); }); it("should set the default import when namespace import exists", () => { doTest(`import * as name from './file';`, "identifier", `import identifier, * as name from './file';`); }); }); describe("getDefaultImport", () => { function doTest(text, expectedName) { const { firstChild } = testHelpers_1.getInfoFromText(text); const defaultImport = firstChild.getDefaultImport(); if (expectedName == null) chai_1.expect(defaultImport).to.be.undefined; else chai_1.expect(defaultImport.getText()).to.equal(expectedName); } it("should get the default import when it exists", () => { doTest(`import defaultImport from "./test";`, "defaultImport"); }); it("should get the default import when a named import exists as well", () => { doTest(`import defaultImport, {name as any} from "./test";`, "defaultImport"); }); it("should get the default import when a namespace import exists as well", () => { doTest(`import defaultImport, * as name from "./test";`, "defaultImport"); }); it("should not get the default import when a named import exists", () => { doTest(`import {name as any} from "./test";`, undefined); }); it("should not get the default import when a namespace import exists", () => { doTest(`import * as name from "./test";`, undefined); }); it("should not get the default import when importing for the side effects", () => { doTest(`import "./test";`, undefined); }); }); describe("setNamespaceImport", () => { function doTest(text, newNamespaceImport, expected) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text); firstChild.setNamespaceImport(newNamespaceImport); chai_1.expect(sourceFile.getText()).to.equal(expected); } it("should rename when exists", () => { doTest(`import * as identifier from './file'; const t = identifier;`, "newName", `import * as newName from './file'; const t = newName;`); }); it("should set the namespace import when importing for side effects", () => { doTest(`import './file';`, "identifier", `import * as identifier from './file';`); }); it("should throw an error when a named import exists", () => { chai_1.expect(() => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(`import {named} from './file';`); firstChild.setNamespaceImport("identifier"); }).to.throw(); }); it("should set the namespace import when a default import exists", () => { doTest(`import name from './file';`, "identifier", `import name, * as identifier from './file';`); }); }); describe("getNamespaceImport", () => { function doTest(text, expectedName) { const { firstChild } = testHelpers_1.getInfoFromText(text); const identifier = firstChild.getNamespaceImport(); if (expectedName == null) chai_1.expect(identifier).to.be.undefined; else chai_1.expect(identifier.getText()).to.equal(expectedName); } it("should get the namespace import when it exists", () => { doTest(`import * as name from "./test";`, "name"); }); it("should get the namespace import when a default import exists as well", () => { doTest(`import defaultImport, * as name from "./test";`, "name"); }); it("should not get the default import when a default and named import exist", () => { doTest(`import defaultImport, {name as any} from "./test";`, undefined); }); it("should not get the default import when a named import exists", () => { doTest(`import {name as any} from "./test";`, undefined); }); it("should not get the default import when a default import exists", () => { doTest(`import defaultImport from "./test";`, undefined); }); it("should not get the default import when importing for the side effects", () => { doTest(`import "./test";`, undefined); }); }); describe("getNamedImports", () => { function doTest(text, expected) { const { firstChild } = testHelpers_1.getInfoFromText(text); const namedImports = firstChild.getNamedImports(); chai_1.expect(namedImports.length).to.equal(expected.length); for (let i = 0; i < namedImports.length; i++) { chai_1.expect(namedImports[i].getName().getText()).to.equal(expected[i].name); if (expected[i].alias == null) chai_1.expect(namedImports[i].getAlias()).to.equal(undefined); else chai_1.expect(namedImports[i].getAlias().getText()).to.equal(expected[i].alias); } } it("should get the named imports", () => { doTest(`import {name, name2, name3 as name4} from "./test";`, [{ name: "name" }, { name: "name2" }, { name: "name3", alias: "name4" }]); }); it("should get the named import when a default and named import exist", () => { doTest(`import defaultImport, {name as any} from "./test";`, [{ name: "name", alias: "any" }]); }); it("should not get anything when only a namespace import exists", () => { doTest(`import * as name from "./test";`, []); }); it("should not get anything when a a namespace import and a default import exists", () => { doTest(`import defaultImport, * as name from "./test";`, []); }); it("should not get anything when a default import exists", () => { doTest(`import defaultImport from "./test";`, []); }); it("should not get anything when importing for the side effects", () => { doTest(`import "./test";`, []); }); }); describe("insertNamedImports", () => { function doTest(text, index, structures, expected) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text); firstChild.insertNamedImports(index, structures); chai_1.expect(sourceFile.getText()).to.equal(expected); } it("should insert named imports when importing for the side effects", () => { doTest(`import "./test";`, 0, [{ name: "name", alias: "alias" }], `import {name as alias} from "./test";`); }); it("should insert named imports when a default import exists", () => { doTest(`import Default from "./test";`, 0, [{ name: "name" }, { name: "name2" }], `import Default, {name, name2} from "./test";`); }); it("should insert named imports at the start", () => { doTest(`import {name3} from "./test";`, 0, [{ name: "name1" }, { name: "name2" }], `import {name1, name2, name3} from "./test";`); }); it("should insert named imports at the end", () => { doTest(`import {name1} from "./test";`, 1, [{ name: "name2" }, { name: "name3" }], `import {name1, name2, name3} from "./test";`); }); it("should insert named imports in the middle", () => { doTest(`import {name1, name4} from "./test";`, 1, [{ name: "name2" }, { name: "name3" }], `import {name1, name2, name3, name4} from "./test";`); }); }); describe("insertNamedImport", () => { function doTest(text, index, structure, expected) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text); firstChild.insertNamedImport(index, structure); chai_1.expect(sourceFile.getText()).to.equal(expected); } it("should insert at the specified index", () => { doTest(`import {name1, name3} from "./test";`, 1, { name: "name2" }, `import {name1, name2, name3} from "./test";`); }); }); describe("addNamedImport", () => { function doTest(text, structure, expected) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text); firstChild.addNamedImport(structure); chai_1.expect(sourceFile.getText()).to.equal(expected); } it("should add at the end", () => { doTest(`import {name1, name2} from "./test";`, { name: "name3" }, `import {name1, name2, name3} from "./test";`); }); }); describe("addNamedImports", () => { function doTest(text, structures, expected) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text); firstChild.addNamedImports(structures); chai_1.expect(sourceFile.getText()).to.equal(expected); } it("should add named imports at the end", () => { doTest(`import {name1} from "./test";`, [{ name: "name2" }, { name: "name3" }], `import {name1, name2, name3} from "./test";`); }); }); describe("remove", () => { function doTest(text, index, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(text); sourceFile.getImports()[index].remove(); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); } it("should remove the import declaration", () => { doTest("import * from 'i';\nimport * from 'j';\nimport * from 'k';\n", 1, "import * from 'i';\nimport * from 'k';\n"); }); }); }); //# sourceMappingURL=importDeclarationTests.js.map