|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2016 Palantir Technologies, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as ts from "typescript"; |
| 19 | +import * as Lint from "tslint"; |
| 20 | + |
| 21 | +export class Rule extends Lint.Rules.TypedRule { |
| 22 | + /* tslint:disable:object-literal-sort-keys */ |
| 23 | + public static metadata: Lint.IRuleMetadata = { |
| 24 | + ruleName: "no-unnecessary-type-assertion", |
| 25 | + description: "Warns if a type assertion does not change the type of an expression.", |
| 26 | + options: { |
| 27 | + type: "list", |
| 28 | + listType: { |
| 29 | + type: "array", |
| 30 | + items: { type: "string" }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + optionsDescription: "A list of whitelisted assertion types to ignore", |
| 34 | + type: "typescript", |
| 35 | + hasFix: true, |
| 36 | + typescriptOnly: true, |
| 37 | + requiresTypeInfo: true, |
| 38 | + }; |
| 39 | + /* tslint:enable:object-literal-sort-keys */ |
| 40 | + |
| 41 | + public static FAILURE_STRING = "This assertion is unnecessary since it does not change the type of the expression."; |
| 42 | + |
| 43 | + public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { |
| 44 | + return this.applyWithWalker(new Walker(sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker())); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class Walker extends Lint.AbstractWalker<string[]> { |
| 49 | + constructor(sourceFile: ts.SourceFile, ruleName: string, options: string[], private readonly checker: ts.TypeChecker) { |
| 50 | + super(sourceFile, ruleName, options); |
| 51 | + } |
| 52 | + |
| 53 | + public walk(sourceFile: ts.SourceFile) { |
| 54 | + const cb = (node: ts.Node): void => { |
| 55 | + switch (node.kind) { |
| 56 | + case ts.SyntaxKind.TypeAssertionExpression: |
| 57 | + case ts.SyntaxKind.AsExpression: |
| 58 | + this.verifyCast(node as ts.TypeAssertion | ts.AsExpression); |
| 59 | + } |
| 60 | + |
| 61 | + return ts.forEachChild(node, cb); |
| 62 | + }; |
| 63 | + |
| 64 | + return ts.forEachChild(sourceFile, cb); |
| 65 | + } |
| 66 | + |
| 67 | + private verifyCast(node: ts.TypeAssertion | ts.NonNullExpression | ts.AsExpression) { |
| 68 | + if (ts.isAssertionExpression(node) && this.options.indexOf(node.type.getText(this.sourceFile)) !== -1) { |
| 69 | + return; |
| 70 | + } |
| 71 | + const castType = this.checker.getTypeAtLocation(node); |
| 72 | + if (castType === undefined) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (node.kind !== ts.SyntaxKind.NonNullExpression && |
| 77 | + (castType.flags & ts.TypeFlags.Literal || |
| 78 | + castType.flags & ts.TypeFlags.Object && |
| 79 | + (castType as ts.ObjectType).objectFlags & ts.ObjectFlags.Tuple) || |
| 80 | + // Sometimes tuple types don't have ObjectFlags.Tuple set, like when |
| 81 | + // they're being matched against an inferred type. So, in addition, |
| 82 | + // check if any properties are numbers, which implies that this is |
| 83 | + // likely a tuple type. |
| 84 | + (castType.getProperties().some((symbol) => !isNaN(Number(symbol.name))))) { |
| 85 | + |
| 86 | + // It's not always safe to remove a cast to a literal type or tuple |
| 87 | + // type, as those types are sometimes widened without the cast. |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const uncastType = this.checker.getTypeAtLocation(node.expression); |
| 92 | + if (uncastType === castType) { |
| 93 | + this.addFailureAtNode(node, Rule.FAILURE_STRING, node.kind === ts.SyntaxKind.TypeAssertionExpression |
| 94 | + ? Lint.Replacement.deleteFromTo(node.getStart(), node.expression.getStart()) |
| 95 | + : Lint.Replacement.deleteFromTo(node.expression.getEnd(), node.getEnd())); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments