Skip to content

Commit

Permalink
Merge pull request #596 from github/update-flat-config-rules
Browse files Browse the repository at this point in the history
Update rules for flat config
  • Loading branch information
gracepark authored Jan 6, 2025
2 parents 64e6389 + 75978c8 commit 1b65401
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lib/rules/async-currenttarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
7 changes: 4 additions & 3 deletions lib/rules/async-preventdefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/filenames-match-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {

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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-useless-passive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
Expand Down
4 changes: 3 additions & 1 deletion test-examples/flat/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
]
18 changes: 18 additions & 0 deletions test-examples/flat/src/getAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
// ...
})

0 comments on commit 1b65401

Please sign in to comment.