-
Notifications
You must be signed in to change notification settings - Fork 885
[no-unnecessary-type-assertion]: adds an option to ignore whitelisted type assertions. #3257
Conversation
Thanks for your interest in palantir/tslint, @bowenni! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
@@ -61,7 +67,10 @@ class Walker extends Lint.AbstractWalker<void> { | |||
} | |||
|
|||
private verifyCast(node: ts.TypeAssertion | ts.NonNullExpression | ts.AsExpression) { | |||
const castType = this.checker.getTypeAtLocation(node); | |||
if (isAsExpression(node) && this.options.ruleArguments.indexOf(node.type.getText()) !== -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use isAssertionExpression
to also handle <T>foo
@@ -61,6 +67,9 @@ class Walker extends Lint.AbstractWalker<void> { | |||
} | |||
|
|||
private verifyCast(node: ts.TypeAssertion | ts.NonNullExpression | ts.AsExpression) { | |||
if (isAssertionExpression(node) && this.options.ruleArguments.indexOf(node.type.getText()) !== -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please pass the sourceFile
as parameter to .getText()
to avoid unnecessary lookup of the SourceFile
@@ -36,13 +42,13 @@ export class Rule extends Lint.Rules.TypedRule { | |||
public static FAILURE_STRING = "This assertion is unnecessary since it does not change the type of the expression."; | |||
|
|||
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | |||
return this.applyWithWalker(new Walker(sourceFile, this.ruleName, program.getTypeChecker())); | |||
return this.applyWithWalker(new Walker(sourceFile, this.ruleName, this.getOptions(), program.getTypeChecker())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use this.ruleArguments
instead of this.getOptions()
you just need to change the type argument from Lint.IOptions
to string[]
whitelisted type assertions.
Thanks @bowenni |
PR checklist
Overview of change:
In Google we use type aliases for the
any
type to remind the developers that the type is loose and they need to fix it. This option allows "no-unnecessary-type-assertion" to ignore those explicitly added assertions.Is there anything you'd like reviewers to focus on?
CHANGELOG.md entry: