From 5715b08c8a3aa6bbfda214c5474028f401a690e6 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 16 Feb 2018 15:32:58 -0800 Subject: [PATCH] no-unnecessary-type-assertion: Don't check `!` if --strictNullChecks is not enabled --- src/rules/noUnnecessaryTypeAssertionRule.ts | 14 +++++++++++--- .../noStrictNullChecks/test.ts.lint | 6 ++++++ .../noStrictNullChecks/tsconfig.json | 1 + .../noStrictNullChecks/tslint.json | 5 +++++ .../{ => strictNullChecks}/test.ts.fix | 0 .../{ => strictNullChecks}/test.ts.lint | 0 .../{ => strictNullChecks}/tsconfig.json | 0 .../{ => strictNullChecks}/tslint.json | 0 8 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test/rules/no-unnecessary-type-assertion/noStrictNullChecks/test.ts.lint create mode 100644 test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tsconfig.json create mode 100644 test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tslint.json rename test/rules/no-unnecessary-type-assertion/{ => strictNullChecks}/test.ts.fix (100%) rename test/rules/no-unnecessary-type-assertion/{ => strictNullChecks}/test.ts.lint (100%) rename test/rules/no-unnecessary-type-assertion/{ => strictNullChecks}/tsconfig.json (100%) rename test/rules/no-unnecessary-type-assertion/{ => strictNullChecks}/tslint.json (100%) diff --git a/src/rules/noUnnecessaryTypeAssertionRule.ts b/src/rules/noUnnecessaryTypeAssertionRule.ts index 1a08296975f..7254b6097ae 100644 --- a/src/rules/noUnnecessaryTypeAssertionRule.ts +++ b/src/rules/noUnnecessaryTypeAssertionRule.ts @@ -42,12 +42,18 @@ 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, this.ruleArguments, program.getTypeChecker())); + return this.applyWithWalker(new Walker( + sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker(), !!program.getCompilerOptions().strictNullChecks)); } } class Walker extends Lint.AbstractWalker { - constructor(sourceFile: ts.SourceFile, ruleName: string, options: string[], private readonly checker: ts.TypeChecker) { + constructor( + sourceFile: ts.SourceFile, + ruleName: string, + options: string[], + private readonly checker: ts.TypeChecker, + private readonly strictNullChecks: boolean) { super(sourceFile, ruleName, options); } @@ -55,7 +61,9 @@ class Walker extends Lint.AbstractWalker { 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: diff --git a/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/test.ts.lint b/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/test.ts.lint new file mode 100644 index 00000000000..baa6c0a6dfd --- /dev/null +++ b/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/test.ts.lint @@ -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.] diff --git a/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tsconfig.json b/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tsconfig.json new file mode 100644 index 00000000000..0967ef424bc --- /dev/null +++ b/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tsconfig.json @@ -0,0 +1 @@ +{} diff --git a/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tslint.json b/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tslint.json new file mode 100644 index 00000000000..2acf91b7b08 --- /dev/null +++ b/test/rules/no-unnecessary-type-assertion/noStrictNullChecks/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-unnecessary-type-assertion": true + } +} diff --git a/test/rules/no-unnecessary-type-assertion/test.ts.fix b/test/rules/no-unnecessary-type-assertion/strictNullChecks/test.ts.fix similarity index 100% rename from test/rules/no-unnecessary-type-assertion/test.ts.fix rename to test/rules/no-unnecessary-type-assertion/strictNullChecks/test.ts.fix diff --git a/test/rules/no-unnecessary-type-assertion/test.ts.lint b/test/rules/no-unnecessary-type-assertion/strictNullChecks/test.ts.lint similarity index 100% rename from test/rules/no-unnecessary-type-assertion/test.ts.lint rename to test/rules/no-unnecessary-type-assertion/strictNullChecks/test.ts.lint diff --git a/test/rules/no-unnecessary-type-assertion/tsconfig.json b/test/rules/no-unnecessary-type-assertion/strictNullChecks/tsconfig.json similarity index 100% rename from test/rules/no-unnecessary-type-assertion/tsconfig.json rename to test/rules/no-unnecessary-type-assertion/strictNullChecks/tsconfig.json diff --git a/test/rules/no-unnecessary-type-assertion/tslint.json b/test/rules/no-unnecessary-type-assertion/strictNullChecks/tslint.json similarity index 100% rename from test/rules/no-unnecessary-type-assertion/tslint.json rename to test/rules/no-unnecessary-type-assertion/strictNullChecks/tslint.json