From 7d735f4b373dcaf1e7a91362e8d6d938c8b23784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Mon, 27 Nov 2023 10:07:11 +0800 Subject: [PATCH] fix: context methods becoming properties refs: https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/#context-methods-becoming-properties --- lib/rules/callback-return.js | 2 +- lib/rules/exports-style.js | 2 +- lib/rules/file-extension-in-import.js | 2 +- lib/rules/global-require.js | 14 +++++++++----- lib/rules/handle-callback-err.js | 2 +- lib/rules/no-deprecated-api.js | 2 +- lib/rules/no-exports-assign.js | 2 +- lib/rules/no-extraneous-import.js | 2 +- lib/rules/no-extraneous-require.js | 2 +- lib/rules/no-hide-core-modules.js | 4 ++-- lib/rules/no-missing-import.js | 2 +- lib/rules/no-missing-require.js | 2 +- lib/rules/no-path-concat.js | 5 +++-- lib/rules/no-unpublished-bin.js | 2 +- lib/rules/no-unpublished-import.js | 2 +- lib/rules/no-unpublished-require.js | 2 +- lib/rules/no-unsupported-features.js | 6 +++--- lib/rules/no-unsupported-features/es-syntax.js | 4 ++-- lib/rules/prefer-promises/dns.js | 4 ++-- lib/rules/prefer-promises/fs.js | 4 ++-- lib/rules/shebang.js | 4 ++-- lib/util/check-prefer-global.js | 4 ++-- lib/util/check-unsupported-builtins.js | 4 ++-- lib/util/get-configured-node-version.js | 2 +- lib/util/get-typescript-extension-map.js | 4 +++- lib/util/is-typescript.js | 4 +++- lib/util/visit-import.js | 4 +++- lib/util/visit-require.js | 7 +++++-- 28 files changed, 57 insertions(+), 43 deletions(-) diff --git a/lib/rules/callback-return.js b/lib/rules/callback-return.js index 30f33f74..415e6170 100644 --- a/lib/rules/callback-return.js +++ b/lib/rules/callback-return.js @@ -26,7 +26,7 @@ module.exports = { create(context) { const callbacks = context.options[0] || ["callback", "cb", "next"] - const sourceCode = context.getSourceCode() + const sourceCode = context.sourceCode ?? context.getSourceCode() /** * Find the closest parent matching a list of types. diff --git a/lib/rules/exports-style.js b/lib/rules/exports-style.js index f0419b85..4c609759 100644 --- a/lib/rules/exports-style.js +++ b/lib/rules/exports-style.js @@ -258,7 +258,7 @@ module.exports = { const batchAssignAllowed = Boolean( context.options[1] != null && context.options[1].allowBatchAssign ) - const sourceCode = context.getSourceCode() + const sourceCode = context.sourceCode ?? context.getSourceCode() /** * Gets the location info of reports. diff --git a/lib/rules/file-extension-in-import.js b/lib/rules/file-extension-in-import.js index a6666f67..4a287890 100644 --- a/lib/rules/file-extension-in-import.js +++ b/lib/rules/file-extension-in-import.js @@ -57,7 +57,7 @@ module.exports = { type: "suggestion", }, create(context) { - if (context.getFilename().startsWith("<")) { + if ((context.getFilename() ?? context.filename).startsWith("<")) { return {} } const defaultStyle = context.options[0] || "always" diff --git a/lib/rules/global-require.js b/lib/rules/global-require.js index b49b470b..2bafaabc 100644 --- a/lib/rules/global-require.js +++ b/lib/rules/global-require.js @@ -64,7 +64,7 @@ module.exports = { }, create(context) { - const { sourceCode } = context + const sourceCode = context.sourceCode ?? context.getSourceCode() return { CallExpression(node) { @@ -75,10 +75,14 @@ module.exports = { node.callee.name === "require" && !isShadowed(currentScope, node.callee) ) { - const isGoodRequire = (sourceCode.getAncestors?.(node) ?? context.getAncestors()) // TODO: remove context.getAncestors() when dropping support for ESLint < v9 - .every( - parent => ACCEPTABLE_PARENTS.indexOf(parent.type) > -1 - ) + const isGoodRequire = ( + sourceCode.getAncestors?.(node) ?? + context.getAncestors() + ) // TODO: remove context.getAncestors() when dropping support for ESLint < v9 + .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 bfd35062..a2fceda1 100644 --- a/lib/rules/handle-callback-err.js +++ b/lib/rules/handle-callback-err.js @@ -24,7 +24,7 @@ module.exports = { }, create(context) { - const sourceCode = context.sourceCode + const sourceCode = context.sourceCode ?? context.getSourceCode() const errorArgument = context.options[0] || "err" /** diff --git a/lib/rules/no-deprecated-api.js b/lib/rules/no-deprecated-api.js index 53689396..255a0cd3 100644 --- a/lib/rules/no-deprecated-api.js +++ b/lib/rules/no-deprecated-api.js @@ -756,7 +756,7 @@ module.exports = { }) } - const { sourceCode } = context + const sourceCode = context.sourceCode ?? context.getSourceCode() return { "Program:exit"(node) { const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9 diff --git a/lib/rules/no-exports-assign.js b/lib/rules/no-exports-assign.js index f2029d7c..e9d4c38d 100644 --- a/lib/rules/no-exports-assign.js +++ b/lib/rules/no-exports-assign.js @@ -50,7 +50,7 @@ module.exports = { type: "problem", }, create(context) { - const sourceCode = context.sourceCode + const sourceCode = context.sourceCode ?? context.getSourceCode() return { AssignmentExpression(node) { diff --git a/lib/rules/no-extraneous-import.js b/lib/rules/no-extraneous-import.js index c84c9df3..6407f915 100644 --- a/lib/rules/no-extraneous-import.js +++ b/lib/rules/no-extraneous-import.js @@ -34,7 +34,7 @@ module.exports = { messages, }, create(context) { - const filePath = context.getFilename() + const filePath = context.getFilename() ?? context.filename if (filePath === "") { return {} } diff --git a/lib/rules/no-extraneous-require.js b/lib/rules/no-extraneous-require.js index 7d3321bd..cc997dae 100644 --- a/lib/rules/no-extraneous-require.js +++ b/lib/rules/no-extraneous-require.js @@ -36,7 +36,7 @@ module.exports = { messages, }, create(context) { - const filePath = context.getFilename() + const filePath = context.getFilename() ?? context.filename if (filePath === "") { return {} } diff --git a/lib/rules/no-hide-core-modules.js b/lib/rules/no-hide-core-modules.js index a854a479..a83c8b7e 100644 --- a/lib/rules/no-hide-core-modules.js +++ b/lib/rules/no-hide-core-modules.js @@ -85,10 +85,10 @@ module.exports = { }, }, create(context) { - if (context.getFilename() === "") { + if ((context.getFilename() ?? context.filename) === "") { return {} } - const filePath = path.resolve(context.getFilename()) + const filePath = path.resolve(context.getFilename() ?? context.filename) const dirPath = path.dirname(filePath) const packageJson = getPackageJson(filePath) const deps = new Set( diff --git a/lib/rules/no-missing-import.js b/lib/rules/no-missing-import.js index 00e4d2b2..2c6bb43a 100644 --- a/lib/rules/no-missing-import.js +++ b/lib/rules/no-missing-import.js @@ -36,7 +36,7 @@ module.exports = { messages, }, create(context) { - const filePath = context.getFilename() + const filePath = context.getFilename() ?? context.filename if (filePath === "") { return {} } diff --git a/lib/rules/no-missing-require.js b/lib/rules/no-missing-require.js index 1125767c..0e7aa175 100644 --- a/lib/rules/no-missing-require.js +++ b/lib/rules/no-missing-require.js @@ -38,7 +38,7 @@ module.exports = { messages, }, create(context) { - const filePath = context.getFilename() + const filePath = context.getFilename() ?? context.filename if (filePath === "") { return {} } diff --git a/lib/rules/no-path-concat.js b/lib/rules/no-path-concat.js index 9d63213f..c590ccc1 100644 --- a/lib/rules/no-path-concat.js +++ b/lib/rules/no-path-concat.js @@ -180,8 +180,9 @@ module.exports = { create(context) { return { "Program:exit"(node) { - const sourceCode = context.sourceCode - const globalScope = sourceCode.getScope?.(node) ?? context.getScope() + const sourceCode = context.sourceCode ?? context.getSourceCode() + const globalScope = + sourceCode.getScope?.(node) ?? context.getScope() const tracker = new ReferenceTracker(globalScope) const sepNodes = new Set() diff --git a/lib/rules/no-unpublished-bin.js b/lib/rules/no-unpublished-bin.js index dd62181f..de8749d6 100644 --- a/lib/rules/no-unpublished-bin.js +++ b/lib/rules/no-unpublished-bin.js @@ -56,7 +56,7 @@ module.exports = { return { Program(node) { // Check file path. - let rawFilePath = context.getFilename() + let rawFilePath = context.getFilename() ?? context.filename if (rawFilePath === "") { return } diff --git a/lib/rules/no-unpublished-import.js b/lib/rules/no-unpublished-import.js index 7fd34bfa..70acfebe 100644 --- a/lib/rules/no-unpublished-import.js +++ b/lib/rules/no-unpublished-import.js @@ -35,7 +35,7 @@ module.exports = { messages, }, create(context) { - const filePath = context.getFilename() + const filePath = context.getFilename() ?? context.filename const options = context.options[0] || {} const ignoreTypeImport = options.ignoreTypeImport === void 0 diff --git a/lib/rules/no-unpublished-require.js b/lib/rules/no-unpublished-require.js index 7e804765..a9c58819 100644 --- a/lib/rules/no-unpublished-require.js +++ b/lib/rules/no-unpublished-require.js @@ -36,7 +36,7 @@ module.exports = { messages, }, create(context) { - const filePath = context.getFilename() + const filePath = context.getFilename() ?? context.filename if (filePath === "") { return {} } diff --git a/lib/rules/no-unsupported-features.js b/lib/rules/no-unsupported-features.js index 912d5e7b..a7a2893d 100644 --- a/lib/rules/no-unsupported-features.js +++ b/lib/rules/no-unsupported-features.js @@ -1085,10 +1085,10 @@ module.exports = { }, }, create(context) { - const sourceCode = context.getSourceCode() + const sourceCode = context.sourceCode ?? context.getSourceCode() const supportInfo = parseOptions( context.options[0], - getDefaultVersion(context.getFilename()) + getDefaultVersion(context.getFilename() ?? context.filename) ) /** @@ -1336,7 +1336,7 @@ module.exports = { FunctionDeclaration(node) { const scope = ( - context.sourceCode.getScope?.(node) ?? context.getScope() + 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 ca71e6d7..dfae1cd3 100644 --- a/lib/rules/no-unsupported-features/es-syntax.js +++ b/lib/rules/no-unsupported-features/es-syntax.js @@ -443,8 +443,8 @@ function normalizeScope(initialScope, node) { function defineVisitor(context, options) { const testInfoPrototype = { get isStrict() { - const scope = - context.sourceCode.getScope?.(this.node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9 + const sourceCode = context.sourceCode ?? context.getSourceCode() + const scope = 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 06208262..2fe7a08a 100644 --- a/lib/rules/prefer-promises/dns.js +++ b/lib/rules/prefer-promises/dns.js @@ -53,8 +53,8 @@ module.exports = { create(context) { return { "Program:exit"(node) { - const scope = - context.sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9 + const sourceCode = context.sourceCode ?? context.getSourceCode() + const scope = 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 34bb6ccb..d8f0ac6c 100644 --- a/lib/rules/prefer-promises/fs.js +++ b/lib/rules/prefer-promises/fs.js @@ -54,8 +54,8 @@ module.exports = { create(context) { return { "Program:exit"(node) { - const scope = - context.sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9 + const sourceCode = context.sourceCode ?? context.getSourceCode() + const scope = 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/shebang.js b/lib/rules/shebang.js index 01f228bb..58d3e356 100644 --- a/lib/rules/shebang.js +++ b/lib/rules/shebang.js @@ -94,8 +94,8 @@ module.exports = { }, }, create(context) { - const sourceCode = context.getSourceCode() - let filePath = context.getFilename() + const sourceCode = context.sourceCode ?? context.getSourceCode() + let filePath = context.getFilename() ?? context.filename if (filePath === "") { return {} } diff --git a/lib/util/check-prefer-global.js b/lib/util/check-prefer-global.js index 0070862f..d2a75cc1 100644 --- a/lib/util/check-prefer-global.js +++ b/lib/util/check-prefer-global.js @@ -31,10 +31,10 @@ class Verifier { */ verifyToPreferGlobals() { const { context, trackMap } = this + const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: sourceCode.ast is the correct node? const scope = - context.sourceCode.getScope?.(context.sourceCode.ast) ?? - context.getScope() + 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 bcd8067f..d8d3dcf7 100644 --- a/lib/util/check-unsupported-builtins.js +++ b/lib/util/check-unsupported-builtins.js @@ -85,10 +85,10 @@ module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins( trackMap ) { const options = parseOptions(context) + const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: context.sourceCode.ast is the correct node? const scope = - context.sourceCode.getScope?.(context.sourceCode.ast) ?? - context.getScope() + sourceCode.getScope?.(context.sourceCode.ast) ?? context.getScope() const tracker = new ReferenceTracker(scope, { mode: "legacy" }) const references = [ ...tracker.iterateCjsReferences(trackMap.modules || {}), diff --git a/lib/util/get-configured-node-version.js b/lib/util/get-configured-node-version.js index ed235fa7..07cec117 100644 --- a/lib/util/get-configured-node-version.js +++ b/lib/util/get-configured-node-version.js @@ -47,7 +47,7 @@ module.exports = function getConfiguredNodeVersion(context) { const version = get(context.options && context.options[0]) || get(context.settings && (context.settings.n || context.settings.node)) - const filePath = context.getFilename() + const filePath = context.getFilename() ?? context.filename return ( getSemverRange(version) || diff --git a/lib/util/get-typescript-extension-map.js b/lib/util/get-typescript-extension-map.js index ccdb2feb..a82fac52 100644 --- a/lib/util/get-typescript-extension-map.js +++ b/lib/util/get-typescript-extension-map.js @@ -117,10 +117,12 @@ module.exports = function getTypescriptExtensionMap(context) { get(context.settings?.n ?? context.settings?.node) || getFromTSConfigFromFile( // eslint ^8 - context.physicalFilename ?? + context.getPhysicalFilename() ?? + context.physicalFilename ?? // eslint ^7.28 (deprecated ^8) context.getPhysicalFilename?.() ?? // eslint ^8 (if physicalFilename undefined) + context.getFilename() ?? context.filename ?? // eslint ^7 (deprecated ^8) context.getFilename?.() diff --git a/lib/util/is-typescript.js b/lib/util/is-typescript.js index 479397cc..16d99914 100644 --- a/lib/util/is-typescript.js +++ b/lib/util/is-typescript.js @@ -11,6 +11,8 @@ const typescriptExtensions = [".ts", ".tsx", ".cts", ".mts"] * @returns {boolean} */ module.exports = function isTypescript(context) { - const sourceFileExt = path.extname(context.getPhysicalFilename()) + const sourceFileExt = path.extname( + context.getPhysicalFilename() ?? context.physicalFilename + ) return typescriptExtensions.includes(sourceFileExt) } diff --git a/lib/util/visit-import.js b/lib/util/visit-import.js index 2402651f..01df2311 100644 --- a/lib/util/visit-import.js +++ b/lib/util/visit-import.js @@ -30,7 +30,9 @@ module.exports = function visitImport( callback ) { const targets = [] - const basedir = path.dirname(path.resolve(context.getFilename())) + const basedir = path.dirname( + path.resolve(context.getFilename() ?? context.filename) + ) const paths = getResolvePaths(context, optionIndex) const extensions = getTryExtensions(context, optionIndex) const options = { basedir, paths, extensions } diff --git a/lib/util/visit-require.js b/lib/util/visit-require.js index ad309aef..bf006eee 100644 --- a/lib/util/visit-require.js +++ b/lib/util/visit-require.js @@ -33,15 +33,18 @@ module.exports = function visitRequire( callback ) { const targets = [] - const basedir = path.dirname(path.resolve(context.getFilename())) + const basedir = path.dirname( + path.resolve(context.getFilename() ?? context.filename) + ) const paths = getResolvePaths(context) const extensions = getTryExtensions(context) const options = { basedir, paths, extensions } return { "Program:exit"(node) { + const sourceCode = context.sourceCode ?? context.getSourceCode() const tracker = new ReferenceTracker( - context.sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9 + sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9 ) const references = tracker.iterateGlobalReferences({ require: {