UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

218 lines (216 loc) 12.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const errors = require("./../../../errors"); const testHelpers_1 = require("./../testHelpers"); describe("ExportableNode", () => { const { sourceFile: mainSourceFile } = testHelpers_1.getInfoFromText("export var exportedVar = 1;\nvar myExplicitVar: string;\nexport default class Identifier {}\n"); const statements = mainSourceFile.getVariableStatements(); const exportedStatement = statements[0]; const notExportedStatement = statements[1]; const exportedDefaultClass = mainSourceFile.getClasses()[0]; describe("hasExportKeyword", () => { it("should have an export keyword when exported", () => { chai_1.expect(exportedStatement.hasExportKeyword()).to.be.true; }); it("should not have an export keyword when not exported", () => { chai_1.expect(notExportedStatement.hasExportKeyword()).to.be.false; }); }); describe("getExportKeyword", () => { it("should have an export keyword when exported", () => { chai_1.expect(exportedStatement.getExportKeyword().getText()).to.equal("export"); }); it("should not have an export keyword when not exported", () => { chai_1.expect(notExportedStatement.getExportKeyword()).to.be.undefined; }); }); describe("getExportKeywordOrThrow", () => { it("should have an export keyword when exported", () => { chai_1.expect(exportedStatement.getExportKeywordOrThrow().getText()).to.equal("export"); }); it("should not have an export keyword when not exported", () => { chai_1.expect(() => notExportedStatement.getExportKeywordOrThrow()).to.throw(); }); }); describe("hasDefaultKeyword", () => { it("should have a default keyword when default exported", () => { chai_1.expect(exportedDefaultClass.hasDefaultKeyword()).to.be.true; }); describe("not exported node", () => { it("should not have a default keyword when not default exported", () => { chai_1.expect(exportedStatement.hasDefaultKeyword()).to.be.false; }); }); }); describe("getDefaultKeyword", () => { it("should have a default keyword when default exported", () => { chai_1.expect(exportedDefaultClass.getDefaultKeyword().getText()).to.equal("default"); }); it("should not have an export keyword when not default exported", () => { chai_1.expect(exportedStatement.getDefaultKeyword()).to.be.undefined; }); }); describe("getDefaultKeywordOrThrow", () => { it("should have a default keyword when default exported", () => { chai_1.expect(exportedDefaultClass.getDefaultKeywordOrThrow().getText()).to.equal("default"); }); it("should not have an export keyword when not default exported", () => { chai_1.expect(() => exportedStatement.getDefaultKeywordOrThrow()).to.throw(); }); }); describe("isDefaultExport", () => { function doTest(text, expected) { const { firstChild } = testHelpers_1.getInfoFromText(text); chai_1.expect(firstChild.isDefaultExport()).to.equal(expected); } it("should be the default export when default exported on a different line", () => { doTest("class Identifier {}\nexport default Identifier;", true); }); it("should be the default export when default exported on the same line", () => { doTest("export default class Identifier {}", true); }); it("should not be a default export when not", () => { doTest("class Identifier {}", false); }); it("should not be a default export when not and there exists another default export", () => { doTest("class Identifier {}\nexport default class Identifier2 {}", false); }); }); describe("isNamedExport", () => { function doTest(text, expected) { const { firstChild } = testHelpers_1.getInfoFromText(text); chai_1.expect(firstChild.isNamedExport()).to.equal(expected); } it("should be a named export when one", () => { doTest("export class Identifier {}", true); }); it("should not be a named export when it's a default export", () => { doTest("export default class Identifier {}", false); }); it("should not be a named export when neither a default or named export", () => { doTest("class Identifier {}", false); }); it("should not be a named export when contained in a namespace", () => { const { firstChild } = testHelpers_1.getInfoFromText("namespace Namespace { export class Identifier {} }"); const innerClass = firstChild.getClasses()[0]; chai_1.expect(innerClass.isNamedExport()).to.be.false; }); }); describe("setIsDefaultExport", () => { function doTest(text, value, expectedText) { const { sourceFile, firstChild } = testHelpers_1.getInfoFromText(text); firstChild.setIsDefaultExport(value); chai_1.expect(sourceFile.getText()).to.equal(expectedText); } describe("setting as the default export", () => { it("should remove any existing default export and make the specified class the default export", () => { doTest("class Identifier {}\nexport default class Identifier2 {}", true, "export default class Identifier {}\nclass Identifier2 {}"); }); it("should remove any existing default export and make the specified class the default export when using a default export statement", () => { doTest("class Identifier {}\nclass Identifier2 {}\nexport default Identifier2;\n", true, "export default class Identifier {}\nclass Identifier2 {}\n"); }); it("should do nothing if already the default export", () => { doTest("export default class Identifier {}", true, "export default class Identifier {}"); }); it("should add default if already an export", () => { doTest("export class Identifier {}", true, "export default class Identifier {}"); }); it("should throw an error if setting as a default export within a namespace", () => { const { firstChild } = testHelpers_1.getInfoFromText("namespace Identifier { class Identifier {} }"); const innerChild = firstChild.getClasses()[0]; chai_1.expect(() => innerChild.setIsDefaultExport(true)).to.throw(errors.InvalidOperationError); }); }); describe("unsetting as the default export", () => { it("should remove the default export", () => { doTest("export default class Identifier {}", false, "class Identifier {}"); }); it("should do nothing if already not the default export", () => { doTest("export class Identifier {}\nexport default class Identifier2 {}", false, "export class Identifier {}\nexport default class Identifier2 {}"); }); }); }); describe("setIsExported", () => { function doTest(text, value, expected) { const { sourceFile, firstChild } = testHelpers_1.getInfoFromText(text); firstChild.setIsExported(value); chai_1.expect(sourceFile.getText()).to.equal(expected); } function doInnerTest(text, value, expected) { const { sourceFile, firstChild } = testHelpers_1.getInfoFromText(text); const innerChild = firstChild.getClasses()[0]; innerChild.setIsExported(value); chai_1.expect(sourceFile.getText()).to.equal(expected); } describe("setting as exported", () => { it("should do nothing if already exported", () => { doTest("export class Identifier {}", true, "export class Identifier {}"); }); it("should add the export keyword if not exported", () => { doTest("class Identifier {}", true, "export class Identifier {}"); }); it("should add the export keyword if not exported to a class that has decorators", () => { doTest("@dec class Identifier {}", true, "@dec export class Identifier {}"); }); it("should do nothing if already exported from a namespace", () => { doInnerTest("namespace Identifier { export class Identifier {} }", true, "namespace Identifier { export class Identifier {} }"); }); it("should add the export keyword if not exported from a namespace", () => { doInnerTest("namespace Identifier { class Identifier {} }", true, "namespace Identifier { export class Identifier {} }"); }); it("should remove it as a default export if one", () => { doTest("export default class Identifier {}", true, "export class Identifier {}"); }); it("should remove it as a default export if one and exported in a separate statement", () => { doTest("class Identifier {}\nexport default Identifier;\n", true, "export class Identifier {}\n"); }); }); describe("setting as not exported", () => { it("should do nothing if already not exported", () => { doTest("class Identifier {}", false, "class Identifier {}"); }); it("should remove the export keyword if exported", () => { doTest("export class Identifier {}", false, "class Identifier {}"); }); it("should remove the export keyword if exported and has another modifier", () => { doTest("export abstract class Identifier {}", false, "abstract class Identifier {}"); }); it("should do nothing if already not exported from a namespace", () => { doInnerTest("namespace Identifier { class Identifier {} }", false, "namespace Identifier { class Identifier {} }"); }); it("should remove the export keyword if exported from a namespace", () => { doInnerTest("namespace Identifier { export class Identifier {} }", false, "namespace Identifier { class Identifier {} }"); }); it("should remove it as a default export if one", () => { doTest("export default class Identifier {}", false, "class Identifier {}"); }); it("should remove it as a default export if one and exported in a separate statement", () => { doTest("class Identifier {}\nexport default Identifier;\n", false, "class Identifier {}\n"); }); }); }); describe("fill", () => { function doTest(startingCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode); firstChild.fill(structure); chai_1.expect(firstChild.getText()).to.equal(expectedCode); } it("should not modify anything if the structure doesn't change anything", () => { doTest("function myFunction() {}", {}, "function myFunction() {}"); }); it("should not modify anything if the structure doesn't change anything and the node has everything set", () => { doTest("export default function myFunction() {}", {}, "export default function myFunction() {}"); }); it("should modify when setting as export", () => { doTest("function myFunction() {}", { isExported: true }, "export function myFunction() {}"); }); it("should modify when setting as default export", () => { doTest("function myFunction() {}", { isDefaultExport: true }, "export default function myFunction() {}"); }); it("should be default export when setting as default export and exported", () => { doTest("function myFunction() {}", { isDefaultExport: true, isExported: true }, "export default function myFunction() {}"); }); }); }); //# sourceMappingURL=exportableNodeTests.js.map