Skip to content

Commit

Permalink
feat: #187 - Added TypeGuards.hasExpression.
Browse files Browse the repository at this point in the history
This can be used to tell if a node has a .getExpression function or not.
  • Loading branch information
dsherret committed Jan 14, 2018
1 parent 7d99523 commit 6e37480
Show file tree
Hide file tree
Showing 3 changed files with 2,651 additions and 2,592 deletions.
19 changes: 17 additions & 2 deletions code-generation/createTypeGuardsUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function createTypeGuardsUtility(ast: TsSimpleAst, classVMs: ClassViewMod
docs: [{
description: "Type guards for checking the type of a node."
}],
methods: getMethodInfos().map(method => ({
methods: [...getMethodInfos().map(method => ({
name: `is${method.name}`,
isStatic: true,
docs: [{
Expand All @@ -55,7 +55,7 @@ export function createTypeGuardsUtility(ast: TsSimpleAst, classVMs: ClassViewMod
writer.writeLine("default:")
.indent().write("return false;").newLine();
})
}))
})), getHasExpressionTypeGuard()]
});

file.save();
Expand Down Expand Up @@ -131,6 +131,21 @@ export function createTypeGuardsUtility(ast: TsSimpleAst, classVMs: ClassViewMod
return nodeToWrapperVM.syntaxKindNames;
}
}

function getHasExpressionTypeGuard(): MethodDeclarationStructure {
return {
docs: [{ description: "Gets if the node has an expression.\r\n@param node - Node to check." }],
isStatic: true,
name: "hasExpression",
returnType: "node is compiler.Node & { getExpression(): compiler.Expression; }",
parameters: [{ name: "node", type: "compiler.Node" }],
bodyText: writer => {
writer.writeLine("if ((node as any).getExpression == null)");
writer.indent().write("return false;");
writer.writeLine("return (node as any).getExpression() != null;");
}
};
}
}

function isAllowedClass(classVM: ClassViewModel) {
Expand Down
34 changes: 34 additions & 0 deletions src/tests/utils/typeGuardsTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as ts from "typescript";
import {expect} from "chai";
import {TypeGuards} from "./../../utils";
import {getInfoFromText} from "./../compiler/testHelpers";

describe(nameof(TypeGuards), () => {
// most of the code in TypeGuards is not worth the effort to test... it's auto generated from code so it should be close to correct

describe(nameof(TypeGuards.hasExpression), () => {
it("should have an expression when it's a function call", () => {
const {firstChild} = getInfoFromText("funcCall()");
expect(TypeGuards.hasExpression(firstChild)).to.be.true;
if (TypeGuards.hasExpression(firstChild))
expect(firstChild.getExpression().getText()).to.equal("funcCall()");
});

it("should not have an expression when it doesn't", () => {
const {firstChild} = getInfoFromText("class Test {}");
expect(TypeGuards.hasExpression(firstChild)).to.be.false;
});

it("should have an expression when it's a return statement with a value", () => {
const {firstChild} = getInfoFromText("return 5;");
expect(TypeGuards.hasExpression(firstChild)).to.be.true;
if (TypeGuards.hasExpression(firstChild))
expect(firstChild.getExpression().getText()).to.equal("5");
});

it("should not have an expression when it's a return statement without a value", () => {
const {firstChild} = getInfoFromText("return;");
expect(TypeGuards.hasExpression(firstChild)).to.be.false;
});
});
});

0 comments on commit 6e37480

Please sign in to comment.