ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
42 lines (40 loc) • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
describe("TypeReferenceNode", () => {
function getTypeReferenceNode(text) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
return sourceFile.getVariableDeclarations()[0].getTypeNodeOrThrow();
}
describe("getTypeName", () => {
function doTest(text, expectedSyntaxKind, expectedTypeName) {
const typeRefNode = getTypeReferenceNode(text);
chai_1.expect(typeRefNode.getTypeName().getKind()).to.equal(expectedSyntaxKind);
chai_1.expect(typeRefNode.getTypeName().getText()).to.equal(expectedTypeName);
}
it("should get the type name when an identifier", () => {
doTest("const myVariable: Class<string>;", ts.SyntaxKind.Identifier, "Class");
});
it("should get the type name when a fully qualified name", () => {
doTest("const myVariable: Class.Tests<string>;", ts.SyntaxKind.QualifiedName, "Class.Tests");
});
});
describe("getTypeArguments", () => {
function doTest(text, expectedArgs) {
const typeRefNode = getTypeReferenceNode(text);
chai_1.expect(typeRefNode.getTypeArguments().map(t => t.getText())).to.deep.equal(expectedArgs);
}
it("should return empty an empty array when there are no type args", () => {
doTest("const myVariable: Class.Tests;", []);
});
it("should get the type args when an identifier", () => {
doTest("const myVariable: Class<string>;", ["string"]);
});
it("should get the type args when a fully qualified name", () => {
doTest("const myVariable: Class.Tests<string>;", ["string"]);
});
});
});
//# sourceMappingURL=typeReferenceTests.js.map