UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

311 lines (309 loc) 16.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("Type", () => { function getTypeFromText(text) { const result = testHelpers_1.getInfoFromText(text); return Object.assign({}, result, { firstType: result.firstChild.getDeclarationList().getDeclarations()[0].getType() }); } describe("compilerType", () => { it("should get the compiler type", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.compilerType.flags).to.equal(ts.TypeFlags.String); }); }); describe("getText", () => { const longType = "string | number | Date | { reallyReallyLooooooonnnnnnnnnnnnggggggggggggggggNnnnnnnaaaaaaaaammmmmmmmmmmeeeeee: string; }"; it("should get the text", () => { const { firstType } = getTypeFromText("let myType: string[];"); chai_1.expect(firstType.getText()).to.equal("string[]"); }); it("should get the text when providing the enclosing node", () => { const { firstChild, firstType } = getTypeFromText(`let myType: ${longType};`); chai_1.expect(firstType.getText(firstChild)).to.equal(longType); }); it("should use the type format flags", () => { const { firstChild, firstType } = getTypeFromText(`let myType: ${longType};`); chai_1.expect(firstType.getText(firstChild, ts.TypeFormatFlags.None)).to.equal(longType.substring(0, 97) + "..."); }); }); describe("getProperties", () => { it("should get the properties when there are none", () => { const { firstType } = getTypeFromText("let myType: {};"); chai_1.expect(firstType.getProperties().length).to.equal(0); }); it("should get the properties of a non-object type", () => { const { firstType } = getTypeFromText("let myType: 1;"); chai_1.expect(firstType.getProperties().length).to.equal(6); }); it("should get the properties when some exist", () => { const { firstType } = getTypeFromText("let myType: { str: string; };"); const props = firstType.getProperties(); chai_1.expect(props.length).to.equal(1); chai_1.expect(props[0].getName()).to.equal("str"); }); }); describe("getProperty", () => { it("should get the property by name", () => { const { firstType } = getTypeFromText("let myType: { str: string; other: number; };"); const prop = firstType.getProperty("other"); chai_1.expect(prop.getName()).to.equal("other"); }); it("should get the property by function", () => { const { firstType } = getTypeFromText("let myType: { str: string; other: number; };"); const prop = firstType.getProperty(p => p.getName() === "other"); chai_1.expect(prop.getName()).to.equal("other"); }); }); describe("getApparentProperties", () => { it("should return the apparent properties of a type", () => { const { firstType } = getTypeFromText("let myType: 1;"); chai_1.expect(firstType.getApparentProperties().length).to.equal(6); }); }); describe("getApparentProperty", () => { it("should get the property by name", () => { const { firstType } = getTypeFromText("let myType: { str: string; other: number; };"); const prop = firstType.getApparentProperty("other"); chai_1.expect(prop.getName()).to.equal("other"); }); it("should get the property by function", () => { const { firstType } = getTypeFromText("let myType: { str: string; other: number; };"); const prop = firstType.getApparentProperty(p => p.getName() === "other"); chai_1.expect(prop.getName()).to.equal("other"); }); }); describe("getUnionTypes", () => { it("should get the union types when there aren't any", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.getUnionTypes().length).to.equal(0); }); it("should get the union types when they exist", () => { const { firstType } = getTypeFromText("let myType: string | number;"); chai_1.expect(firstType.getUnionTypes().length).to.equal(2); chai_1.expect(firstType.getUnionTypes()[0].getFlags()).to.equal(ts.TypeFlags.String); chai_1.expect(firstType.getUnionTypes()[1].getFlags()).to.equal(ts.TypeFlags.Number); }); it("should not return anything for an intersection type", () => { const { firstType } = getTypeFromText("let myType: string & number;"); chai_1.expect(firstType.getUnionTypes().length).to.equal(0); }); }); describe("getIntersectionTypes", () => { it("should get the union types when there aren't any", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.getIntersectionTypes().length).to.equal(0); }); it("should get the union types when they exist", () => { const { firstType } = getTypeFromText("let myType: string & number;"); chai_1.expect(firstType.getIntersectionTypes().length).to.equal(2); chai_1.expect(firstType.getIntersectionTypes()[0].getFlags()).to.equal(ts.TypeFlags.String); chai_1.expect(firstType.getIntersectionTypes()[1].getFlags()).to.equal(ts.TypeFlags.Number); }); it("should not return anything for a union type", () => { const { firstType } = getTypeFromText("let myType: string | number;"); chai_1.expect(firstType.getIntersectionTypes().length).to.equal(0); }); }); describe("isAnonymousType", () => { it("should get when it is an anonymous type", () => { const { firstType } = getTypeFromText("let myType: { str: string; };"); chai_1.expect(firstType.isAnonymousType()).to.equal(true); }); it("should get when it's not an anonymous type", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.isAnonymousType()).to.equal(false); }); }); describe("isBooleanType", () => { it("should get when it is a boolean type", () => { const { firstType } = getTypeFromText("let myType: boolean;"); chai_1.expect(firstType.isBooleanType()).to.equal(true); }); it("should get when it's not a boolean type", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.isBooleanType()).to.equal(false); }); }); describe("isEnumType", () => { it("should get when it is an enum type", () => { const { firstType } = getTypeFromText("let myType: MyEnum; enum MyEnum {}"); chai_1.expect(firstType.isEnumType()).to.equal(true); }); it("should get when it's not an enum type", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.isEnumType()).to.equal(false); }); }); describe("isInterfaceType", () => { it("should get when it is an interface type", () => { const { firstType } = getTypeFromText("let myType: MyInterface; interface MyInterface {}"); chai_1.expect(firstType.isInterfaceType()).to.equal(true); }); it("should get when it's not an enum type", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.isInterfaceType()).to.equal(false); }); }); describe("isIntersectionType", () => { it("should get when it is an intersection type", () => { const { firstType } = getTypeFromText("let myType: number & string;"); chai_1.expect(firstType.isIntersectionType()).to.equal(true); }); it("should get when it's not an intersection type", () => { const { firstType } = getTypeFromText("let myType: number | string;"); chai_1.expect(firstType.isIntersectionType()).to.equal(false); }); }); describe("isObjectType", () => { it("should get when it is an object type", () => { const { firstType } = getTypeFromText("let myType: { str: string; };"); chai_1.expect(firstType.isObjectType()).to.equal(true); }); it("should get when it's not an object type", () => { const { firstType } = getTypeFromText("let myType: number;"); chai_1.expect(firstType.isObjectType()).to.equal(false); }); }); describe("isUnionType", () => { it("should get when it is a union type", () => { const { firstType } = getTypeFromText("let myType: number | string;"); chai_1.expect(firstType.isUnionType()).to.equal(true); }); it("should get when it's not a union type", () => { const { firstType } = getTypeFromText("let myType: number & string;"); chai_1.expect(firstType.isUnionType()).to.equal(false); }); }); describe("getFlags", () => { it("should get the type flags", () => { const { firstType } = getTypeFromText("let myType: number;"); chai_1.expect(firstType.getFlags()).to.equal(ts.TypeFlags.Number); }); }); describe("getObjectFlags", () => { it("should get the object flags when not an object", () => { const { firstType } = getTypeFromText("let myType: number;"); chai_1.expect(firstType.getObjectFlags()).to.equal(0); }); it("should get the object flags when an object", () => { const { firstType } = getTypeFromText("let myType: MyInterface; interface MyInterface {}"); chai_1.expect(firstType.getObjectFlags()).to.equal(ts.ObjectFlags.Interface); }); }); describe("getSymbol", () => { it("should get symbol when it has one", () => { const { firstType } = getTypeFromText("let myType: MyClass; class MyClass {}"); chai_1.expect(firstType.getSymbol().getName()).to.equal("MyClass"); }); it("should return undefined when it doesn't have one", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.getSymbol()).to.be.undefined; }); }); describe("getApparentType", () => { it("should get the apparent type", () => { const { firstType } = getTypeFromText(`const myType = 4;`); chai_1.expect(firstType.getApparentType().getText()).to.equal("Number"); }); }); describe("getCallSignatures", () => { it("should return no call signatures when none exist", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.getCallSignatures().length).to.equal(0); }); it("should return the call signatures of a type", () => { const { firstType } = getTypeFromText("let myType: () => string;"); chai_1.expect(firstType.getCallSignatures().length).to.equal(1); }); }); describe("getConstructSignatures", () => { it("should return no construct signatures when none exist", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.getConstructSignatures().length).to.equal(0); }); it("should return the construct signatures of a type", () => { const { firstType } = getTypeFromText("let myType: { new(): MyClass; };"); chai_1.expect(firstType.getConstructSignatures().length).to.equal(1); }); }); describe("getBaseTypes", () => { it("should return the base types of a type", () => { const { firstType } = getTypeFromText("let myType: MyInterface; interface MyInterface extends OtherInterface {}\ninterface OtherInterface"); const baseTypes = firstType.getBaseTypes(); chai_1.expect(baseTypes.length).to.equal(1); chai_1.expect(baseTypes[0].getText()).to.equal("OtherInterface"); }); }); describe("getStringIndexType", () => { it("should return undefined when no string index type", () => { const { firstType } = getTypeFromText("let myType: { };"); const stringIndexType = firstType.getStringIndexType(); chai_1.expect(stringIndexType).to.be.undefined; }); it("should return the string index type", () => { const { firstType } = getTypeFromText("let myType: { [index: string]: object; [index: number]: Date; };"); const stringIndexType = firstType.getStringIndexType(); chai_1.expect(stringIndexType.getText()).to.equal("object"); }); }); describe("getNumberIndexType", () => { it("should return undefined when no number index type", () => { const { firstType } = getTypeFromText("let myType: { };"); const numberIndexType = firstType.getNumberIndexType(); chai_1.expect(numberIndexType).to.be.undefined; }); it("should return the number index type", () => { const { firstType } = getTypeFromText("let myType: { [index: string]: object; [index: number]: Date; };"); const numberIndexType = firstType.getNumberIndexType(); chai_1.expect(numberIndexType.getText()).to.equal("Date"); }); }); describe("getNonNullableType", () => { it("should return the original type for a type that's already non-nullable", () => { const { firstType } = getTypeFromText("let myType: string;"); const nonNullableType = firstType.getNonNullableType(); chai_1.expect(nonNullableType.getText()).to.equal("string"); }); it("should return the non-nullable type", () => { const { firstType } = getTypeFromText("let myType: string | undefined;"); const nonNullableType = firstType.getNonNullableType(); chai_1.expect(nonNullableType.getText()).to.equal("string"); }); }); describe("getAliasSymbol", () => { it("should return undefined when not exists", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.getAliasSymbol()).to.be.undefined; }); it("should return the alias symbol when it exists", () => { const { firstType } = getTypeFromText("let myType: MyAlias; type MyAlias = {str: string;};"); chai_1.expect(firstType.getAliasSymbol().getFlags()).to.equal(ts.SymbolFlags.TypeAlias); }); }); describe("getAliasTypeArguments", () => { it("should not have any when none exist", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.getAliasTypeArguments().length).to.equal(0); }); it("should return the type args when they exist", () => { const { firstType } = getTypeFromText("let myType: MyAlias<string>; type MyAlias<T> = {str: T;};"); const typeArgs = firstType.getAliasTypeArguments(); chai_1.expect(typeArgs.length).to.equal(1); chai_1.expect(typeArgs[0].getText()).to.equal("string"); }); }); describe("isUndefinedType", () => { it("should not be undefined when not undefined", () => { const { firstType } = getTypeFromText("let myType: string;"); chai_1.expect(firstType.isUndefinedType()).to.be.false; }); it("should be undefined when not undefined", () => { const { firstType } = getTypeFromText("let myType: undefined;"); chai_1.expect(firstType.isUndefinedType()).to.be.true; }); }); }); //# sourceMappingURL=typeTests.js.map