diff --git a/src/language/walker/syntaxWalker.ts b/src/language/walker/syntaxWalker.ts index ef4049446e7..f9e1e46fc73 100644 --- a/src/language/walker/syntaxWalker.ts +++ b/src/language/walker/syntaxWalker.ts @@ -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); } @@ -352,6 +356,10 @@ export class SyntaxWalker { this.visitClassDeclaration( node); break; + case ts.SyntaxKind.ClassExpression: + this.visitClassExpression( node); + break; + case ts.SyntaxKind.CatchClause: this.visitCatchClause( node); break; diff --git a/src/rules/memberOrderingRule.ts b/src/rules/memberOrderingRule.ts index d14222992f6..d5d94e3286e 100644 --- a/src/rules/memberOrderingRule.ts +++ b/src/rules/memberOrderingRule.ts @@ -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(); @@ -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(); @@ -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))); @@ -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; diff --git a/test/rules/member-ordering/order/custom/test.ts.lint b/test/rules/member-ordering/order/custom/test.ts.lint index c549da9d9e1..6a4f8d749e2 100644 --- a/test/rules/member-ordering/order/custom/test.ts.lint +++ b/test/rules/member-ordering/order/custom/test.ts.lint @@ -20,6 +20,7 @@ class AlsoOkay { someMethod() {} }; } + private z = 10; } @@ -27,3 +28,9 @@ const foo = { // TS treats this as a method, but we should be careful not to someMethod() {} }; + +function makeAClass() { + const myClass = class { + method(){} + }; +}