Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

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

Merged
merged 3 commits into from
Dec 9, 2024
Merged
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 15 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 15 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 14 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 10 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',
},
],
},
],
})
Morty Proxy This is a proxified and sanitized view of the page, visit original site.