Skip to content

Commit

Permalink
fix: #265 - Fix error thrown getting members in class containing semi…
Browse files Browse the repository at this point in the history
…colon terminated constructor & method declarations.
  • Loading branch information
dsherret committed Mar 1, 2018
1 parent 5e96ae6 commit dfb979f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/compiler/class/ClassDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,19 +741,28 @@ export class ClassDeclaration extends ClassDeclarationBase<ts.ClassDeclaration>
}

private getBodyMembers() {
const members = this.compilerNode.members.map(m => this.getNodeFromCompilerNode<ClassMemberTypes>(m));

// filter out the method declarations or constructor declarations without a body if not ambient
return this.isAmbient() ? members : members.filter(m => {
if (!(TypeGuards.isConstructorDeclaration(m) || TypeGuards.isMethodDeclaration(m)))
return true;
if (TypeGuards.isMethodDeclaration(m) && m.isAbstract())
return true;
return m.isImplementation();
});
return getAllBodyMembers(this).filter(m => isSupportedClassMember(m));

function getAllBodyMembers(classDec: ClassDeclaration) {
const members = classDec.compilerNode.members.map(m => classDec.getNodeFromCompilerNode<ClassMemberTypes>(m));

// filter out the method declarations or constructor declarations without a body if not ambient
return classDec.isAmbient() ? members : members.filter(m => {
if (!(TypeGuards.isConstructorDeclaration(m) || TypeGuards.isMethodDeclaration(m)))
return true;
if (TypeGuards.isMethodDeclaration(m) && m.isAbstract())
return true;
return m.isImplementation();
});
}
}
}

function isClassPropertyType(m: Node) {
return TypeGuards.isPropertyDeclaration(m) || TypeGuards.isSetAccessorDeclaration(m) || TypeGuards.isGetAccessorDeclaration(m) || TypeGuards.isParameterDeclaration(m);
}

function isSupportedClassMember(m: Node) {
return TypeGuards.isMethodDeclaration(m) || TypeGuards.isPropertyDeclaration(m) || TypeGuards.isGetAccessorDeclaration(m) || TypeGuards.isSetAccessorDeclaration(m)
|| TypeGuards.isConstructorDeclaration(m) || TypeGuards.isParameterDeclaration(m);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions src/tests/issues/265tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {expect} from "chai";
import {ClassDeclaration} from "./../../compiler";
import {getInfoFromText} from "./../compiler/testHelpers";

describe("tests for issue #265", () => {
it("should be able to handle getting static members when there exists a semicolon token", () => {
const text = `
class Identifier {
method() {};
}`;
const {firstChild, sourceFile} = getInfoFromText<ClassDeclaration>(text);
expect(() => firstChild.getStaticMembers()).to.not.throw();
});
});

0 comments on commit dfb979f

Please sign in to comment.