ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
109 lines (107 loc) • 7.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler_1 = require("./../../../compiler");
const testHelpers_1 = require("./../testHelpers");
describe("FunctionDeclaration", () => {
describe("insertOverloads", () => {
function doTest(startCode, index, structures, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertOverloads(index, structures);
chai_1.expect(result.length).to.equal(structures.length);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should insert when no other overloads exist", () => {
doTest("function identifier() {}\n", 0, [{ returnType: "number" }, {}], "function identifier(): number;\nfunction identifier();\nfunction identifier() {}\n");
});
it("should insert with the ambientable and exportable nodes the same as the implementation signature unless overwritten", () => {
doTest("declare async function* identifier(param: string): string {}\n", 0, [{ returnType: "number", hasDeclareKeyword: false }, {}], "function identifier(): number;\ndeclare function identifier();\ndeclare async function* identifier(param: string): string {}\n");
});
it("should be able to insert at start when another overload exists", () => {
doTest("function identifier();\nfunction identifier() {}\n", 0, [{ returnType: "string" }], "function identifier(): string;\nfunction identifier();\nfunction identifier() {}\n");
});
it("should be able to insert at end when another overload exists", () => {
doTest("function identifier();\nfunction identifier() {}\n", 1, [{ returnType: "string" }], "function identifier();\nfunction identifier(): string;\nfunction identifier() {}\n");
});
it("should be able to insert in the middle when other overloads exists", () => {
doTest("function identifier();\nfunction identifier();\nfunction identifier() {}\n", 1, [{ returnType: "string" }], "function identifier();\nfunction identifier(): string;\nfunction identifier();\nfunction identifier() {}\n");
});
});
describe("insertOverload", () => {
function doTest(startCode, index, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.insertOverload(index, structure);
chai_1.expect(result).to.be.instanceOf(compiler_1.FunctionDeclaration);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should insert", () => {
doTest("function identifier();\nfunction identifier();\nfunction identifier() {}\n", 1, { returnType: "number" }, "function identifier();\nfunction identifier(): number;\nfunction identifier();\nfunction identifier() {}\n");
});
});
describe("addOverloads", () => {
function doTest(startCode, structures, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addOverloads(structures);
chai_1.expect(result.length).to.equal(structures.length);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should add at the end", () => {
doTest("function identifier();\nfunction identifier() {}\n", [{ returnType: "number" }, { returnType: "string" }], "function identifier();\nfunction identifier(): number;\nfunction identifier(): string;\nfunction identifier() {}\n");
});
});
describe("addOverload", () => {
function doTest(startCode, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = firstChild.addOverload(structure);
chai_1.expect(result).to.be.instanceOf(compiler_1.FunctionDeclaration);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode);
}
it("should add at the end", () => {
doTest("function identifier();\nfunction identifier() {}\n", { returnType: "number" }, "function identifier();\nfunction identifier(): number;\nfunction identifier() {}\n");
});
});
describe("fill", () => {
function doTest(startingCode, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode);
firstChild.fill(structure);
chai_1.expect(sourceFile.getText()).to.equal(expectedCode);
}
it("should not modify anything if the structure doesn't change anything", () => {
doTest("function identifier() {\n}", {}, "function identifier() {\n}");
});
it("should modify when changed", () => {
const structure = {
overloads: [{ returnType: "string" }]
};
doTest("function identifier() {\n}", structure, "function identifier(): string;\nfunction identifier() {\n}");
});
});
describe("remove", () => {
function doTest(text, index, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
sourceFile.getFunctions()[index].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
function doOverloadTest(text, index, overloadIndex, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
sourceFile.getFunctions()[index].getOverloads()[overloadIndex].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should remove the function declaration", () => {
doTest("function I() {}\n\nfunction J() {}\n\nfunction K() {}", 1, "function I() {}\n\nfunction K() {}");
});
it("should remove the function declaration and its overloads", () => {
doTest("function I() {}\n\nfunction J(): void;\nfunction J() {}\n\nfunction K() {}", 1, "function I() {}\n\nfunction K() {}");
});
it("should remove the function declaration overload when the first", () => {
doOverloadTest("function I() {}\n\nfunction J(): void;\nfunction J() {}\n\nfunction K() {}", 1, 0, "function I() {}\n\nfunction J() {}\n\nfunction K() {}");
});
it("should remove the function declaration overload when the middle", () => {
doOverloadTest("function I() {}\n\nfunction J(first): void;\nfunction J(second): void;\nfunction J(third): void;\nfunction J() {}\n\nfunction K() {}", 1, 1, "function I() {}\n\nfunction J(first): void;\nfunction J(third): void;\nfunction J() {}\n\nfunction K() {}");
});
it("should remove the function declaration overload when last", () => {
doOverloadTest("function I() {}\n\nfunction J(first): void;\nfunction J(second): void;\nfunction J() {}\n\nfunction K() {}", 1, 1, "function I() {}\n\nfunction J(first): void;\nfunction J() {}\n\nfunction K() {}");
});
});
});
//# sourceMappingURL=functionDeclarationTests.js.map