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

Commit

Permalink
adds an option to "no-unnecessary-type-assertion" rule to ignore (#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenni authored and ajafff committed Oct 6, 2017
1 parent cef6ec5 commit c55e3c8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/rules/noUnnecessaryTypeAssertionRule.ts
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { isObjectFlagSet, isObjectType, isTypeFlagSet } from "tsutils";
import { isAssertionExpression, isObjectFlagSet, isObjectType, isTypeFlagSet } from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";

Expand All @@ -24,8 +24,14 @@ export class Rule extends Lint.Rules.TypedRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-unnecessary-type-assertion",
description: "Warns if a type assertion does not change the type of an expression.",
options: null,
optionsDescription: "Not configurable",
options: {
type: "list",
listType: {
type: "array",
items: {type: "string"},
},
},
optionsDescription: "A list of whitelisted assertion types to ignore",
type: "typescript",
hasFix: true,
typescriptOnly: true,
Expand All @@ -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.ruleArguments, program.getTypeChecker()));
}
}

class Walker extends Lint.AbstractWalker<void> {
constructor(sourceFile: ts.SourceFile, ruleName: string, private readonly checker: ts.TypeChecker) {
super(sourceFile, ruleName, undefined);
class Walker extends Lint.AbstractWalker<string[]> {
constructor(sourceFile: ts.SourceFile, ruleName: string, options: string[], private readonly checker: ts.TypeChecker) {
super(sourceFile, ruleName, options);
}

public walk(sourceFile: ts.SourceFile) {
Expand All @@ -61,6 +67,9 @@ class Walker extends Lint.AbstractWalker<void> {
}

private verifyCast(node: ts.TypeAssertion | ts.NonNullExpression | ts.AsExpression) {
if (isAssertionExpression(node) && this.options.indexOf(node.type.getText(this.sourceFile)) !== -1) {
return;
}
const castType = this.checker.getTypeAtLocation(node);
if (castType === undefined) {
return;
Expand Down
3 changes: 3 additions & 0 deletions test/rules/no-unnecessary-type-assertion/test.ts.fix
@@ -1,6 +1,8 @@
const nonNullStringLiteral: 'test';
const nonNullString: string;
const nullableString: string|undefined;
let anyType: any;
type AnyDuringMigration = any;

// non-null
let a = nonNullStringLiteral;
Expand Down Expand Up @@ -41,6 +43,7 @@ let q = value1;
let r = <Iface2>value1;
let s = value2;
let t = value2 as Iface1;
let aa = anyType as AnyDuringMigration;

interface TypeA {
kind: 'a';
Expand Down
3 changes: 3 additions & 0 deletions test/rules/no-unnecessary-type-assertion/test.ts.lint
@@ -1,6 +1,8 @@
const nonNullStringLiteral: 'test';
const nonNullString: string;
const nullableString: string|undefined;
let anyType: any;
type AnyDuringMigration = any;

// non-null
let a = nonNullStringLiteral!;
Expand Down Expand Up @@ -54,6 +56,7 @@ let r = <Iface2>value1;
let s = value2 as Iface2;
~~~~~~~~~~~~~~~~ [This assertion is unnecessary since it does not change the type of the expression.]
let t = value2 as Iface1;
let aa = anyType as AnyDuringMigration;

interface TypeA {
kind: 'a';
Expand Down
2 changes: 1 addition & 1 deletion test/rules/no-unnecessary-type-assertion/tslint.json
@@ -1,5 +1,5 @@
{
"rules": {
"no-unnecessary-type-assertion": true
"no-unnecessary-type-assertion": [true, "AnyDuringMigration"]
}
}

0 comments on commit c55e3c8

Please sign in to comment.