diff --git a/lib/rules/async-currenttarget.js b/lib/rules/async-currenttarget.js index d2115b40..4b96fa61 100644 --- a/lib/rules/async-currenttarget.js +++ b/lib/rules/async-currenttarget.js @@ -13,13 +13,20 @@ module.exports = { return { AwaitExpression() { - scopeDidWait.add(context.getScope(), true) + scopeDidWait.add(context.getScope()) }, MemberExpression(node) { if (node.property && node.property.name === 'currentTarget') { - const scope = context.getScope() - if (scope.block.async && scopeDidWait.has(scope)) { - context.report({node, message: 'event.currentTarget inside an async function is error prone'}) + let scope = context.getScope() + while (scope) { + if (scopeDidWait.has(scope)) { + context.report({ + node, + message: "event.currentTarget inside an async function is error prone", + }) + break + } + scope = scope.upper } } }, diff --git a/lib/rules/async-preventdefault.js b/lib/rules/async-preventdefault.js index 51adc77c..8ed1fc47 100644 --- a/lib/rules/async-preventdefault.js +++ b/lib/rules/async-preventdefault.js @@ -13,13 +13,20 @@ module.exports = { return { AwaitExpression() { - scopeDidWait.add(context.getScope(), true) + scopeDidWait.add(context.getScope()) }, CallExpression(node) { if (node.callee.property && node.callee.property.name === 'preventDefault') { - const scope = context.getScope() - if (scope.block.async && scopeDidWait.has(scope)) { - context.report({node, message: 'event.preventDefault() inside an async function is error prone'}) + let scope = context.getScope() + while (scope) { + if (scopeDidWait.has(scope)) { + context.report({ + node, + message: "event.preventDefault() inside an async function is error prone", + }) + break + } + scope = scope.upper } } }, diff --git a/tests/async-currenttarget.js b/tests/async-currenttarget.js index 1437f982..e34e3dcd 100644 --- a/tests/async-currenttarget.js +++ b/tests/async-currenttarget.js @@ -12,6 +12,10 @@ ruleTester.run('async-currenttarget', rule, { code: 'document.addEventListener(async function(event) { event.currentTarget; await delay() })', parserOptions: {ecmaVersion: 2017}, }, + { + code: 'document.addEventListener(async function(event) { const currentTarget = event.currentTarget; await delay(); foo(() => currentTarget) })', + parserOptions: {ecmaVersion: 2017}, + }, ], invalid: [ { @@ -24,5 +28,15 @@ ruleTester.run('async-currenttarget', rule, { }, ], }, + { + code: 'document.addEventListener(async function(event) { await delay(); foo(() => e.currentTarget) })', + parserOptions: {ecmaVersion: 2017}, + errors: [ + { + message: 'event.currentTarget inside an async function is error prone', + type: 'MemberExpression', + }, + ], + }, ], }) diff --git a/tests/async-preventdefault.js b/tests/async-preventdefault.js index a28bfde0..f031cc3a 100644 --- a/tests/async-preventdefault.js +++ b/tests/async-preventdefault.js @@ -27,5 +27,15 @@ ruleTester.run('async-preventdefault', rule, { }, ], }, + { + code: 'document.addEventListener(async function(event) { await delay(); foo(() => event.preventDefault()) })', + parserOptions: {ecmaVersion: 2017}, + errors: [ + { + message: 'event.preventDefault() inside an async function is error prone', + type: 'CallExpression', + }, + ], + }, ], })