Skip to content

Commit b075568

Browse files
authoredDec 11, 2023
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

+1-1
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

+8-8
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

+1-1
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

+8-3
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

+2-1
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

+5-2
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

+4-1
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

+1-1
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

+1-1
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

+3-2
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(

‎lib/rules/no-missing-import.js

+1-1
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-missing-require.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = {
3838
messages,
3939
},
4040
create(context) {
41-
const filePath = context.getFilename()
41+
const filePath = context.filename ?? context.getFilename()
4242
if (filePath === "<input>") {
4343
return {}
4444
}

‎lib/rules/no-path-concat.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ module.exports = {
179179

180180
create(context) {
181181
return {
182-
"Program:exit"() {
183-
const globalScope = context.getScope()
182+
"Program:exit"(node) {
183+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
184+
const globalScope =
185+
sourceCode.getScope?.(node) ?? context.getScope()
184186
const tracker = new ReferenceTracker(globalScope)
185187
const sepNodes = new Set()
186188

‎lib/rules/no-unpublished-bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = {
5656
return {
5757
Program(node) {
5858
// Check file path.
59-
let rawFilePath = context.getFilename()
59+
let rawFilePath = context.filename ?? context.getFilename()
6060
if (rawFilePath === "<input>") {
6161
return
6262
}

‎lib/rules/no-unpublished-import.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = {
3535
messages,
3636
},
3737
create(context) {
38-
const filePath = context.getFilename()
38+
const filePath = context.filename ?? context.getFilename()
3939
const options = context.options[0] || {}
4040
const ignoreTypeImport =
4141
options.ignoreTypeImport === void 0

‎lib/rules/no-unpublished-require.js

+1-1
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-unsupported-features.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1085,10 +1085,10 @@ module.exports = {
10851085
},
10861086
},
10871087
create(context) {
1088-
const sourceCode = context.getSourceCode()
1088+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
10891089
const supportInfo = parseOptions(
10901090
context.options[0],
1091-
getDefaultVersion(context.getFilename())
1091+
getDefaultVersion(context.filename ?? context.getFilename())
10921092
)
10931093

10941094
/**
@@ -1098,7 +1098,8 @@ module.exports = {
10981098
* @returns {void}
10991099
*/
11001100
function* getReferences(names) {
1101-
const globalScope = context.getScope()
1101+
const globalScope =
1102+
sourceCode.getScope?.(sourceCode.ast) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
11021103

11031104
for (const name of names) {
11041105
const variable = globalScope.set.get(name)
@@ -1159,6 +1160,8 @@ module.exports = {
11591160
* @returns {void}
11601161
*/
11611162
function report(node, key) {
1163+
const globalScope =
1164+
sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
11621165
const version = supportInfo.version
11631166
const feature = supportInfo.features[key]
11641167
if (feature.supported) {
@@ -1175,7 +1178,7 @@ module.exports = {
11751178
version,
11761179
},
11771180
})
1178-
} else if (!normalizeScope(context.getScope(), node).isStrict) {
1181+
} else if (!normalizeScope(globalScope, node).isStrict) {
11791182
context.report({
11801183
node,
11811184
messageId: "unsupported",
@@ -1331,7 +1334,9 @@ module.exports = {
13311334
},
13321335

13331336
FunctionDeclaration(node) {
1334-
const scope = context.getScope().upper
1337+
const scope = (
1338+
sourceCode.getScope?.(node) ?? context.getScope()
1339+
).upper //TODO: remove context.getScope() when dropping support for ESLint < v9
13351340
if (!TOPLEVEL_SCOPE_TYPE.test(scope.type)) {
13361341
report(node, "blockScopedFunctions")
13371342
}

‎lib/rules/no-unsupported-features/es-syntax.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ function normalizeScope(initialScope, node) {
443443
function defineVisitor(context, options) {
444444
const testInfoPrototype = {
445445
get isStrict() {
446-
return normalizeScope(context.getScope(), this.node).isStrict
446+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
447+
const scope = sourceCode.getScope?.(this.node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
448+
return normalizeScope(scope, this.node).isStrict
447449
},
448450
}
449451

‎lib/rules/prefer-promises/dns.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ module.exports = {
5252

5353
create(context) {
5454
return {
55-
"Program:exit"() {
56-
const scope = context.getScope()
55+
"Program:exit"(node) {
56+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
57+
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
5758
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
5859
const references = [
5960
...tracker.iterateCjsReferences(trackMap),

‎lib/rules/prefer-promises/fs.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ module.exports = {
5353

5454
create(context) {
5555
return {
56-
"Program:exit"() {
57-
const scope = context.getScope()
56+
"Program:exit"(node) {
57+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
58+
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
5859
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
5960
const references = [
6061
...tracker.iterateCjsReferences(trackMap),

‎lib/rules/shebang.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ module.exports = {
9494
},
9595
},
9696
create(context) {
97-
const sourceCode = context.getSourceCode()
98-
let filePath = context.getFilename()
97+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
98+
let filePath = context.filename ?? context.getFilename()
9999
if (filePath === "<input>") {
100100
return {}
101101
}

‎lib/util/check-prefer-global.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class Verifier {
3131
*/
3232
verifyToPreferGlobals() {
3333
const { context, trackMap } = this
34-
const tracker = new ReferenceTracker(context.getScope(), {
34+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
35+
const scope =
36+
sourceCode.getScope?.(sourceCode.ast) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
37+
const tracker = new ReferenceTracker(scope, {
3538
mode: "legacy",
3639
})
3740

@@ -51,7 +54,10 @@ class Verifier {
5154
*/
5255
verifyToPreferModules() {
5356
const { context, trackMap } = this
54-
const tracker = new ReferenceTracker(context.getScope())
57+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
58+
const scope =
59+
sourceCode.getScope?.(sourceCode.ast) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
60+
const tracker = new ReferenceTracker(scope)
5561

5662
for (const { node } of tracker.iterateGlobalReferences(
5763
trackMap.globals

‎lib/util/check-unsupported-builtins.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins(
8585
trackMap
8686
) {
8787
const options = parseOptions(context)
88-
const tracker = new ReferenceTracker(context.getScope(), { mode: "legacy" })
88+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
89+
const scope = sourceCode.getScope?.(sourceCode.ast) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
90+
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
8991
const references = [
9092
...tracker.iterateCjsReferences(trackMap.modules || {}),
9193
...tracker.iterateEsmReferences(trackMap.modules || {}),

‎lib/util/get-configured-node-version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = function getConfiguredNodeVersion(context) {
4747
const version =
4848
get(context.options && context.options[0]) ||
4949
get(context.settings && (context.settings.n || context.settings.node))
50-
const filePath = context.getFilename()
50+
const filePath = context.filename ?? context.getFilename()
5151

5252
return (
5353
getSemverRange(version) ||

‎lib/util/get-typescript-extension-map.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,15 @@ function getFromTSConfigFromFile(filename) {
112112
* @returns {string[]} A list of extensions.
113113
*/
114114
module.exports = function getTypescriptExtensionMap(context) {
115+
const filename =
116+
context.physicalFilename ??
117+
context.getPhysicalFilename?.() ??
118+
context.filename ??
119+
context.getFilename?.() // TODO: remove context.get(PhysicalFilename|Filename) when dropping eslint < v10
115120
return (
116121
get(context.options?.[0]) ||
117122
get(context.settings?.n ?? context.settings?.node) ||
118-
getFromTSConfigFromFile(
119-
// eslint ^8
120-
context.physicalFilename ??
121-
// eslint ^7.28 (deprecated ^8)
122-
context.getPhysicalFilename?.() ??
123-
// eslint ^8 (if physicalFilename undefined)
124-
context.filename ??
125-
// eslint ^7 (deprecated ^8)
126-
context.getFilename?.()
127-
) ||
123+
getFromTSConfigFromFile(filename) ||
128124
PRESERVE_MAPPING
129125
)
130126
}

‎lib/util/is-typescript.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const typescriptExtensions = [".ts", ".tsx", ".cts", ".mts"]
1111
* @returns {boolean}
1212
*/
1313
module.exports = function isTypescript(context) {
14-
const sourceFileExt = path.extname(context.getPhysicalFilename())
14+
const sourceFileExt = path.extname(
15+
context.physicalFilename ?? context.getPhysicalFilename()
16+
)
1517
return typescriptExtensions.includes(sourceFileExt)
1618
}

‎lib/util/visit-import.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ module.exports = function visitImport(
3030
callback
3131
) {
3232
const targets = []
33-
const basedir = path.dirname(path.resolve(context.getFilename()))
33+
const basedir = path.dirname(
34+
path.resolve(context.filename ?? context.getFilename())
35+
)
3436
const paths = getResolvePaths(context, optionIndex)
3537
const extensions = getTryExtensions(context, optionIndex)
3638
const options = { basedir, paths, extensions }

‎lib/util/visit-require.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ module.exports = function visitRequire(
3333
callback
3434
) {
3535
const targets = []
36-
const basedir = path.dirname(path.resolve(context.getFilename()))
36+
const basedir = path.dirname(
37+
path.resolve(context.filename ?? context.getFilename())
38+
)
3739
const paths = getResolvePaths(context)
3840
const extensions = getTryExtensions(context)
3941
const options = { basedir, paths, extensions }
4042

4143
return {
42-
"Program:exit"() {
43-
const tracker = new ReferenceTracker(context.getScope())
44+
"Program:exit"(node) {
45+
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
46+
const tracker = new ReferenceTracker(
47+
sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
48+
)
4449
const references = tracker.iterateGlobalReferences({
4550
require: {
4651
[CALL]: true,

‎package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"dependencies": {
1717
"@eslint-community/eslint-utils": "^4.4.0",
1818
"builtins": "^5.0.1",
19-
"eslint-plugin-es-x": "^7.1.0",
19+
"eslint-plugin-es-x": "^7.5.0",
2020
"get-tsconfig": "^4.7.0",
2121
"ignore": "^5.2.4",
2222
"is-builtin-module": "^3.2.1",
@@ -27,10 +27,10 @@
2727
},
2828
"devDependencies": {
2929
"@eslint/js": "^8.43.0",
30-
"@types/eslint": "^8.44.2",
30+
"@types/eslint": "^8.44.6",
3131
"@typescript-eslint/parser": "^5.60.0",
3232
"esbuild": "^0.18.7",
33-
"eslint": "^8.43.0",
33+
"eslint": "^8.53.0",
3434
"eslint-config-prettier": "^8.8.0",
3535
"eslint-doc-generator": "^1.4.3",
3636
"eslint-plugin-eslint-plugin": "^5.1.0",

0 commit comments

Comments
 (0)
Please sign in to comment.