Skip to content

Commit

Permalink
feat: #204 - Add IfStatement.
Browse files Browse the repository at this point in the history
  • Loading branch information
dicarlo2 committed Jan 8, 2018
1 parent b3494a2 commit 095eb24
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/compiler/statement/Block.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as ts from "typescript";
import {Node} from "./../common";
import {TextInsertableNode} from "./../base";
import {Statement} from "./Statement";
import {StatementedNode} from "./StatementedNode";

export const BlockBase = TextInsertableNode(StatementedNode(Node));
export const BlockBase = TextInsertableNode(StatementedNode(Statement));
export class Block extends BlockBase<ts.Block> {
}
3 changes: 2 additions & 1 deletion src/compiler/statement/ExpressionStatement.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as ts from "typescript";
import {removeStatementedNodeChild} from "./../../manipulation";
import {Node, Expression} from "./../common";
import {Statement} from "./Statement"
import {ChildOrderableNode} from "./../base";

export const ExpressionStatementBase = ChildOrderableNode(Node);
export const ExpressionStatementBase = ChildOrderableNode(Statement);
export class ExpressionStatement extends ExpressionStatementBase<ts.ExpressionStatement> {
/**
* Gets this expression statement's expression.
Expand Down
38 changes: 38 additions & 0 deletions src/compiler/statement/IfStatement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as ts from "typescript";
import {removeStatementedNodeChild} from "./../../manipulation";
import {Node, Expression} from "./../common";
import {ChildOrderableNode} from "./../base";
import {Statement} from "./Statement";

export const IfStatementBase = ChildOrderableNode(Statement);
export class IfStatement extends IfStatementBase<ts.IfStatement> {
/**
* Gets this if statement's expression.
*/
getExpression() {
return this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.expression, this.sourceFile) as Expression;
}

/**
* Gets this if statement's then statement.
*/
getThenStatement() {
return this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.thenStatement, this.sourceFile) as Statement;
}

/**
* Gets this if statement's else statement.
*/
getElseStatement() {
return this.compilerNode.elseStatement == null
? undefined
: this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.elseStatement, this.sourceFile) as Statement;
}

/**
* Removes this expression statement.
*/
remove() {
removeStatementedNodeChild(this);
}
}
3 changes: 2 additions & 1 deletion src/compiler/statement/ReturnStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import * as errors from "./../../errors";
import {removeStatementedNodeChild} from "./../../manipulation";
import {Node, Expression} from "./../common";
import {ChildOrderableNode} from "./../base";
import {Statement} from "./Statement";

export const ReturnStatementBase = ChildOrderableNode(Node);
export const ReturnStatementBase = ChildOrderableNode(Statement);
export class ReturnStatement extends ReturnStatementBase<ts.ReturnStatement> {
/**
* Gets this return statement's expression if it exists or throws.
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/statement/Statement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as ts from "typescript";
import {Type} from "./../type";
import {Node} from "../common";

export class Statement<T extends ts.Statement = ts.Statement> extends Node<T> {
}
3 changes: 2 additions & 1 deletion src/compiler/statement/VariableStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {NamespaceChildableNode} from "./../namespace";
import {callBaseFill} from "./../callBaseFill";
import {VariableDeclaration} from "./VariableDeclaration";
import {VariableDeclarationType} from "./VariableDeclarationType";
import {Statement} from "./Statement";

export const VariableStatementBase = ChildOrderableNode(NamespaceChildableNode(JSDocableNode(AmbientableNode(ExportableNode(ModifierableNode(Node))))));
export const VariableStatementBase = ChildOrderableNode(NamespaceChildableNode(JSDocableNode(AmbientableNode(ExportableNode(ModifierableNode(Statement))))));
export class VariableStatement extends VariableStatementBase<ts.VariableStatement> {
/**
* Get the variable declarations.
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/statement/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from "./Block";
export * from "./ExpressionStatement";
export * from "./IfStatement";
export * from "./ReturnStatement";
export * from "./Statement";
export * from "./StatementedNode";
export * from "./VariableDeclaration";
export * from "./VariableDeclarationType";
Expand Down
1 change: 1 addition & 0 deletions src/factories/nodeToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const nodeToWrapperMappings: { [key: number]: any } = {
[ts.SyntaxKind.GetAccessor]: compiler.GetAccessorDeclaration,
[ts.SyntaxKind.HeritageClause]: compiler.HeritageClause,
[ts.SyntaxKind.Identifier]: compiler.Identifier,
[ts.SyntaxKind.IfStatement]: compiler.IfStatement,
[ts.SyntaxKind.ImportDeclaration]: compiler.ImportDeclaration,
[ts.SyntaxKind.ImportSpecifier]: compiler.ImportSpecifier,
[ts.SyntaxKind.InterfaceDeclaration]: compiler.InterfaceDeclaration,
Expand Down
56 changes: 56 additions & 0 deletions src/tests/compiler/statement/ifStatementTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as ts from "typescript";
import {expect} from "chai";
import {IfStatement} from "./../../../compiler";
import {getInfoFromText} from "./../testHelpers";

function getInfoFromTextWithIfStatement(text: string) {
const obj = getInfoFromText(text);
const ifStatement = (
obj.sourceFile.getFirstDescendantByKindOrThrow(ts.SyntaxKind.IfStatement)
) as IfStatement;
return {...obj, ifStatement};
}

describe(nameof(IfStatement), () => {
const expression = "1 + 2 === 3";
const thenStatement = "{ x = 1; }";
const elseStatement = "{ x = 2; }";
const statement = `if (${expression}) ${thenStatement} else ${elseStatement}`;
describe(nameof<IfStatement>(n => n.getExpression), () => {
function doTest(text: string, expectedText: string) {
const {ifStatement} = getInfoFromTextWithIfStatement(text);
expect(ifStatement.getExpression().getText()).to.equal(expectedText);
}

it("should get the correct expression", () => {
doTest(statement, expression);
});
});

describe(nameof<IfStatement>(n => n.getThenStatement), () => {
function doTest(text: string, expectedText: string) {
const {ifStatement} = getInfoFromTextWithIfStatement(text);
expect(ifStatement.getThenStatement().getText()).to.equal(expectedText);
}

it("should get the correct then statement", () => {
doTest(statement, thenStatement);
});
});

describe(nameof<IfStatement>(n => n.getElseStatement), () => {
function doTest(text: string, expectedText: string | null) {
const {ifStatement} = getInfoFromTextWithIfStatement(text);
const value = ifStatement.getElseStatement();
expect(value == null ? value : value.getText()).to.equal(expectedText);
}

it("should get the correct else statement", () => {
doTest(statement, elseStatement);
});

it("should get the correct null else statement", () => {
doTest("if (x) { x = 2 }", null);
});
});
});
8 changes: 8 additions & 0 deletions src/tests/compiler/statement/statementTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as ts from "typescript";
import {expect} from "chai";
import {Statement, ClassDeclaration} from "./../../../compiler";
import {getInfoFromText} from "./../testHelpers";

describe(nameof(Statement), () => {
// todo: tests
});
22 changes: 18 additions & 4 deletions src/utils/TypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

/* tslint:disable */
import * as ts from "typescript";
import * as compiler from "./../compiler";

import * as compiler from "./../compiler";

/**
* Type guards for checking the type of a node.
*/
Expand Down Expand Up @@ -209,6 +209,7 @@ export class TypeGuards {
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.ModuleDeclaration:
case ts.SyntaxKind.ExpressionStatement:
case ts.SyntaxKind.IfStatement:
case ts.SyntaxKind.ReturnStatement:
case ts.SyntaxKind.VariableStatement:
case ts.SyntaxKind.TypeAliasDeclaration:
Expand Down Expand Up @@ -573,6 +574,19 @@ export class TypeGuards {
}
}

/**
* Gets if the node is a IfStatement.
* @param node - Node to check.
*/
static isIfStatement(node: compiler.Node): node is compiler.IfStatement {
switch (node.getKind()) {
case ts.SyntaxKind.IfStatement:
return true;
default:
return false;
}
}

/**
* Gets if the node is a ImplementsClauseableNode.
* @param node - Node to check.
Expand Down Expand Up @@ -1546,5 +1560,5 @@ export class TypeGuards {
default:
return false;
}
}
}
}
}

0 comments on commit 095eb24

Please sign in to comment.