Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: eslint v9.0.0 compatibility (fixes #143) #144

Merged
merged 7 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() // TODO: just use context.sourceCode when dropping eslint < v9

/**
* Find the closest parent matching a list of types.
Expand Down
16 changes: 8 additions & 8 deletions 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() // TODO: just use context.sourceCode when dropping eslint < v9

/**
* Gets the location info of reports.
Expand Down 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
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.filename ?? context.getFilename()).startsWith("<")) {
return {}
}
const defaultStyle = context.options[0] || "always"
Expand Down
11 changes: 8 additions & 3 deletions lib/rules/global-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,21 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9

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()
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
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 ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
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) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
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.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
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 ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9

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
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.filename ?? context.getFilename()
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.filename ?? context.getFilename()
if (filePath === "<input>") {
return {}
}
Expand Down
5 changes: 3 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,11 @@ module.exports = {
},
},
create(context) {
if (context.getFilename() === "<input>") {
const filename = context.filename ?? context.getFilename()
if (filename === "<input>") {
return {}
}
const filePath = path.resolve(context.getFilename())
const filePath = path.resolve(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.filename ?? context.getFilename()
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.filename ?? context.getFilename()
if (filePath === "<input>") {
return {}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/no-path-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ module.exports = {

create(context) {
return {
"Program:exit"() {
const globalScope = context.getScope()
"Program:exit"(node) {
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
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.filename ?? context.getFilename()
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.filename ?? context.getFilename()
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.filename ?? context.getFilename()
if (filePath === "<input>") {
return {}
}
Expand Down
15 changes: 10 additions & 5 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() // TODO: just use context.sourceCode when dropping eslint < v9
const supportInfo = parseOptions(
context.options[0],
getDefaultVersion(context.getFilename())
getDefaultVersion(context.filename ?? context.getFilename())
)

/**
Expand All @@ -1098,7 +1098,8 @@ module.exports = {
* @returns {void}
*/
function* getReferences(names) {
const globalScope = context.getScope()
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 +1160,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 +1178,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 +1334,9 @@ module.exports = {
},

FunctionDeclaration(node) {
const scope = context.getScope().upper
const scope = (
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 sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
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
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 sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
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
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 sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
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() // TODO: just use context.sourceCode when dropping eslint < v9
let filePath = context.filename ?? context.getFilename()
if (filePath === "<input>") {
return {}
}
Expand Down
10 changes: 8 additions & 2 deletions lib/util/check-prefer-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class Verifier {
*/
verifyToPreferGlobals() {
const { context, trackMap } = this
const tracker = new ReferenceTracker(context.getScope(), {
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
const scope =
sourceCode.getScope?.(sourceCode.ast) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
const tracker = new ReferenceTracker(scope, {
mode: "legacy",
})

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

for (const { node } of tracker.iterateGlobalReferences(
trackMap.globals
Expand Down
4 changes: 3 additions & 1 deletion lib/util/check-unsupported-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins(
trackMap
) {
const options = parseOptions(context)
const tracker = new ReferenceTracker(context.getScope(), { mode: "legacy" })
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
const scope = sourceCode.getScope?.(sourceCode.ast) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
const references = [
...tracker.iterateCjsReferences(trackMap.modules || {}),
...tracker.iterateEsmReferences(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.filename ?? context.getFilename()

return (
getSemverRange(version) ||
Expand Down
16 changes: 6 additions & 10 deletions lib/util/get-typescript-extension-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,15 @@ function getFromTSConfigFromFile(filename) {
* @returns {string[]} A list of extensions.
*/
module.exports = function getTypescriptExtensionMap(context) {
const filename =
context.physicalFilename ??
context.getPhysicalFilename?.() ??
context.filename ??
context.getFilename?.() // TODO: remove context.get(PhysicalFilename|Filename) when dropping eslint < v10
return (
get(context.options?.[0]) ||
get(context.settings?.n ?? context.settings?.node) ||
getFromTSConfigFromFile(
// eslint ^8
context.physicalFilename ??
// eslint ^7.28 (deprecated ^8)
context.getPhysicalFilename?.() ??
// eslint ^8 (if physicalFilename undefined)
context.filename ??
// eslint ^7 (deprecated ^8)
context.getFilename?.()
) ||
getFromTSConfigFromFile(filename) ||
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, in #139 I moved this wholesale to lib/util/get-tsconfig.js:32

PRESERVE_MAPPING
)
}
Expand Down