Skip to content

Commit

Permalink
Use SourceCode#{getDeclaredVariables,getAncestors}, require `eslint…
Browse files Browse the repository at this point in the history
…@>=8.38.0` (#2066)
  • Loading branch information
fisker authored Apr 10, 2023
1 parent f5beccb commit 04af765
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -94,7 +94,7 @@
"yaml": "^2.2.1"
},
"peerDependencies": {
"eslint": ">=8.37.0"
"eslint": ">=8.38.0"
},
"ava": {
"files": [
Expand Down
12 changes: 6 additions & 6 deletions rules/consistent-destructuring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
});
},
Expand All @@ -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;
Expand All @@ -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 =>
Expand Down
16 changes: 8 additions & 8 deletions rules/no-array-for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion rules/no-thenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : []),
},
];

Expand Down
16 changes: 8 additions & 8 deletions rules/prefer-export-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function getFixFunction({
};
}

function getExported(identifier, context, sourceCode) {
function getExported(identifier, sourceCode) {
const {parent} = identifier;
switch (parent.type) {
case 'ExportDefaultDeclaration': {
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -327,15 +327,15 @@ 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;
}

variables = variables.map(variable => {
const imported = getImported(variable, sourceCode);
const exports = getExports(imported, context, sourceCode);
const exports = getExports(imported, sourceCode);

return {
variable,
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-keyboard-event-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-object-from-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions rules/prefer-optional-catch-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions rules/template-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 04af765

Please sign in to comment.