From cb2361e9700d22e8cc1db20d6532a692aa15a22e Mon Sep 17 00:00:00 2001 From: Grace Park Date: Mon, 6 Jan 2025 16:24:43 +0000 Subject: [PATCH 1/2] update rules for flat config --- lib/rules/async-currenttarget.js | 9 +++++---- lib/rules/async-preventdefault.js | 7 ++++--- lib/rules/filenames-match-regex.js | 4 ++-- lib/rules/no-useless-passive.js | 2 +- test-examples/flat/eslint.config.mjs | 4 +++- test-examples/flat/src/getAttribute.js | 18 ++++++++++++++++++ 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/rules/async-currenttarget.js b/lib/rules/async-currenttarget.js index 0b5dff95..9828e17d 100644 --- a/lib/rules/async-currenttarget.js +++ b/lib/rules/async-currenttarget.js @@ -10,14 +10,15 @@ module.exports = { create(context) { const scopeDidWait = new WeakSet() - + const sourceCode = context.sourceCode ?? context.getSourceCode() + return { - AwaitExpression() { - scopeDidWait.add(context.getScope()) + AwaitExpression(node) { + scopeDidWait.add(sourceCode.getScope ? sourceCode.getScope(node) : context.getScope()) }, MemberExpression(node) { if (node.property && node.property.name === 'currentTarget') { - let scope = context.getScope() + let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope() while (scope) { if (scopeDidWait.has(scope)) { context.report({ diff --git a/lib/rules/async-preventdefault.js b/lib/rules/async-preventdefault.js index 33bd34b2..f4154bf0 100644 --- a/lib/rules/async-preventdefault.js +++ b/lib/rules/async-preventdefault.js @@ -10,14 +10,15 @@ module.exports = { create(context) { const scopeDidWait = new WeakSet() + const sourceCode = context.sourceCode ?? context.getSourceCode() return { - AwaitExpression() { - scopeDidWait.add(context.getScope()) + AwaitExpression(node) { + scopeDidWait.add(sourceCode.getScope ? sourceCode.getScope(node) : context.getScope()) }, CallExpression(node) { if (node.callee.property && node.callee.property.name === 'preventDefault') { - let scope = context.getScope() + let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope() while (scope) { if (scopeDidWait.has(scope)) { context.report({ diff --git a/lib/rules/filenames-match-regex.js b/lib/rules/filenames-match-regex.js index f50952c5..8e3ea341 100644 --- a/lib/rules/filenames-match-regex.js +++ b/lib/rules/filenames-match-regex.js @@ -29,10 +29,10 @@ module.exports = { const defaultRegexp = /^[a-z0-9-]+(.[a-z0-9-]+)?$/ const conventionRegexp = context.options[0] ? new RegExp(context.options[0]) : defaultRegexp const ignoreExporting = context.options[1] ? context.options[1] : false - + return { Program(node) { - const filename = context.getFilename() + const filename = context.filename ?? context.getFilename() const absoluteFilename = path.resolve(filename) const parsed = parseFilename(absoluteFilename) const shouldIgnore = isIgnoredFilename(filename) diff --git a/lib/rules/no-useless-passive.js b/lib/rules/no-useless-passive.js index 5e771884..e472b5b8 100644 --- a/lib/rules/no-useless-passive.js +++ b/lib/rules/no-useless-passive.js @@ -32,7 +32,7 @@ module.exports = { if (i === -1) return const passiveProp = options.properties[i] const l = options.properties.length - const source = context.getSourceCode() + const source = context.sourceCode ?? context.getSourceCode() context.report({ node: passiveProp, message: `"${name.value}" event listener is not cancellable and so \`passive: true\` does nothing.`, diff --git a/test-examples/flat/eslint.config.mjs b/test-examples/flat/eslint.config.mjs index 0ff163c1..d6b1cefa 100644 --- a/test-examples/flat/eslint.config.mjs +++ b/test-examples/flat/eslint.config.mjs @@ -13,7 +13,9 @@ export default [ 'github/no-then': 'error', 'github/no-blur': 'error', 'github/async-preventdefault': 'error', - 'github/filenames-match-regex': ['error', '^([a-z0-9]+)([A-Z][a-z0-9]+)*$'], + 'github/async-currenttarget': 'error', + 'github/no-useless-passive': 'error', + 'github/filenames-match-regex': 'error', }, }, ] diff --git a/test-examples/flat/src/getAttribute.js b/test-examples/flat/src/getAttribute.js index bccff298..d708c404 100644 --- a/test-examples/flat/src/getAttribute.js +++ b/test-examples/flat/src/getAttribute.js @@ -12,3 +12,21 @@ document.addEventListener('click', async function (event) { event.preventDefault() }) + +window.addEventListener( + 'scroll', + () => { + console.log('Scroll event fired!') + }, + {passive: true}, +) + +document.addEventListener('click', async function (event) { + // event.currentTarget will be an HTMLElement + const url = event.currentTarget.getAttribute('data-url') + const data = await fetch(url) + + // But now, event.currentTarget will be null + const text = event.currentTarget.getAttribute('data-text') + // ... +}) From 75978c8513059deff2f9f15d3e77e537c6e14dd1 Mon Sep 17 00:00:00 2001 From: Grace Park Date: Mon, 6 Jan 2025 16:29:26 +0000 Subject: [PATCH 2/2] run prettier --- lib/rules/async-currenttarget.js | 2 +- lib/rules/filenames-match-regex.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rules/async-currenttarget.js b/lib/rules/async-currenttarget.js index 9828e17d..a9c46999 100644 --- a/lib/rules/async-currenttarget.js +++ b/lib/rules/async-currenttarget.js @@ -11,7 +11,7 @@ module.exports = { create(context) { const scopeDidWait = new WeakSet() const sourceCode = context.sourceCode ?? context.getSourceCode() - + return { AwaitExpression(node) { scopeDidWait.add(sourceCode.getScope ? sourceCode.getScope(node) : context.getScope()) diff --git a/lib/rules/filenames-match-regex.js b/lib/rules/filenames-match-regex.js index 8e3ea341..beec42d2 100644 --- a/lib/rules/filenames-match-regex.js +++ b/lib/rules/filenames-match-regex.js @@ -29,7 +29,7 @@ module.exports = { const defaultRegexp = /^[a-z0-9-]+(.[a-z0-9-]+)?$/ const conventionRegexp = context.options[0] ? new RegExp(context.options[0]) : defaultRegexp const ignoreExporting = context.options[1] ? context.options[1] : false - + return { Program(node) { const filename = context.filename ?? context.getFilename()