UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

167 lines (165 loc) 9.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("TypedNode", () => { const { sourceFile: mainSourceFile } = testHelpers_1.getInfoFromText("var myImplicitVar = 1; var myExplicitVar: string; type TypeAlias1 = string;"); const implicitVarDeclaration = mainSourceFile.getVariableStatements()[0].getDeclarationList().getDeclarations()[0]; const explicitVarDeclaration = mainSourceFile.getVariableStatements()[1].getDeclarationList().getDeclarations()[0]; const typeAliasDeclaration = mainSourceFile.getTypeAliases()[0]; describe("getType", () => { it("should get the expected implicit type", () => { chai_1.expect(implicitVarDeclaration.getType().getText()).to.equal("number"); }); it("should get the expected explicit type", () => { chai_1.expect(explicitVarDeclaration.getType().getText()).to.equal("string"); }); it("should get the expected type for a type alias", () => { chai_1.expect(typeAliasDeclaration.getType().getText()).to.equal("string"); }); }); describe("getTypeNode", () => { it("should return undefined when no type node exists", () => { chai_1.expect(implicitVarDeclaration.getTypeNode()).to.be.undefined; }); it("should get the type node when it exists", () => { chai_1.expect(explicitVarDeclaration.getTypeNode().getText()).to.equal("string"); }); it("should get the type node for a type alias", () => { chai_1.expect(typeAliasDeclaration.getTypeNode().getText()).to.equal("string"); }); }); describe("getTypeNodeOrThrow", () => { it("should get the type node when it exists", () => { chai_1.expect(explicitVarDeclaration.getTypeNodeOrThrow().getText()).to.equal("string"); }); it("should return undefined when no type node exists", () => { chai_1.expect(() => implicitVarDeclaration.getTypeNodeOrThrow()).to.throw(); }); }); describe("setType", () => { describe("class properties", () => { function doTest(startText, type, expectedText) { const { firstChild } = testHelpers_1.getInfoFromText(startText); const prop = firstChild.getInstanceProperties()[0]; prop.setType(type); chai_1.expect(firstChild.getText()).to.equal(expectedText); } it("should set when implicit, with a semi-colon, and initializer", () => { doTest(`class Identifier { prop = ""; }`, "any", `class Identifier { prop: any = ""; }`); }); it("should set when explicit, with a semi-colon, no initializer", () => { doTest("class Identifier { prop: string; }", "number", "class Identifier { prop: number; }"); }); it("should set when explicit, without a semi-colon, no initializer", () => { doTest("class Identifier { prop: string }", "number", "class Identifier { prop: number }"); }); it("should set when explicit, with a semi-colon, with initializer", () => { doTest(`class Identifier { prop: string = ""; }`, "any", `class Identifier { prop: any = ""; }`); }); it("should remove when the type is empty", () => { doTest(`class Identifier { prop: string = ""; }`, "", `class Identifier { prop = ""; }`); }); }); describe("function parameters", () => { function doTest(startText, type, expectedText) { const { firstChild } = testHelpers_1.getInfoFromText(startText); const param = firstChild.getParameters()[0]; param.setType(type); chai_1.expect(firstChild.getText()).to.equal(expectedText); } it("should set when implicit", () => { doTest(`function Identifier(param) {}`, "number", `function Identifier(param: number) {}`); }); it("should set when implicit and multiple parameters", () => { doTest(`function Identifier(param, param2) {}`, "number", `function Identifier(param: number, param2) {}`); }); it("should set when explicit", () => { doTest(`function Identifier(param: string) {}`, "number", `function Identifier(param: number) {}`); }); it("should set when explicit and with an initializer", () => { doTest(`function Identifier(param: string = "") {}`, "any", `function Identifier(param: any = "") {}`); }); it("should remove when the type is empty", () => { doTest(`function Identifier(param: string = "") {}`, "", `function Identifier(param = "") {}`); }); }); describe("type alias", () => { // todo: support this scenario /* it("should set when no type exists", () => { const {firstChild} = getInfoFromText<TypeAliasDeclaration>(`type Identifier;`); firstChild.setType("number"); expect(firstChild.getText()).to.equal(`type Identifier = number;`); }); */ it("should set when type exists", () => { const { firstChild } = testHelpers_1.getInfoFromText(`type Identifier = string;`); firstChild.setType("number"); chai_1.expect(firstChild.getText()).to.equal(`type Identifier = number;`); }); it("should throw an error when providing nothing", () => { const { firstChild } = testHelpers_1.getInfoFromText(`type Identifier = string;`); chai_1.expect(() => firstChild.setType("")).to.throw(); }); }); describe("variable declaration", () => { function doTest(startText, type, expectedText) { const { firstChild } = testHelpers_1.getInfoFromText(expectedText); const declaration = firstChild.getDeclarationList().getDeclarations()[0]; declaration.setType(type); chai_1.expect(firstChild.getText()).to.equal(expectedText); } it("should set when no type exists", () => { doTest(`var identifier;`, "number", `var identifier: number;`); }); it("should set when type exists", () => { doTest(`var identifier: string;`, "number", `var identifier: number;`); }); it("should set when type exists and initializer", () => { doTest(`var identifier: string = "";`, "number", `var identifier: number = "";`); }); it("should set for other declaration in list", () => { const { firstChild } = testHelpers_1.getInfoFromText(`var var1, var2, var3;`); const declaration = firstChild.getDeclarationList().getDeclarations()[1]; declaration.setType("number"); chai_1.expect(firstChild.getText()).to.equal(`var var1, var2: number, var3;`); }); it("should remove when the type is empty", () => { doTest(`var identifier: string = "";`, "", `var identifier = "";`); }); }); }); 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 modify when setting", () => { doTest("type myAlias = string;", { type: "number" }, "type myAlias = number;"); }); it("should not modify anything if the structure doesn't change anything", () => { doTest("type myAlias = string;", {}, "type myAlias = string;"); }); }); describe("removeType", () => { function doTest(startText, expectedText) { const { firstChild } = testHelpers_1.getInfoFromText(startText); const prop = firstChild.getInstanceProperties()[0]; prop.removeType(); chai_1.expect(firstChild.getText()).to.equal(expectedText); } it("should remove when exists", () => { doTest(`class Identifier { prop: string = ""; }`, `class Identifier { prop = ""; }`); }); it("should do nothing when not exists", () => { doTest(`class Identifier { prop = ""; }`, `class Identifier { prop = ""; }`); }); it("should throw an error when removing a type alias", () => { const { firstChild } = testHelpers_1.getInfoFromText(`type Identifier = string;`); chai_1.expect(() => firstChild.removeType()).to.throw(); }); }); }); //# sourceMappingURL=typedNodeTests.js.map