forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: add tslint rule to ensure that setters are declared after getters
Based on angular#11483 (comment). Adds a custom tslint rule to enforce that we put getters before setters.
- Loading branch information
Showing
7 changed files
with
39 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint'; | ||
import * as tsutils from 'tsutils'; | ||
|
||
/** | ||
* Rule that enforces that property setters are declared after getters. | ||
*/ | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class Walker extends Lint.RuleWalker { | ||
visitGetAccessor(getter: ts.GetAccessorDeclaration) { | ||
if (getter.parent && tsutils.isClassDeclaration(getter.parent)) { | ||
const getterName = getter.name.getText(); | ||
const setter = getter.parent.members.find(member => { | ||
return tsutils.isSetAccessorDeclaration(member) && member.name.getText() === getterName; | ||
}) as ts.SetAccessorDeclaration | undefined; | ||
|
||
if (setter && setter.pos < getter.pos) { | ||
this.addFailureAtNode(setter, 'Setters must be declared after getters.'); | ||
} | ||
} | ||
|
||
super.visitGetAccessor(getter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters