From 2f91a70372debe9f7118145b59c94391cefce62a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Thu, 23 Nov 2023 14:33:37 +0800 Subject: [PATCH] fix: eslint v9.0.0 compatibility (fixes #143) refs: https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/ --- lib/rules/exports-style.js | 14 +++++++------- lib/rules/global-require.js | 19 ++++++++++++------- lib/rules/handle-callback-err.js | 3 ++- lib/rules/no-deprecated-api.js | 7 +++++-- lib/rules/no-exports-assign.js | 5 ++++- lib/rules/no-path-concat.js | 5 +++-- lib/rules/no-unsupported-features.js | 12 +++++++++--- .../no-unsupported-features/es-syntax.js | 4 +++- lib/rules/prefer-promises/dns.js | 5 +++-- lib/rules/prefer-promises/fs.js | 5 +++-- lib/util/check-prefer-global.js | 6 +++++- lib/util/check-unsupported-builtins.js | 6 +++++- lib/util/visit-require.js | 6 ++++-- package.json | 2 +- 14 files changed, 66 insertions(+), 33 deletions(-) diff --git a/lib/rules/exports-style.js b/lib/rules/exports-style.js index c9b87c38..f0419b85 100644 --- a/lib/rules/exports-style.js +++ b/lib/rules/exports-style.js @@ -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)) @@ -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 @@ -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 diff --git a/lib/rules/global-require.js b/lib/rules/global-require.js index 70584771..f5003013 100644 --- a/lib/rules/global-require.js +++ b/lib/rules/global-require.js @@ -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" }) diff --git a/lib/rules/handle-callback-err.js b/lib/rules/handle-callback-err.js index 6abdba7a..2a5ac80c 100644 --- a/lib/rules/handle-callback-err.js +++ b/lib/rules/handle-callback-err.js @@ -24,6 +24,7 @@ module.exports = { }, create(context) { + const sourceCode = context.sourceCode const errorArgument = context.options[0] || "err" /** @@ -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] diff --git a/lib/rules/no-deprecated-api.js b/lib/rules/no-deprecated-api.js index 3962be6f..53689396 100644 --- a/lib/rules/no-deprecated-api.js +++ b/lib/rules/no-deprecated-api.js @@ -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", }) diff --git a/lib/rules/no-exports-assign.js b/lib/rules/no-exports-assign.js index 29bc9c42..f2029d7c 100644 --- a/lib/rules/no-exports-assign.js +++ b/lib/rules/no-exports-assign.js @@ -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 = {} diff --git a/lib/rules/no-path-concat.js b/lib/rules/no-path-concat.js index f0557336..85629a47 100644 --- a/lib/rules/no-path-concat.js +++ b/lib/rules/no-path-concat.js @@ -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() diff --git a/lib/rules/no-unsupported-features.js b/lib/rules/no-unsupported-features.js index 874f4ac0..912d5e7b 100644 --- a/lib/rules/no-unsupported-features.js +++ b/lib/rules/no-unsupported-features.js @@ -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) @@ -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) { @@ -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", @@ -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") } diff --git a/lib/rules/no-unsupported-features/es-syntax.js b/lib/rules/no-unsupported-features/es-syntax.js index f6a0a2db..ca71e6d7 100644 --- a/lib/rules/no-unsupported-features/es-syntax.js +++ b/lib/rules/no-unsupported-features/es-syntax.js @@ -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 }, } diff --git a/lib/rules/prefer-promises/dns.js b/lib/rules/prefer-promises/dns.js index 10bfd687..06208262 100644 --- a/lib/rules/prefer-promises/dns.js +++ b/lib/rules/prefer-promises/dns.js @@ -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), diff --git a/lib/rules/prefer-promises/fs.js b/lib/rules/prefer-promises/fs.js index c7a85f2b..34bb6ccb 100644 --- a/lib/rules/prefer-promises/fs.js +++ b/lib/rules/prefer-promises/fs.js @@ -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), diff --git a/lib/util/check-prefer-global.js b/lib/util/check-prefer-global.js index 7eaf442a..0070862f 100644 --- a/lib/util/check-prefer-global.js +++ b/lib/util/check-prefer-global.js @@ -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", }) diff --git a/lib/util/check-unsupported-builtins.js b/lib/util/check-unsupported-builtins.js index 32a14e8e..bcd8067f 100644 --- a/lib/util/check-unsupported-builtins.js +++ b/lib/util/check-unsupported-builtins.js @@ -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 || {}), diff --git a/lib/util/visit-require.js b/lib/util/visit-require.js index fcdeb0b3..c59c83c9 100644 --- a/lib/util/visit-require.js +++ b/lib/util/visit-require.js @@ -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, diff --git a/package.json b/package.json index f0ecff97..8ecd44b0 100644 --- a/package.json +++ b/package.json @@ -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",