Skip to content

Commit

Permalink
feat: #204 - Add WithStatement.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 10, 2018
1 parent 56b9bc4 commit 215383a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/compiler/statement/WithStatement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as ts from "typescript";
import {removeStatementedNodeChild} from "./../../manipulation";
import {ChildOrderableNode} from "./../base";
import {Expression} from "./../common";
import {Statement} from "./Statement";

export const WithStatementBase = ChildOrderableNode(Statement);
export class WithStatement extends WithStatementBase<ts.WithStatement> {
/**
* Gets this with statement's expression.
*/
getExpression() {
return this.getNodeFromCompilerNode(this.compilerNode.expression) as Expression;
}

/**
* Gets this with statement's statement.
*/
getStatement() {
return this.getNodeFromCompilerNode(this.compilerNode.statement) as Statement;
}

/**
* Removes this with statement.
*/
remove() {
removeStatementedNodeChild(this);
}
}
1 change: 1 addition & 0 deletions src/compiler/statement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from "./VariableDeclarationList";
export * from "./VariableDeclarationType";
export * from "./VariableStatement";
export * from "./WhileStatement";
export * from "./WithStatement";
1 change: 1 addition & 0 deletions src/factories/nodeToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const nodeToWrapperMappings: { [key: number]: any } = {
[ts.SyntaxKind.JSDocPropertyTag]: compiler.JSDocPropertyTag,
[ts.SyntaxKind.SemicolonToken]: compiler.Node,
[ts.SyntaxKind.WhileStatement]: compiler.WhileStatement,
[ts.SyntaxKind.WithStatement]: compiler.WithStatement,
// literals
[ts.SyntaxKind.AnyKeyword]: compiler.Expression,
[ts.SyntaxKind.BooleanKeyword]: compiler.Expression,
Expand Down
36 changes: 36 additions & 0 deletions src/tests/compiler/statement/withStatementTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as ts from "typescript";
import {expect} from "chai";
import {WithStatement} from "./../../../compiler";
import {getInfoFromText} from "./../testHelpers";

function getInfoFromTextWithForInStatement(text: string) {
const obj = getInfoFromText(text);
const withStatement = (
obj.sourceFile.getFirstDescendantByKindOrThrow(ts.SyntaxKind.WithStatement)
) as WithStatement;
return {...obj, withStatement};
}

describe(nameof(WithStatement), () => {
describe(nameof<WithStatement>(n => n.getExpression), () => {
function doTest(text: string, expectedText: string) {
const {withStatement} = getInfoFromTextWithForInStatement(text);
expect(withStatement.getExpression().getText()).to.equal(expectedText);
}

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

describe(nameof<WithStatement>(n => n.getStatement), () => {
function doTest(text: string, expectedText: string) {
const {withStatement} = getInfoFromTextWithForInStatement(text);
expect(withStatement.getStatement().getText()).to.equal(expectedText);
}

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

0 comments on commit 215383a

Please sign in to comment.