ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
61 lines (59 loc) • 3.75 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 getSetAccessorInfo(text) {
const result = testHelpers_1.getInfoFromText(text);
const setAccessor = result.firstChild.getInstanceProperties().find(f => f.getKind() === ts.SyntaxKind.SetAccessor);
return Object.assign({}, result, { setAccessor });
}
describe("SetAccessorDeclaration", () => {
describe("getGetAccessor", () => {
it("should return undefined if no corresponding set accessor exists", () => {
const { setAccessor } = getSetAccessorInfo(`class Identifier { set identifier(val: string) {} }`);
chai_1.expect(setAccessor.getGetAccessor()).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 { setAccessor } = getSetAccessorInfo(code);
chai_1.expect(setAccessor.getGetAccessor().getText()).to.equal(`get identifier() { return ""; }`);
});
});
describe("getGetAccessorOrThrow", () => {
it("should throw if no corresponding set accessor exists", () => {
const { setAccessor } = getSetAccessorInfo(`class Identifier { set identifier(val: string) {} }`);
chai_1.expect(() => setAccessor.getGetAccessorOrThrow()).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 { setAccessor } = getSetAccessorInfo(code);
chai_1.expect(setAccessor.getGetAccessorOrThrow().getText()).to.equal(`get identifier() { return ""; }`);
});
});
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 set prop(val: string) { }\n}", "prop", "class Identifier {\n}");
});
it("should not remove the get accessor", () => {
doTest("class Identifier {\n set prop(val: string) {}\n\n get prop(): string { return ''; }\n}", "prop", "class Identifier {\n get prop(): string { return ''; }\n}");
});
it("should remove when it's the first member", () => {
doTest("class Identifier {\n set prop(val: string) {}\n\n set prop2(val: string) {}\n}", "prop", "class Identifier {\n set prop2(val: string) {}\n}");
});
it("should remove when it's the middle member", () => {
doTest("class Identifier {\n set prop(val: string) {}\n\n set prop2(val: string) {}\n\n set prop3(val: string) {}\n}", "prop2", "class Identifier {\n set prop(val: string) {}\n\n set prop3(val: string) {}\n}");
});
it("should remove when it's the last member", () => {
doTest("class Identifier {\n prop: string;\n set prop2(val: string) {}\n}", "prop2", "class Identifier {\n prop: string;\n}");
});
});
});
//# sourceMappingURL=setAccessorDeclarationTests.js.map