-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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 disallow private setters (#12706)
Adds a tslint rule to prevent people from adding private getters since they don't contribute to the public API and they generate more ES5 code.
- Loading branch information
Showing
7 changed files
with
59 additions
and
19 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,39 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint'; | ||
import * as tsutils from 'tsutils'; | ||
|
||
/** | ||
* Rule that doesn't allow private 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) { | ||
// Check whether the getter is private. | ||
if (!getter.modifiers || !getter.modifiers.find(m => m.kind === ts.SyntaxKind.PrivateKeyword)) { | ||
return super.visitGetAccessor(getter); | ||
} | ||
|
||
// Check that it's inside a class. | ||
if (!getter.parent || !tsutils.isClassDeclaration(getter.parent)) { | ||
return super.visitGetAccessor(getter); | ||
} | ||
|
||
const getterName = getter.name.getText(); | ||
const setter = getter.parent.members.find(member => { | ||
return tsutils.isSetAccessorDeclaration(member) && member.name.getText() === getterName; | ||
}) as ts.SetAccessorDeclaration | undefined; | ||
|
||
// Only log a failure if it doesn't have a corresponding setter. | ||
if (!setter) { | ||
this.addFailureAtNode(getter, 'Private setters generate unnecessary ' + | ||
'code. Use a function instead.'); | ||
} | ||
|
||
return 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