Skip to content

Commit

Permalink
Merge pull request #567 from kurtextrem/main
Browse files Browse the repository at this point in the history
fix: async-currenttarget/preventdefault doesn’t consider nested scopes
  • Loading branch information
manuelpuyol authored Dec 9, 2024
2 parents abcfc3b + 5522392 commit 3b71fff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
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",

Check failure on line 25 in lib/rules/async-currenttarget.js

View workflow job for this annotation

GitHub Actions / build (20)

Replace `"event.currentTarget·inside·an·async·function·is·error·prone"` with `'event.currentTarget·inside·an·async·function·is·error·prone'`

Check failure on line 25 in lib/rules/async-currenttarget.js

View workflow job for this annotation

GitHub Actions / build (22)

Replace `"event.currentTarget·inside·an·async·function·is·error·prone"` with `'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",

Check failure on line 25 in lib/rules/async-preventdefault.js

View workflow job for this annotation

GitHub Actions / build (20)

Replace `"event.preventDefault()·inside·an·async·function·is·error·prone"` with `'event.preventDefault()·inside·an·async·function·is·error·prone'`

Check failure on line 25 in lib/rules/async-preventdefault.js

View workflow job for this annotation

GitHub Actions / build (22)

Replace `"event.preventDefault()·inside·an·async·function·is·error·prone"` with `'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',
},
],
},
],
})

0 comments on commit 3b71fff

Please sign in to comment.