Skip to content

Commit

Permalink
fix: context methods becoming properties
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Nov 27, 2023
1 parent 4507f99 commit 7d735f4
Show file tree
Hide file tree
Showing 28 changed files with 57 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/rules/callback-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/exports-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions lib/rules/global-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = {
},

create(context) {
const { sourceCode } = context
const sourceCode = context.sourceCode ?? context.getSourceCode()

return {
CallExpression(node) {
Expand All @@ -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" })
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/handle-callback-err.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
},

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

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-exports-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
type: "problem",
},
create(context) {
const sourceCode = context.sourceCode
const sourceCode = context.sourceCode ?? context.getSourceCode()

return {
AssignmentExpression(node) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-extraneous-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
messages,
},
create(context) {
const filePath = context.getFilename()
const filePath = context.getFilename() ?? context.filename
if (filePath === "<input>") {
return {}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-extraneous-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
messages,
},
create(context) {
const filePath = context.getFilename()
const filePath = context.getFilename() ?? context.filename
if (filePath === "<input>") {
return {}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-hide-core-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ module.exports = {
},
},
create(context) {
if (context.getFilename() === "<input>") {
if ((context.getFilename() ?? context.filename) === "<input>") {
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(
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
messages,
},
create(context) {
const filePath = context.getFilename()
const filePath = context.getFilename() ?? context.filename
if (filePath === "<input>") {
return {}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = {
messages,
},
create(context) {
const filePath = context.getFilename()
const filePath = context.getFilename() ?? context.filename
if (filePath === "<input>") {
return {}
}
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 @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unpublished-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
return {
Program(node) {
// Check file path.
let rawFilePath = context.getFilename()
let rawFilePath = context.getFilename() ?? context.filename
if (rawFilePath === "<input>") {
return
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unpublished-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unpublished-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
messages,
},
create(context) {
const filePath = context.getFilename()
const filePath = context.getFilename() ?? context.filename
if (filePath === "<input>") {
return {}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-unsupported-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

/**
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-unsupported-features/es-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/prefer-promises/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/prefer-promises/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/shebang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "<input>") {
return {}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/util/check-prefer-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down
4 changes: 2 additions & 2 deletions lib/util/check-unsupported-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}),
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-configured-node-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
4 changes: 3 additions & 1 deletion lib/util/get-typescript-extension-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?.()
Expand Down
4 changes: 3 additions & 1 deletion lib/util/is-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion lib/util/visit-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
7 changes: 5 additions & 2 deletions lib/util/visit-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 7d735f4

Please sign in to comment.