From d197ea784fefaefdf688bb3766907beea2e53e1c Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 5 Feb 2019 02:43:21 +0100 Subject: [PATCH] fix(eslint-plugin): fix crash in no-unnecessary-type-assertion --- .../lib/rules/no-unnecessary-type-assertion.js | 5 +++++ .../tests/lib/rules/no-unnecessary-type-assertion.js | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/packages/eslint-plugin/lib/rules/no-unnecessary-type-assertion.js b/packages/eslint-plugin/lib/rules/no-unnecessary-type-assertion.js index ed1747c0d95e..34f4b6182155 100644 --- a/packages/eslint-plugin/lib/rules/no-unnecessary-type-assertion.js +++ b/packages/eslint-plugin/lib/rules/no-unnecessary-type-assertion.js @@ -91,6 +91,11 @@ function verifyCast(node, context, checker) { const originalNode = context.parserServices.esTreeNodeToTSNodeMap.get(node); const options = context.options[0]; + if (!originalNode || !originalNode.type) { + // Nodes without type + return; + } + if ( options && options.typesToIgnore && diff --git a/packages/eslint-plugin/tests/lib/rules/no-unnecessary-type-assertion.js b/packages/eslint-plugin/tests/lib/rules/no-unnecessary-type-assertion.js index 633f1a334330..8795cff89c51 100644 --- a/packages/eslint-plugin/tests/lib/rules/no-unnecessary-type-assertion.js +++ b/packages/eslint-plugin/tests/lib/rules/no-unnecessary-type-assertion.js @@ -51,6 +51,18 @@ type Foo = number; const foo = (3 + 5) as Foo;`, options: [{ typesToIgnore: ['Foo'] }] }, + { + code: `const foo = (3 + 5) as any;`, + options: [{ typesToIgnore: ['any'] }] + }, + { + code: `((Syntax as any).ArrayExpression = 'foo')`, + options: [{ typesToIgnore: ['any'] }] + }, + { + code: `const foo = (3 + 5) as string;`, + options: [{ typesToIgnore: ['string'] }] + }, { code: ` type Foo = number;