Skip to content

Commit

Permalink
Fix member ordering bug (palantir#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkillian authored and adidahiya committed May 14, 2016
1 parent 70a8a1f commit dc439c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/language/walker/syntaxWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class SyntaxWalker {
this.walkChildren(node);
}

protected visitClassExpression(node: ts.ClassExpression) {
this.walkChildren(node);
}

protected visitCatchClause(node: ts.CatchClause) {
this.walkChildren(node);
}
Expand Down Expand Up @@ -352,6 +356,10 @@ export class SyntaxWalker {
this.visitClassDeclaration(<ts.ClassDeclaration> node);
break;

case ts.SyntaxKind.ClassExpression:
this.visitClassExpression(<ts.ClassExpression> node);
break;

case ts.SyntaxKind.CatchClause:
this.visitCatchClause(<ts.CatchClause> node);
break;
Expand Down
24 changes: 18 additions & 6 deletions src/rules/memberOrderingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function getNodeOption({accessLevel, isConstructor, kind, membership}: INodeAndM
export class MemberOrderingWalker extends Lint.RuleWalker {
private previousMember: IModifiers;
private memberStack: INodeAndModifiers[][] = [];
private hasOrderOption = this.getHasOrderOption();

public visitClassDeclaration(node: ts.ClassDeclaration) {
this.resetPreviousModifiers();
Expand All @@ -171,6 +172,14 @@ export class MemberOrderingWalker extends Lint.RuleWalker {
this.checkMemberOrder();
}

public visitClassExpression(node: ts.ClassExpression) {
this.resetPreviousModifiers();

this.newMemberList();
super.visitClassExpression(node);
this.checkMemberOrder();
}

public visitInterfaceDeclaration(node: ts.InterfaceDeclaration) {
this.resetPreviousModifiers();

Expand Down Expand Up @@ -265,16 +274,20 @@ export class MemberOrderingWalker extends Lint.RuleWalker {

/* start new code */
private newMemberList() {
this.memberStack.push([]);
if (this.hasOrderOption) {
this.memberStack.push([]);
}
}

private pushMember(node: INodeAndModifiers) {
this.memberStack[this.memberStack.length - 1].push(node);
if (this.hasOrderOption) {
this.memberStack[this.memberStack.length - 1].push(node);
}
}

private checkMemberOrder() {
const memberList = this.memberStack.pop();
if (this.hasOrderOption()) {
if (this.hasOrderOption) {
const memberList = this.memberStack.pop();
const order = this.getOrder();
const memberRank = memberList.map((n) => order.indexOf(getNodeOption(n)));

Expand Down Expand Up @@ -307,11 +320,10 @@ export class MemberOrderingWalker extends Lint.RuleWalker {
prevRank = rank;
}
});

}
}

private hasOrderOption() {
private getHasOrderOption() {
const allOptions = this.getOptions();
if (allOptions == null || allOptions.length === 0) {
return false;
Expand Down
7 changes: 7 additions & 0 deletions test/rules/member-ordering/order/custom/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ class AlsoOkay {
someMethod() {}
};
}

private z = 10;
}

const foo = {
// TS treats this as a method, but we should be careful not to
someMethod() {}
};

function makeAClass() {
const myClass = class {
method(){}
};
}

0 comments on commit dc439c4

Please sign in to comment.