From 3ef954617a94b59140e08b584896592f5dee07ad Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Tue, 8 Jan 2019 19:16:33 -0800 Subject: [PATCH 1/2] [FIX] prevent crashes from declare statements --- lib/rules/no-this-alias.js | 2 +- tests/lib/rules/no-this-alias.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-this-alias.js b/lib/rules/no-this-alias.js index a32f472..e7c7988 100644 --- a/lib/rules/no-this-alias.js +++ b/lib/rules/no-this-alias.js @@ -62,7 +62,7 @@ module.exports = { VariableDeclarator(node) { const { id, init } = node; - if (init.type !== "ThisExpression") return; + if (!init || init.type !== "ThisExpression") return; if (allowDestructuring && node.id.type !== "Identifier") return; if (!allowedNames.includes(id.name)) { diff --git a/tests/lib/rules/no-this-alias.js b/tests/lib/rules/no-this-alias.js index f3ad295..ea09e8f 100644 --- a/tests/lib/rules/no-this-alias.js +++ b/tests/lib/rules/no-this-alias.js @@ -55,6 +55,12 @@ const [foo, bar] = this; }, ], }, + // https://github.com/bradzacher/eslint-plugin-typescript/issues/281 + ` +declare module 'foo' { + declare const aVar: string +} + `, ], invalid: [ From fa998a7e623e0f67f167d98f808d33bf22a98c86 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Tue, 8 Jan 2019 21:29:15 -0800 Subject: [PATCH 2/2] simplify with a selector --- lib/rules/no-this-alias.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/rules/no-this-alias.js b/lib/rules/no-this-alias.js index e7c7988..19fda9b 100644 --- a/lib/rules/no-this-alias.js +++ b/lib/rules/no-this-alias.js @@ -59,11 +59,10 @@ module.exports = { )[0]; return { - VariableDeclarator(node) { - const { id, init } = node; + "VariableDeclarator[init.type='ThisExpression']"(node) { + const { id } = node; - if (!init || init.type !== "ThisExpression") return; - if (allowDestructuring && node.id.type !== "Identifier") return; + if (allowDestructuring && id.type !== "Identifier") return; if (!allowedNames.includes(id.name)) { context.report({