From c69f4b7132937f112d497213abb0b3fb69d85ae9 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 9 Jun 2019 12:57:23 -0700 Subject: [PATCH] fix(eslint-plugin): [no-unnecessary-type-assertion] handle missing declarations (#537) --- .../rules/no-unnecessary-type-assertion.ts | 5 +++ packages/eslint-plugin/src/util/types.ts | 13 +++++- .../no-unnecessary-type-assertion.test.ts | 40 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts index 54bb2d6cc92..6dd69974ca2 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts @@ -134,6 +134,11 @@ export default util.createRule({ */ function isPossiblyUsedBeforeAssigned(node: ts.Expression): boolean { const declaration = util.getDeclaration(checker, node); + if (!declaration) { + // don't know what the declaration is for some reason, so just assume the worst + return true; + } + if ( // non-strict mode doesn't care about used before assigned errors isStrictCompilerOptionEnabled(compilerOptions, 'strictNullChecks') && diff --git a/packages/eslint-plugin/src/util/types.ts b/packages/eslint-plugin/src/util/types.ts index 98ae09b02c4..04eb720126a 100644 --- a/packages/eslint-plugin/src/util/types.ts +++ b/packages/eslint-plugin/src/util/types.ts @@ -140,8 +140,17 @@ export function isNullableType( export function getDeclaration( checker: ts.TypeChecker, node: ts.Expression, -): ts.Declaration { - return checker.getSymbolAtLocation(node)!.declarations![0]; +): ts.Declaration | null { + const symbol = checker.getSymbolAtLocation(node); + if (!symbol) { + return null; + } + const declarations = symbol.declarations; + if (!declarations) { + return null; + } + + return declarations[0]; } /** diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts index 7ed69a441f4..4c06e95a042 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts @@ -95,6 +95,21 @@ declare const str: string | null; foo(str!); `, + // https://github.com/typescript-eslint/typescript-eslint/issues/532 + ` +declare function a(a: string): any; +declare const b: string | null; +class Mx { + @a(b!) + private prop = 1; +} + `, + ` +class Mx { + @a(b!) + private prop = 1; +} + `, ], invalid: [ @@ -280,5 +295,30 @@ class Foo { }, ], }, + // https://github.com/typescript-eslint/typescript-eslint/issues/532 + { + code: ` +declare function a(a: string): any; +const b = 'asdf'; +class Mx { + @a(b!) + private prop = 1; +} + `, + output: ` +declare function a(a: string): any; +const b = 'asdf'; +class Mx { + @a(b) + private prop = 1; +} + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + line: 5, + }, + ], + }, ], });