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

[no-unnecessary-type-assertion]: adds an option to ignore whitelisted type assertions. #3257

Merged
merged 1 commit into from Oct 6, 2017

Conversation

bowenni
Copy link
Contributor

@bowenni bowenni commented Sep 28, 2017

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

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:

@palantirtech
Copy link
Member

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) {
Copy link
Contributor

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) {
Copy link
Contributor

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()));
Copy link
Contributor

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[]

@ajafff ajafff merged commit c55e3c8 into palantir:master Oct 6, 2017
@ajafff
Copy link
Contributor

ajafff commented Oct 6, 2017

Thanks @bowenni

@bowenni bowenni deleted the options branch October 6, 2017 22:57
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants