ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
103 lines (101 loc) • 7.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
describe("Identifier", () => {
describe("rename", () => {
it("should rename", () => {
const text = "function myFunction() {} const reference = myFunction;";
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
firstChild.getNameIdentifier().rename("newFunction");
chai_1.expect(sourceFile.getFullText()).to.equal(text.replace(/myFunction/g, "newFunction"));
});
});
describe("getDefinitions", () => {
it("should get the definition", () => {
const sourceFileText = "function myFunction() {}\nconst reference = myFunction;";
const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText(sourceFileText);
const secondSourceFile = tsSimpleAst.addSourceFileFromText("second.ts", "const reference2 = myFunction;");
const definitions = secondSourceFile.getVariableDeclarationOrThrow("reference2").getInitializerOrThrow().getDefinitions();
chai_1.expect(definitions.length).to.equal(1);
chai_1.expect(definitions[0].getName()).to.equal("myFunction");
chai_1.expect(definitions[0].getSourceFile().getFullText()).to.equal(sourceFileText);
chai_1.expect(definitions[0].getKind()).to.equal(ts.ScriptElementKind.functionElement);
chai_1.expect(definitions[0].getTextSpan().getStart()).to.equal(firstChild.getNameIdentifier().getStart());
chai_1.expect(definitions[0].getNode()).to.equal(firstChild);
});
it("should get the definition when nested inside a namespace", () => {
const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText("namespace N { export function myFunction() {} }\nconst reference = N.myFunction;");
const definitions = sourceFile.getVariableDeclarationOrThrow("reference").getInitializerOrThrow().getNameIdentifier().getDefinitions();
chai_1.expect(definitions.length).to.equal(1);
chai_1.expect(definitions[0].getNode()).to.equal(firstChild.getFunctions()[0]);
});
});
describe("getImplementations", () => {
it("should get the implementations", () => {
const sourceFileText = "interface MyInterface {}\nexport class Class1 implements MyInterface {}\nclass Class2 implements MyInterface {}";
const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText(sourceFileText);
const implementations = firstChild.getNameIdentifier().getImplementations();
chai_1.expect(implementations.length).to.equal(2);
chai_1.expect(implementations[0].getNode().getName()).to.equal("Class1");
chai_1.expect(implementations[1].getNode().getName()).to.equal("Class2");
});
});
describe("findReferences", () => {
it("should find all the references", () => {
const { firstChild, sourceFile, tsSimpleAst } = testHelpers_1.getInfoFromText("function myFunction() {}\nconst reference = myFunction;");
const secondSourceFile = tsSimpleAst.addSourceFileFromText("second.ts", "const reference2 = myFunction;");
const referencedSymbols = firstChild.getNameIdentifier().findReferences();
chai_1.expect(referencedSymbols.length).to.equal(1);
const referencedSymbol = referencedSymbols[0];
const references = referencedSymbol.getReferences();
// definition
const definition = referencedSymbol.getDefinition();
chai_1.expect(definition.getSourceFile()).to.equal(sourceFile);
chai_1.expect(definition.getContainerName()).to.equal("");
chai_1.expect(definition.getContainerKind()).to.equal("");
chai_1.expect(definition.getKind()).to.equal("function");
chai_1.expect(definition.getName()).to.equal("function myFunction(): void");
chai_1.expect(definition.getTextSpan().getStart()).to.equal(9);
chai_1.expect(definition.getTextSpan().getLength()).to.equal("myFunction".length);
chai_1.expect(definition.getDisplayParts()[0].getText()).to.equal("function"); // only bother testing the first one
chai_1.expect(definition.getDisplayParts()[0].getKind()).to.equal("keyword");
// first reference
chai_1.expect(references[0].getSourceFile()).to.equal(sourceFile);
chai_1.expect(references[0].getTextSpan().getStart()).to.equal(9);
chai_1.expect(references[0].getTextSpan().getLength()).to.equal("myFunction".length);
chai_1.expect(references[0].isDefinition()).to.equal(true);
chai_1.expect(references[0].isInString()).to.equal(undefined);
chai_1.expect(references[0].isWriteAccess()).to.equal(true);
chai_1.expect(references[0].getNode().getParentOrThrow().getKind()).to.equal(ts.SyntaxKind.FunctionDeclaration);
// second reference
chai_1.expect(references[1].getSourceFile()).to.equal(sourceFile);
chai_1.expect(references[1].getTextSpan().getStart()).to.equal(43);
chai_1.expect(references[1].getTextSpan().getLength()).to.equal("myFunction".length);
chai_1.expect(references[1].isDefinition()).to.equal(false);
chai_1.expect(references[1].isInString()).to.equal(undefined);
chai_1.expect(references[1].isWriteAccess()).to.equal(false);
chai_1.expect(references[1].getNode().getParentOrThrow().getKind()).to.equal(ts.SyntaxKind.VariableDeclaration);
// third reference
chai_1.expect(references[2].getSourceFile()).to.equal(secondSourceFile);
chai_1.expect(references[2].getTextSpan().getStart()).to.equal(19);
chai_1.expect(references[2].getTextSpan().getLength()).to.equal("myFunction".length);
chai_1.expect(references[2].isDefinition()).to.equal(false);
chai_1.expect(references[2].isInString()).to.equal(undefined);
chai_1.expect(references[2].isWriteAccess()).to.equal(false);
chai_1.expect(references[2].getNode().getParentOrThrow().getKind()).to.equal(ts.SyntaxKind.VariableDeclaration);
});
});
describe("getType", () => {
function doTest(text, expectedTypes) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
const identifiers = sourceFile.getDescendantsOfKind(ts.SyntaxKind.Identifier);
chai_1.expect(identifiers.map(i => i.getType().getText())).to.deep.equal(expectedTypes);
}
it("should get the identifier", () => {
doTest("class Identifier {}\n var t = Identifier;", ["Identifier", "typeof Identifier", "typeof Identifier"]);
});
});
});
//# sourceMappingURL=identifierTests.js.map