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

Commit

Permalink
no-unnecessary-type-assertion: Don't check ! if --strictNullChecks …
Browse files Browse the repository at this point in the history
…is not enabled (#3724)
  • Loading branch information
Andy Hanson authored and Josh Goldberg committed Jun 15, 2019
1 parent 484d429 commit b836f28
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/rules/noUnnecessaryTypeAssertionRule.ts
Expand Up @@ -45,7 +45,13 @@ export class Rule extends Lint.Rules.TypedRule {

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithWalker(
new Walker(sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker()),
new Walker(
sourceFile,
this.ruleName,
this.ruleArguments,
program.getTypeChecker(),
!!program.getCompilerOptions().strictNullChecks,
),
);
}
}
Expand All @@ -56,6 +62,7 @@ class Walker extends Lint.AbstractWalker<string[]> {
ruleName: string,
options: string[],
private readonly checker: ts.TypeChecker,
private readonly strictNullChecks: boolean,
) {
super(sourceFile, ruleName, options);
}
Expand All @@ -64,7 +71,9 @@ class Walker extends Lint.AbstractWalker<string[]> {
const cb = (node: ts.Node): void => {
switch (node.kind) {
case ts.SyntaxKind.NonNullExpression:
this.checkNonNullAssertion(node as ts.NonNullExpression);
if (this.strictNullChecks) {
this.checkNonNullAssertion(node as ts.NonNullExpression);
}
break;
case ts.SyntaxKind.TypeAssertionExpression:
case ts.SyntaxKind.AsExpression:
Expand Down
@@ -0,0 +1,6 @@
declare const x: string | undefined;
x!;

declare const y: string;
y as string;
~~~~~~~~~~~ [This assertion is unnecessary since it does not change the type of the expression.]
@@ -0,0 +1 @@
{}
@@ -0,0 +1,5 @@
{
"rules": {
"no-unnecessary-type-assertion": true
}
}

0 comments on commit b836f28

Please sign in to comment.