Skip to content

Commit b075568

Browse files
authored
feat: eslint v9.0.0 compatibility (fixes #143) (#144)
refs: https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/
1 parent 8bd6c7e commit b075568

29 files changed

+99
-62
lines changed

lib/rules/callback-return.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = {
2626

2727
create(context) {
2828
const callbacks = context.options[0] || ["callback", "cb", "next"]
29-
const sourceCode = context.getSourceCode()
29+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
3030

3131
/**
3232
* Find the closest parent matching a list of types.

lib/rules/exports-style.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ module.exports = {
258258
const batchAssignAllowed = Boolean(
259259
context.options[1] != null && context.options[1].allowBatchAssign
260260
)
261-
const sourceCode = context.getSourceCode()
261+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
262262

263263
/**
264264
* Gets the location info of reports.
@@ -286,8 +286,7 @@ module.exports = {
286286
*
287287
* @returns {void}
288288
*/
289-
function enforceModuleExports() {
290-
const globalScope = context.getScope()
289+
function enforceModuleExports(globalScope) {
291290
const exportsNodes = getExportsNodes(globalScope)
292291
const assignList = batchAssignAllowed
293292
? createAssignmentList(getModuleExportsNodes(globalScope))
@@ -317,8 +316,7 @@ module.exports = {
317316
*
318317
* @returns {void}
319318
*/
320-
function enforceExports() {
321-
const globalScope = context.getScope()
319+
function enforceExports(globalScope) {
322320
const exportsNodes = getExportsNodes(globalScope)
323321
const moduleExportsNodes = getModuleExportsNodes(globalScope)
324322
const assignList = batchAssignAllowed
@@ -370,13 +368,15 @@ module.exports = {
370368
}
371369

372370
return {
373-
"Program:exit"() {
371+
"Program:exit"(node) {
372+
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
373+
374374
switch (mode) {
375375
case "module.exports":
376-
enforceModuleExports()
376+
enforceModuleExports(scope)
377377
break
378378
case "exports":
379-
enforceExports()
379+
enforceExports(scope)
380380
break
381381

382382
// no default

lib/rules/file-extension-in-import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = {
5757
type: "suggestion",
5858
},
5959
create(context) {
60-
if (context.getFilename().startsWith("<")) {
60+
if ((context.filename ?? context.getFilename()).startsWith("<")) {
6161
return {}
6262
}
6363
const defaultStyle = context.options[0] || "always"

lib/rules/global-require.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,21 @@ module.exports = {
6464
},
6565

6666
create(context) {
67+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
68+
6769
return {
6870
CallExpression(node) {
69-
const currentScope = context.getScope()
71+
const currentScope =
72+
sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
7073

7174
if (
7275
node.callee.name === "require" &&
7376
!isShadowed(currentScope, node.callee)
7477
) {
75-
const isGoodRequire = context
76-
.getAncestors()
78+
const isGoodRequire = (
79+
sourceCode.getAncestors?.(node) ??
80+
context.getAncestors()
81+
) // TODO: remove context.getAncestors() when dropping support for ESLint < v9
7782
.every(
7883
parent =>
7984
ACCEPTABLE_PARENTS.indexOf(parent.type) > -1

lib/rules/handle-callback-err.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
},
2525

2626
create(context) {
27+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
2728
const errorArgument = context.options[0] || "err"
2829

2930
/**
@@ -69,7 +70,7 @@ module.exports = {
6970
* @returns {void}
7071
*/
7172
function checkForError(node) {
72-
const scope = context.getScope()
73+
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
7374
const parameters = getParameters(scope)
7475
const firstParameter = parameters[0]
7576

lib/rules/no-deprecated-api.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,12 @@ module.exports = {
756756
})
757757
}
758758

759+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
759760
return {
760-
"Program:exit"() {
761-
const tracker = new ReferenceTracker(context.getScope(), {
761+
"Program:exit"(node) {
762+
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
763+
764+
const tracker = new ReferenceTracker(scope, {
762765
mode: "legacy",
763766
})
764767

lib/rules/no-exports-assign.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ module.exports = {
5050
type: "problem",
5151
},
5252
create(context) {
53+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
54+
5355
return {
5456
AssignmentExpression(node) {
55-
const scope = context.getScope()
57+
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
58+
5659
if (
5760
!isExports(node.left, scope) ||
5861
// module.exports = exports = {}

lib/rules/no-extraneous-import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
messages,
3535
},
3636
create(context) {
37-
const filePath = context.getFilename()
37+
const filePath = context.filename ?? context.getFilename()
3838
if (filePath === "<input>") {
3939
return {}
4040
}

lib/rules/no-extraneous-require.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
messages,
3737
},
3838
create(context) {
39-
const filePath = context.getFilename()
39+
const filePath = context.filename ?? context.getFilename()
4040
if (filePath === "<input>") {
4141
return {}
4242
}

lib/rules/no-hide-core-modules.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ module.exports = {
8585
},
8686
},
8787
create(context) {
88-
if (context.getFilename() === "<input>") {
88+
const filename = context.filename ?? context.getFilename()
89+
if (filename === "<input>") {
8990
return {}
9091
}
91-
const filePath = path.resolve(context.getFilename())
92+
const filePath = path.resolve(filename)
9293
const dirPath = path.dirname(filePath)
9394
const packageJson = getPackageJson(filePath)
9495
const deps = new Set(

0 commit comments

Comments
 (0)