Skip to content

Commit

Permalink
fix: #312 - StatementedNode uses inconsistent source for child count.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Apr 29, 2018
1 parent f2b01dc commit 67a22b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/compiler/statement/StatementedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
addStatements(text: string): Statement[];
addStatements(writerFunction: (writer: CodeBlockWriter) => void): Statement[];
addStatements(textOrWriterFunction: string | ((writer: CodeBlockWriter) => void)) {
const childSyntaxList = this.getChildSyntaxListOrThrow();
return this.insertStatements(childSyntaxList.getChildCount(), textOrWriterFunction);
return this.insertStatements(this.getCompilerStatements().length, textOrWriterFunction);
}

insertStatements(index: number, text: string): Statement[];
Expand All @@ -477,7 +476,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

removeStatement(index: number) {
index = verifyAndGetIndex(index, this.getStatements().length - 1);
index = verifyAndGetIndex(index, this.getCompilerStatements().length - 1);
return this.removeStatements([index, index]);
}

Expand All @@ -497,7 +496,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

addClasses(structures: ClassDeclarationStructure[]) {
return this.insertClasses(this.getChildSyntaxListOrThrow().getChildCount(), structures);
return this.insertClasses(this.getCompilerStatements().length, structures);
}

insertClass(index: number, structure: ClassDeclarationStructure) {
Expand Down Expand Up @@ -542,7 +541,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

addEnums(structures: EnumDeclarationStructure[]) {
return this.insertEnums(this.getChildSyntaxListOrThrow().getChildCount(), structures);
return this.insertEnums(this.getCompilerStatements().length, structures);
}

insertEnum(index: number, structure: EnumDeclarationStructure) {
Expand Down Expand Up @@ -587,7 +586,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

addFunctions(structures: FunctionDeclarationStructure[]) {
return this.insertFunctions(this.getChildSyntaxListOrThrow().getChildCount(), structures);
return this.insertFunctions(this.getCompilerStatements().length, structures);
}

insertFunction(index: number, structure: FunctionDeclarationStructure) {
Expand Down Expand Up @@ -638,7 +637,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

addInterfaces(structures: InterfaceDeclarationStructure[]) {
return this.insertInterfaces(this.getChildSyntaxListOrThrow().getChildCount(), structures);
return this.insertInterfaces(this.getCompilerStatements().length, structures);
}

insertInterface(index: number, structure: InterfaceDeclarationStructure) {
Expand Down Expand Up @@ -683,7 +682,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

addNamespaces(structures: NamespaceDeclarationStructure[]) {
return this.insertNamespaces(this.getChildSyntaxListOrThrow().getChildCount(), structures);
return this.insertNamespaces(this.getCompilerStatements().length, structures);
}

insertNamespace(index: number, structure: NamespaceDeclarationStructure) {
Expand Down Expand Up @@ -727,7 +726,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

addTypeAliases(structures: TypeAliasDeclarationStructure[]) {
return this.insertTypeAliases(this.getChildSyntaxListOrThrow().getChildCount(), structures);
return this.insertTypeAliases(this.getCompilerStatements().length, structures);
}

insertTypeAlias(index: number, structure: TypeAliasDeclarationStructure) {
Expand Down Expand Up @@ -787,7 +786,7 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
}

addVariableStatements(structures: VariableStatementStructure[]) {
return this.insertVariableStatements(this.getChildSyntaxListOrThrow().getChildCount(), structures);
return this.insertVariableStatements(this.getCompilerStatements().length, structures);
}

insertVariableStatement(index: number, structure: VariableStatementStructure) {
Expand Down
18 changes: 18 additions & 0 deletions src/tests/issues/312tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from "chai";
import { ClassDeclaration } from "../../compiler";
import { getInfoFromText } from "../compiler/testHelpers";

describe("tests for issue #312", () => {
it("should still correctly add a function when the syntax is wrong like this", () => {
const text = `
export type compilerVersions = "2.8.1" | "2.6.2" | "2.7.2";
export enum CompilerVersion {
typescript = "2.8.1",
typescript-2.6.2 = "2.6.2",
typescript-2.7.2 = "2.7.2"
}`;
const { sourceFile } = getInfoFromText(text);
expect(() => sourceFile.addFunction({ name: "func" })).to.not.throw();
});
});

0 comments on commit 67a22b9

Please sign in to comment.