Skip to content

Commit

Permalink
fix: eslint v9.0.0 compatibility (fixes #143)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Nov 24, 2023
1 parent deeb998 commit 2f91a70
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 33 deletions.
14 changes: 7 additions & 7 deletions lib/rules/exports-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ module.exports = {
*
* @returns {void}
*/
function enforceModuleExports() {
const globalScope = context.getScope()
function enforceModuleExports(globalScope) {
const exportsNodes = getExportsNodes(globalScope)
const assignList = batchAssignAllowed
? createAssignmentList(getModuleExportsNodes(globalScope))
Expand Down Expand Up @@ -317,8 +316,7 @@ module.exports = {
*
* @returns {void}
*/
function enforceExports() {
const globalScope = context.getScope()
function enforceExports(globalScope) {
const exportsNodes = getExportsNodes(globalScope)
const moduleExportsNodes = getModuleExportsNodes(globalScope)
const assignList = batchAssignAllowed
Expand Down Expand Up @@ -370,13 +368,15 @@ module.exports = {
}

return {
"Program:exit"() {
"Program:exit"(node) {
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9

switch (mode) {
case "module.exports":
enforceModuleExports()
enforceModuleExports(scope)
break
case "exports":
enforceExports()
enforceExports(scope)
break

// no default
Expand Down
19 changes: 12 additions & 7 deletions lib/rules/global-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,25 @@ module.exports = {
},

create(context) {
const { sourceCode } = context

return {
CallExpression(node) {
const currentScope = context.getScope()
const currentScope =
sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9

if (
node.callee.name === "require" &&
!isShadowed(currentScope, node.callee)
) {
const isGoodRequire = context
.getAncestors()
.every(
parent =>
ACCEPTABLE_PARENTS.indexOf(parent.type) > -1
)
const isGoodRequire = (
sourceCode?.getAncestors ?? context.getAncestors
)(
// TODO: remove context.getAncestors() when dropping support for ESLint < v9
node
).every(
parent => ACCEPTABLE_PARENTS.indexOf(parent.type) > -1
)

if (!isGoodRequire) {
context.report({ node, messageId: "unexpected" })
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/handle-callback-err.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode
const errorArgument = context.options[0] || "err"

/**
Expand Down Expand Up @@ -69,7 +70,7 @@ module.exports = {
* @returns {void}
*/
function checkForError(node) {
const scope = context.getScope()
const scope = sourceCode.getScope(node)
const parameters = getParameters(scope)
const firstParameter = parameters[0]

Expand Down
7 changes: 5 additions & 2 deletions lib/rules/no-deprecated-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,12 @@ module.exports = {
})
}

const { sourceCode } = context
return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope(), {
"Program:exit"(node) {
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9

const tracker = new ReferenceTracker(scope, {
mode: "legacy",
})

Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-exports-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ module.exports = {
type: "problem",
},
create(context) {
const sourceCode = context.sourceCode

return {
AssignmentExpression(node) {
const scope = context.getScope()
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9

if (
!isExports(node.left, scope) ||
// module.exports = exports = {}
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-path-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ module.exports = {

create(context) {
return {
"Program:exit"() {
const globalScope = context.getScope()
"Program:exit"(node) {
const sourceCode = context.sourceCode
const globalScope = sourceCode.getScope(node)
const tracker = new ReferenceTracker(globalScope)
const sepNodes = new Set()

Expand Down
12 changes: 9 additions & 3 deletions lib/rules/no-unsupported-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,9 @@ module.exports = {
* @returns {void}
*/
function* getReferences(names) {
const globalScope = context.getScope()
// TODO: ???
const globalScope =
sourceCode.getScope?.(sourceCode.ast) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9

for (const name of names) {
const variable = globalScope.set.get(name)
Expand Down Expand Up @@ -1159,6 +1161,8 @@ module.exports = {
* @returns {void}
*/
function report(node, key) {
const globalScope =
sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
const version = supportInfo.version
const feature = supportInfo.features[key]
if (feature.supported) {
Expand All @@ -1175,7 +1179,7 @@ module.exports = {
version,
},
})
} else if (!normalizeScope(context.getScope(), node).isStrict) {
} else if (!normalizeScope(globalScope, node).isStrict) {
context.report({
node,
messageId: "unsupported",
Expand Down Expand Up @@ -1331,7 +1335,9 @@ module.exports = {
},

FunctionDeclaration(node) {
const scope = context.getScope().upper
const scope = (
context.sourceCode.getScope?.(node) ?? context.getScope()
).upper //TODO: remove context.getScope() when dropping support for ESLint < v9
if (!TOPLEVEL_SCOPE_TYPE.test(scope.type)) {
report(node, "blockScopedFunctions")
}
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-unsupported-features/es-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ function normalizeScope(initialScope, node) {
function defineVisitor(context, options) {
const testInfoPrototype = {
get isStrict() {
return normalizeScope(context.getScope(), this.node).isStrict
const scope =
context.sourceCode.getScope?.(this.node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
return normalizeScope(scope, this.node).isStrict
},
}

Expand Down
5 changes: 3 additions & 2 deletions lib/rules/prefer-promises/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ module.exports = {

create(context) {
return {
"Program:exit"() {
const scope = context.getScope()
"Program:exit"(node) {
const scope =
context.sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
const references = [
...tracker.iterateCjsReferences(trackMap),
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/prefer-promises/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ module.exports = {

create(context) {
return {
"Program:exit"() {
const scope = context.getScope()
"Program:exit"(node) {
const scope =
context.sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
const references = [
...tracker.iterateCjsReferences(trackMap),
Expand Down
6 changes: 5 additions & 1 deletion lib/util/check-prefer-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class Verifier {
*/
verifyToPreferGlobals() {
const { context, trackMap } = this
const tracker = new ReferenceTracker(context.getScope(), {
// TODO: sourceCode.ast is the correct node?
const scope =
context.sourceCode.getScope?.(context.sourceCode.ast) ??
context.getScope()
const tracker = new ReferenceTracker(scope, {
mode: "legacy",
})

Expand Down
6 changes: 5 additions & 1 deletion lib/util/check-unsupported-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins(
trackMap
) {
const options = parseOptions(context)
const tracker = new ReferenceTracker(context.getScope(), { mode: "legacy" })
// TODO: context.sourceCode.ast is the correct node?
const scope =
context.sourceCode.getScope?.(context.sourceCode.ast) ??
context.getScope()
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
const references = [
...tracker.iterateCjsReferences(trackMap.modules || {}),
...tracker.iterateEsmReferences(trackMap.modules || {}),
Expand Down
6 changes: 4 additions & 2 deletions lib/util/visit-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ module.exports = function visitRequire(
const options = { basedir, paths, extensions }

return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
"Program:exit"(node) {
const tracker = new ReferenceTracker(
context.sourceCode?.getScope(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
)
const references = tracker.iterateGlobalReferences({
require: {
[CALL]: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@types/eslint": "^8.44.2",
"@typescript-eslint/parser": "^5.60.0",
"esbuild": "^0.18.7",
"eslint": "^8.43.0",
"eslint": "^8.47.0",
"eslint-config-prettier": "^8.8.0",
"eslint-doc-generator": "^1.4.3",
"eslint-plugin-eslint-plugin": "^5.1.0",
Expand Down

0 comments on commit 2f91a70

Please sign in to comment.