diff --git a/package.json b/package.json index a5bfc2e09e..b780c13c45 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "c8": "^7.13.0", "chalk": "^5.2.0", "enquirer": "^2.3.6", - "eslint": "^8.37.0", + "eslint": "^8.38.0", "eslint-ava-rule-tester": "^4.0.0", "eslint-doc-generator": "^1.4.3", "eslint-plugin-eslint-plugin": "^5.0.8", @@ -94,7 +94,7 @@ "yaml": "^2.2.1" }, "peerDependencies": { - "eslint": ">=8.37.0" + "eslint": ">=8.38.0" }, "ava": { "files": [ diff --git a/rules/consistent-destructuring.js b/rules/consistent-destructuring.js index 45cfe94d7d..9fe6005115 100644 --- a/rules/consistent-destructuring.js +++ b/rules/consistent-destructuring.js @@ -53,7 +53,7 @@ const isChildInParentScope = (child, parent) => { /** @param {import('eslint').Rule.RuleContext} context */ const create = context => { - const source = context.getSourceCode(); + const sourceCode = context.getSourceCode(); const declarations = new Map(); return { @@ -63,9 +63,9 @@ const create = context => { return; } - declarations.set(source.getText(node.init), { + declarations.set(sourceCode.getText(node.init), { scope: context.getScope(), - variables: context.getDeclaredVariables(node), + variables: sourceCode.getDeclaredVariables(node), objectPattern: node.id, }); }, @@ -74,7 +74,7 @@ const create = context => { return; } - const declaration = declarations.get(source.getText(node.object)); + const declaration = declarations.get(sourceCode.getText(node.object)); if (!declaration) { return; @@ -97,8 +97,8 @@ const create = context => { const hasRest = lastProperty && lastProperty.type === 'RestElement'; - const expression = source.getText(node); - const member = source.getText(node.property); + const expression = sourceCode.getText(node); + const member = sourceCode.getText(node.property); // Member might already be destructured const destructuredMember = destructurings.find(property => diff --git a/rules/no-array-for-each.js b/rules/no-array-for-each.js index 8e7a7f3b43..fe7447a3bf 100644 --- a/rules/no-array-for-each.js +++ b/rules/no-array-for-each.js @@ -94,7 +94,7 @@ function getFixFunction(callExpression, functionInfo, context) { const shouldUseEntries = parameters.length === 2; let text = 'for ('; - text += isFunctionParameterVariableReassigned(callback, context) ? 'let' : 'const'; + text += isFunctionParameterVariableReassigned(callback, sourceCode) ? 'let' : 'const'; text += ' '; text += shouldUseEntries ? `[${indexText}, ${elementText}]` : elementText; text += ' of '; @@ -276,8 +276,8 @@ const isChildScope = (child, parent) => { return false; }; -function isFunctionParametersSafeToFix(callbackFunction, {context, scope, callExpression, allIdentifiers}) { - const variables = context.getDeclaredVariables(callbackFunction); +function isFunctionParametersSafeToFix(callbackFunction, {sourceCode, scope, callExpression, allIdentifiers}) { + const variables = sourceCode.getDeclaredVariables(callbackFunction); for (const variable of variables) { if (variable.defs.length !== 1) { @@ -311,15 +311,15 @@ function isFunctionParametersSafeToFix(callbackFunction, {context, scope, callEx return true; } -function isFunctionParameterVariableReassigned(callbackFunction, context) { - return context.getDeclaredVariables(callbackFunction) +function isFunctionParameterVariableReassigned(callbackFunction, sourceCode) { + return sourceCode.getDeclaredVariables(callbackFunction) .filter(variable => variable.defs[0].type === 'Parameter') .some(variable => variable.references.some(reference => !reference.init && reference.isWrite()), ); } -function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context}) { +function isFixable(callExpression, {scope, functionInfo, allIdentifiers, sourceCode}) { // Check `CallExpression` if (callExpression.optional || callExpression.arguments.length !== 1) { return false; @@ -354,7 +354,7 @@ function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1814 || (parameters.length === 2 && parameters[1].type !== 'Identifier') || parameters.some(({type, typeAnnotation}) => type === 'RestElement' || typeAnnotation) - || !isFunctionParametersSafeToFix(callback, {scope, callExpression, allIdentifiers, context}) + || !isFunctionParametersSafeToFix(callback, {scope, callExpression, allIdentifiers, sourceCode}) ) { return false; } @@ -426,7 +426,7 @@ const create = context => { messageId: MESSAGE_ID_ERROR, }; - if (!isFixable(node, {scope, allIdentifiers, functionInfo, context})) { + if (!isFixable(node, {scope, allIdentifiers, functionInfo, sourceCode})) { yield problem; continue; } diff --git a/rules/no-thenable.js b/rules/no-thenable.js index 6c30a3ee4d..3dfab691ac 100644 --- a/rules/no-thenable.js +++ b/rules/no-thenable.js @@ -84,7 +84,7 @@ const cases = [ { selector: 'ExportNamedDeclaration > VariableDeclaration.declaration', messageId: MESSAGE_ID_EXPORT, - getNodes: (node, context) => context.getDeclaredVariables(node).flatMap(({name, identifiers}) => name === 'then' ? identifiers : []), + getNodes: (node, context) => context.getSourceCode().getDeclaredVariables(node).flatMap(({name, identifiers}) => name === 'then' ? identifiers : []), }, ]; diff --git a/rules/prefer-export-from.js b/rules/prefer-export-from.js index d4a422e1fc..729d859f5f 100644 --- a/rules/prefer-export-from.js +++ b/rules/prefer-export-from.js @@ -170,7 +170,7 @@ function getFixFunction({ }; } -function getExported(identifier, context, sourceCode) { +function getExported(identifier, sourceCode) { const {parent} = identifier; switch (parent.type) { case 'ExportDefaultDeclaration': { @@ -201,7 +201,7 @@ function getExported(identifier, context, sourceCode) { && parent.parent.declarations.length === 1 && parent.parent.declarations[0] === parent && parent.parent.parent.type === 'ExportNamedDeclaration' - && isVariableUnused(parent, context) + && isVariableUnused(parent, sourceCode) ) { return { node: parent.parent.parent, @@ -217,8 +217,8 @@ function getExported(identifier, context, sourceCode) { } } -function isVariableUnused(node, context) { - const variables = context.getDeclaredVariables(node); +function isVariableUnused(node, sourceCode) { + const variables = sourceCode.getDeclaredVariables(node); /* c8 ignore next 3 */ if (variables.length !== 1) { @@ -270,10 +270,10 @@ function getImported(variable, sourceCode) { } } -function getExports(imported, context, sourceCode) { +function getExports(imported, sourceCode) { const exports = []; for (const {identifier} of imported.variable.references) { - const exported = getExported(identifier, context, sourceCode); + const exported = getExported(identifier, sourceCode); if (!exported) { continue; @@ -327,7 +327,7 @@ function create(context) { }, * 'Program:exit'(program) { for (const importDeclaration of importDeclarations) { - let variables = context.getDeclaredVariables(importDeclaration); + let variables = sourceCode.getDeclaredVariables(importDeclaration); if (variables.some(variable => variable.defs.length !== 1 || variable.defs[0].parent !== importDeclaration)) { continue; @@ -335,7 +335,7 @@ function create(context) { variables = variables.map(variable => { const imported = getImported(variable, sourceCode); - const exports = getExports(imported, context, sourceCode); + const exports = getExports(imported, sourceCode); return { variable, diff --git a/rules/prefer-keyboard-event-key.js b/rules/prefer-keyboard-event-key.js index 5999597a5a..b79754a4b3 100644 --- a/rules/prefer-keyboard-event-key.js +++ b/rules/prefer-keyboard-event-key.js @@ -25,7 +25,7 @@ const getEventNodeAndReferences = (context, node) => { switch (callback?.type) { case 'ArrowFunctionExpression': case 'FunctionExpression': { - const eventVariable = context.getDeclaredVariables(callback)[0]; + const eventVariable = context.getSourceCode().getDeclaredVariables(callback)[0]; const references = eventVariable?.references; return { event: callback.params[0], diff --git a/rules/prefer-object-from-entries.js b/rules/prefer-object-from-entries.js index e78030e789..3625207abd 100644 --- a/rules/prefer-object-from-entries.js +++ b/rules/prefer-object-from-entries.js @@ -189,7 +189,7 @@ function create(context) { } const [firstParameter] = callbackFunction.params; - const variables = context.getDeclaredVariables(callbackFunction); + const variables = sourceCode.getDeclaredVariables(callbackFunction); const firstParameterVariable = variables.find(variable => variable.identifiers.length === 1 && variable.identifiers[0] === firstParameter); if (!firstParameterVariable || firstParameterVariable.references.length !== 1) { return; diff --git a/rules/prefer-optional-catch-binding.js b/rules/prefer-optional-catch-binding.js index 94504eb2bf..17b37b541f 100644 --- a/rules/prefer-optional-catch-binding.js +++ b/rules/prefer-optional-catch-binding.js @@ -18,7 +18,8 @@ const selector = [ /** @param {import('eslint').Rule.RuleContext} context */ const create = context => ({ [selector](node) { - const variables = context.getDeclaredVariables(node.parent); + const sourceCode = context.getSourceCode(); + const variables = sourceCode.getDeclaredVariables(node.parent); if (variables.some(variable => variable.references.length > 0)) { return; @@ -31,7 +32,6 @@ const create = context => ({ messageId: type === 'Identifier' ? MESSAGE_ID_WITH_NAME : MESSAGE_ID_WITHOUT_NAME, data: {name}, * fix(fixer) { - const sourceCode = context.getSourceCode(); const tokenBefore = sourceCode.getTokenBefore(node); assertToken(tokenBefore, { test: isOpeningParenToken, diff --git a/rules/template-indent.js b/rules/template-indent.js index ec55859963..ea045fee13 100644 --- a/rules/template-indent.js +++ b/rules/template-indent.js @@ -99,8 +99,8 @@ const create = context => { } } - const ancestry = context.getAncestors().reverse(); - const shouldIndent = selectors.some(selector => esquery.matches(node, esquery.parse(selector), ancestry)); + const ancestors = sourceCode.getAncestors(node).reverse(); + const shouldIndent = selectors.some(selector => esquery.matches(node, esquery.parse(selector), ancestors)); if (shouldIndent) { indentTemplateLiteralNode(node);