ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
61 lines (59 loc) • 3.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
function getGetAccessorInfo(text) {
const result = testHelpers_1.getInfoFromText(text);
const getAccessor = result.firstChild.getInstanceProperties().find(f => f.getKind() === ts.SyntaxKind.GetAccessor);
return Object.assign({}, result, { getAccessor });
}
describe("GetAccessorDeclaration", () => {
describe("getSetAccessor", () => {
it("should return undefined if no corresponding get accessor exists", () => {
const { getAccessor } = getGetAccessorInfo(`class Identifier { get identifier(): string { return "" } }`);
chai_1.expect(getAccessor.getSetAccessor()).to.be.undefined;
});
it("should return the set accessor if a corresponding one exists", () => {
const code = `class Identifier { get identifier() { return ""; } set identifier(val: string) {}\n` +
`get identifier2(): string { return "" }\nset identifier2(value: string) {} }`;
const { getAccessor } = getGetAccessorInfo(code);
chai_1.expect(getAccessor.getSetAccessor().getText()).to.equal("set identifier(val: string) {}");
});
});
describe("getSetAccessorOrThrow", () => {
it("should throw if no corresponding get accessor exists", () => {
const { getAccessor } = getGetAccessorInfo(`class Identifier { get identifier(): string { return "" } }`);
chai_1.expect(() => getAccessor.getSetAccessorOrThrow()).to.throw();
});
it("should return the set accessor if a corresponding one exists", () => {
const code = `class Identifier { get identifier() { return ""; } set identifier(val: string) {}\n` +
`get identifier2(): string { return "" }\nset identifier2(value: string) {} }`;
const { getAccessor } = getGetAccessorInfo(code);
chai_1.expect(getAccessor.getSetAccessorOrThrow().getText()).to.equal("set identifier(val: string) {}");
});
});
describe("remove", () => {
function doTest(code, nameToRemove, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code);
firstChild.getInstanceProperty(nameToRemove).remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should remove when it's the only member", () => {
doTest("class Identifier {\n get prop(): string { return ''; }\n}", "prop", "class Identifier {\n}");
});
it("should not remove the set accessor", () => {
doTest("class Identifier {\n get prop(): string { return ''; }\n\n set prop(val: string) {}\n}", "prop", "class Identifier {\n set prop(val: string) {}\n}");
});
it("should remove when it's the first member", () => {
doTest("class Identifier {\n get prop(): string {}\n\n get prop2(): string {}\n}", "prop", "class Identifier {\n get prop2(): string {}\n}");
});
it("should remove when it's the middle member", () => {
doTest("class Identifier {\n get prop(): string {}\n\n get prop2(): string {}\n\n get prop3(): string {}\n}", "prop2", "class Identifier {\n get prop(): string {}\n\n get prop3(): string {}\n}");
});
it("should remove when it's the last member", () => {
doTest("class Identifier {\n prop: string;\n get prop2(): string {}\n}", "prop2", "class Identifier {\n prop: string;\n}");
});
});
});
//# sourceMappingURL=getAccessorDeclarationTests.js.map