Skip to content
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

Update rules for flat config #596

Merged
merged 2 commits into from
Jan 6, 2025
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
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')
Copy link
Preview

Copilot AI Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event.currentTarget might be null after the await call. Add a null check before accessing getAttribute.

Suggested change
const text = event.currentTarget.getAttribute('data-text')
if (event.currentTarget === null) return;
const text = event.currentTarget.getAttribute('data-text')

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
// ...
})
Loading