Skip to content

Commit

Permalink
fix: Should properly add statements to a case or default clause with …
Browse files Browse the repository at this point in the history
…a block.
  • Loading branch information
dsherret committed Jul 14, 2018
1 parent 738fb47 commit 65d96bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
15 changes: 14 additions & 1 deletion src/compiler/statement/StatementedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,20 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
insertStatements(index: number, writerFunction: WriterFunction): Statement[];
insertStatements(index: number, textOrWriterFunction: string | WriterFunction): Statement[];
insertStatements(index: number, textOrWriterFunction: string | WriterFunction) {
return this.getChildSyntaxListOrThrow().insertChildText(index, textOrWriterFunction);
return getChildSyntaxList.call(this).insertChildText(index, textOrWriterFunction);

function getChildSyntaxList(this: Node) {
const childSyntaxList = this.getChildSyntaxListOrThrow();

// case and default clauses can optionally have blocks
if (TypeGuards.isCaseClause(this) || TypeGuards.isDefaultClause(this)) {
const block = childSyntaxList.getFirstChildIfKind(SyntaxKind.Block);
if (block != null)
return block.getChildSyntaxListOrThrow();
}

return childSyntaxList;
}
}

removeStatement(index: number) {
Expand Down
34 changes: 22 additions & 12 deletions src/tests/compiler/statement/statementedNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,36 +183,46 @@ describe(nameof(StatementedNode), () => {
"function i() { var t;\n newText;var m; }");
});

const caseClause = "switch (x) {\n case 1:\n x = 0;\n y = 1;\n break;\n}";
const caseClause = "switch (x) {\n case 1:\n x = 0;\n break;\n}";
it("should insert statements at the beginning and into a case clase", () => {
doFirstChildTest<CaseClause>(caseClause, 0, "newText;\nsecondText;", 2,
"switch (x) {\n case 1:\n newText;\n secondText;\n x = 0;\n y = 1;\n break;\n}", SyntaxKind.CaseClause);
"switch (x) {\n case 1:\n newText;\n secondText;\n x = 0;\n break;\n}", SyntaxKind.CaseClause);
});

it("should insert statements in the middle and into a case clause", () => {
doFirstChildTest<CaseClause>(caseClause, 2, "newText;\nsecondText;", 2,
"switch (x) {\n case 1:\n x = 0;\n y = 1;\n newText;\n secondText;\n break;\n}", SyntaxKind.CaseClause);
doFirstChildTest<CaseClause>(caseClause, 1, "newText;\nsecondText;", 2,
"switch (x) {\n case 1:\n x = 0;\n newText;\n secondText;\n break;\n}", SyntaxKind.CaseClause);
});

it("should insert statements at the end and into a case clause", () => {
doFirstChildTest<CaseClause>(caseClause, 3, "newText;\nsecondText;", 2,
"switch (x) {\n case 1:\n x = 0;\n y = 1;\n break;\n newText;\n secondText;\n}", SyntaxKind.CaseClause);
doFirstChildTest<CaseClause>(caseClause, 2, "newText;\nsecondText;", 2,
"switch (x) {\n case 1:\n x = 0;\n break;\n newText;\n secondText;\n}", SyntaxKind.CaseClause);
});

const defaultClause = "switch (x) {\n default:\n x = 0;\n y = 1;\n break;\n}";
it("should insert into a case clause with a block", () => {
doFirstChildTest<CaseClause>("switch (x) {\n case 1: {\n }\n\n}", 0, "newText;", 1,
"switch (x) {\n case 1: {\n newText;\n }\n\n}", SyntaxKind.CaseClause);
});

const defaultClause = "switch (x) {\n default:\n x = 0;\n break;\n}";
it("should insert statements at the beginning and into a default clause", () => {
doFirstChildTest<DefaultClause>(defaultClause, 0, "newText;\nsecondText;", 2,
"switch (x) {\n default:\n newText;\n secondText;\n x = 0;\n y = 1;\n break;\n}", SyntaxKind.DefaultClause);
"switch (x) {\n default:\n newText;\n secondText;\n x = 0;\n break;\n}", SyntaxKind.DefaultClause);
});

it("should insert statements in the middle and into a default clause", () => {
doFirstChildTest<DefaultClause>(defaultClause, 2, "newText;\nsecondText;", 2,
"switch (x) {\n default:\n x = 0;\n y = 1;\n newText;\n secondText;\n break;\n}", SyntaxKind.DefaultClause);
doFirstChildTest<DefaultClause>(defaultClause, 1, "newText;\nsecondText;", 2,
"switch (x) {\n default:\n x = 0;\n newText;\n secondText;\n break;\n}", SyntaxKind.DefaultClause);
});

it("should insert statements at the end and into a default clause", () => {
doFirstChildTest<DefaultClause>(defaultClause, 3, "newText;\nsecondText;", 2,
"switch (x) {\n default:\n x = 0;\n y = 1;\n break;\n newText;\n secondText;\n}", SyntaxKind.DefaultClause);
doFirstChildTest<DefaultClause>(defaultClause, 2, "newText;\nsecondText;", 2,
"switch (x) {\n default:\n x = 0;\n break;\n newText;\n secondText;\n}", SyntaxKind.DefaultClause);
});

it("should insert into a default clause with a block", () => {
doFirstChildTest<CaseClause>("switch (x) {\n default: {\n }\n\n}", 0, "newText;", 1,
"switch (x) {\n default: {\n newText;\n }\n\n}", SyntaxKind.DefaultClause);
});

it("should insert statements in a Block", () => {
Expand Down

0 comments on commit 65d96bc

Please sign in to comment.