Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Adding class expression checks to max-classes-per-file rule #1666

Merged
merged 1 commit into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/rules/maxClassesPerFileRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,20 @@ class MaxClassesPerFileWalker extends Lint.RuleWalker {
}

public visitClassDeclaration(node: ts.ClassDeclaration) {
this.increaseClassCount(node);
super.visitClassDeclaration(node);
}

public visitClassExpression(node: ts.ClassExpression) {
this.increaseClassCount(node);
super.visitClassExpression(node);
}

private increaseClassCount(node: ts.ClassExpression | ts.ClassDeclaration) {
this.classCount++;
if (this.classCount > this.maxClassCount) {
const msg = Rule.FAILURE_STRING_FACTORY(this.maxClassCount);
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), msg));
}
super.visitClassDeclaration(node);
}
}
7 changes: 7 additions & 0 deletions test/rules/max-classes-per-file/one/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ class two {
public theAbovePropertyMakesSense = false;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
~ [A maximum of 1 class per file is allowed]

let three = class {
~~~~~~~
public foo = "bar";
~~~~~~~~~~~~~~~~~~~~~~~
}
~ [A maximum of 1 class per file is allowed]
4 changes: 2 additions & 2 deletions test/rules/max-classes-per-file/two/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class two {
public foo = "bar";
}

class three {
~~~~~~~~~~~~
let three = class {
~~~~~~~
public foo = "bar";
~~~~~~~~~~~~~~~~~~~~~~~
}
Expand Down