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

Commit 3b71fff

Browse filesBrowse files
authored
Merge pull request #567 from kurtextrem/main
fix: async-currenttarget/preventdefault doesn’t consider nested scopes
2 parents abcfc3b + 5522392 commit 3b71fff
Copy full SHA for 3b71fff

File tree

Expand file treeCollapse file tree

4 files changed

+46
-8
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+46
-8
lines changed

‎lib/rules/async-currenttarget.js

Copy file name to clipboardExpand all lines: lib/rules/async-currenttarget.js
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ module.exports = {
1313

1414
return {
1515
AwaitExpression() {
16-
scopeDidWait.add(context.getScope(), true)
16+
scopeDidWait.add(context.getScope())
1717
},
1818
MemberExpression(node) {
1919
if (node.property && node.property.name === 'currentTarget') {
20-
const scope = context.getScope()
21-
if (scope.block.async && scopeDidWait.has(scope)) {
22-
context.report({node, message: 'event.currentTarget inside an async function is error prone'})
20+
let scope = context.getScope()
21+
while (scope) {
22+
if (scopeDidWait.has(scope)) {
23+
context.report({
24+
node,
25+
message: "event.currentTarget inside an async function is error prone",
26+
})
27+
break
28+
}
29+
scope = scope.upper
2330
}
2431
}
2532
},

‎lib/rules/async-preventdefault.js

Copy file name to clipboardExpand all lines: lib/rules/async-preventdefault.js
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ module.exports = {
1313

1414
return {
1515
AwaitExpression() {
16-
scopeDidWait.add(context.getScope(), true)
16+
scopeDidWait.add(context.getScope())
1717
},
1818
CallExpression(node) {
1919
if (node.callee.property && node.callee.property.name === 'preventDefault') {
20-
const scope = context.getScope()
21-
if (scope.block.async && scopeDidWait.has(scope)) {
22-
context.report({node, message: 'event.preventDefault() inside an async function is error prone'})
20+
let scope = context.getScope()
21+
while (scope) {
22+
if (scopeDidWait.has(scope)) {
23+
context.report({
24+
node,
25+
message: "event.preventDefault() inside an async function is error prone",
26+
})
27+
break
28+
}
29+
scope = scope.upper
2330
}
2431
}
2532
},

‎tests/async-currenttarget.js

Copy file name to clipboardExpand all lines: tests/async-currenttarget.js
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ ruleTester.run('async-currenttarget', rule, {
1212
code: 'document.addEventListener(async function(event) { event.currentTarget; await delay() })',
1313
parserOptions: {ecmaVersion: 2017},
1414
},
15+
{
16+
code: 'document.addEventListener(async function(event) { const currentTarget = event.currentTarget; await delay(); foo(() => currentTarget) })',
17+
parserOptions: {ecmaVersion: 2017},
18+
},
1519
],
1620
invalid: [
1721
{
@@ -24,5 +28,15 @@ ruleTester.run('async-currenttarget', rule, {
2428
},
2529
],
2630
},
31+
{
32+
code: 'document.addEventListener(async function(event) { await delay(); foo(() => e.currentTarget) })',
33+
parserOptions: {ecmaVersion: 2017},
34+
errors: [
35+
{
36+
message: 'event.currentTarget inside an async function is error prone',
37+
type: 'MemberExpression',
38+
},
39+
],
40+
},
2741
],
2842
})

‎tests/async-preventdefault.js

Copy file name to clipboardExpand all lines: tests/async-preventdefault.js
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,15 @@ ruleTester.run('async-preventdefault', rule, {
2727
},
2828
],
2929
},
30+
{
31+
code: 'document.addEventListener(async function(event) { await delay(); foo(() => event.preventDefault()) })',
32+
parserOptions: {ecmaVersion: 2017},
33+
errors: [
34+
{
35+
message: 'event.preventDefault() inside an async function is error prone',
36+
type: 'CallExpression',
37+
},
38+
],
39+
},
3040
],
3141
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.