Skip to content

Commit

Permalink
feat: #164 - Support ExpressionStatements.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 10, 2017
1 parent cae9bb6 commit d7d48a1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/compiler/statement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./statement/Block";
export * from "./statement/StatementedNode";
export * from "./statement/ExpressionStatement";
export * from "./statement/VariableDeclaration";
export * from "./statement/VariableStatement";
export * from "./statement/VariableDeclarationType";
21 changes: 21 additions & 0 deletions src/compiler/statement/ExpressionStatement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as ts from "typescript";
import {removeStatementedNodeChild} from "./../../manipulation";
import {Node, Expression} from "./../common";
import {ChildOrderableNode} from "./../base";

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

/**
* Removes this expression statement.
*/
remove() {
removeStatementedNodeChild(this);
}
}
1 change: 1 addition & 0 deletions src/factories/nodeToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const nodeToWrapperMappings: { [key: number]: any } = {
[ts.SyntaxKind.ExportDeclaration]: compiler.ExportDeclaration,
[ts.SyntaxKind.ExportSpecifier]: compiler.ExportSpecifier,
[ts.SyntaxKind.ExpressionWithTypeArguments]: compiler.ExpressionWithTypeArguments,
[ts.SyntaxKind.ExpressionStatement]: compiler.ExpressionStatement,
[ts.SyntaxKind.FunctionDeclaration]: compiler.FunctionDeclaration,
[ts.SyntaxKind.FunctionExpression]: compiler.FunctionExpression,
[ts.SyntaxKind.GetAccessor]: compiler.GetAccessorDeclaration,
Expand Down
25 changes: 25 additions & 0 deletions src/tests/compiler/statement/expressionStatementTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {expect} from "chai";
import {ExpressionStatement} from "./../../../compiler";
import {getInfoFromText} from "./../testHelpers";

describe(nameof(ExpressionStatement), () => {
describe(nameof<ExpressionStatement>(d => d.getExpression), () => {
it("should get the expression", () => {
const {firstChild} = getInfoFromText<ExpressionStatement>("hello();");
expect(firstChild.getText()).to.equal("hello();");
expect(firstChild.getExpression().getText()).to.equal("hello()");
});
});

describe(nameof<ExpressionStatement>(d => d.remove), () => {
function doTest(text: string, index: number, expectedText: string) {
const {sourceFile} = getInfoFromText(text);
(sourceFile.getChildSyntaxListOrThrow().getChildren()[index] as ExpressionStatement).remove();
expect(sourceFile.getFullText()).to.equal(expectedText);
}

it("should remove the expression statement", () => {
doTest("hello();\nthere();\ntest();", 1, "hello();\ntest();");
});
});
});

0 comments on commit d7d48a1

Please sign in to comment.