Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: async-currenttarget/preventdefault doesn’t consider nested scopes #567

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/rules/async-currenttarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down
15 changes: 11 additions & 4 deletions lib/rules/async-preventdefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down
14 changes: 14 additions & 0 deletions tests/async-currenttarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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',
},
],
},
],
})
10 changes: 10 additions & 0 deletions tests/async-preventdefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
},
],
})