Skip to content

Commit

Permalink
[Refactor] better use of .reverse, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 20, 2023
1 parent b4b7497 commit ca162fd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/rules/jsx-no-bind.js
Expand Up @@ -123,9 +123,9 @@ module.exports = {
}

function getBlockStatementAncestors(node) {
return context.getAncestors(node).reverse().filter(
return context.getAncestors(node).filter(
(ancestor) => ancestor.type === 'BlockStatement'
);
).reverse();
}

function reportVariableViolation(node, name, blockStart) {
Expand Down
3 changes: 1 addition & 2 deletions lib/util/Components.js
Expand Up @@ -671,8 +671,7 @@ function componentRule(rule, context) {
}

// Try to find the component using variable references
const refs = variableInScope.references;
refs.some((ref) => {
variableInScope.references.some((ref) => {
let refId = ref.identifier;
if (refId.parent && refId.parent.type === 'MemberExpression') {
refId = refId.parent;
Expand Down
7 changes: 5 additions & 2 deletions lib/util/makeNoMethodSetStateRule.js
Expand Up @@ -5,6 +5,8 @@

'use strict';

const findLast = require('array.prototype.findlast');

const docsUrl = require('./docsUrl');
const report = require('./report');
const testReactVersion = require('./version').testReactVersion;
Expand Down Expand Up @@ -91,9 +93,10 @@ module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafe
) {
return;
}
const ancestors = context.getAncestors(callee).reverse();
const ancestors = context.getAncestors(callee);
let depth = 0;
ancestors.some((ancestor) => {
findLast(ancestors, (ancestor) => {
// ancestors.some((ancestor) => {
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
depth += 1;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/util/variable.js
Expand Up @@ -5,6 +5,8 @@

'use strict';

const toReversed = require('array.prototype.toreversed');

/**
* Search a particular variable in a list
* @param {Array} variables The variables list.
Expand Down Expand Up @@ -47,9 +49,8 @@ function variablesInScope(context) {
variables = scope.childScopes[0].childScopes[0].variables.concat(variables);
}
}
variables.reverse();

return variables;
return toReversed(variables);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -26,7 +26,9 @@
"bugs": "https://github.com/jsx-eslint/eslint-plugin-react/issues",
"dependencies": {
"array-includes": "^3.1.7",
"array.prototype.findlast": "^1.2.3",
"array.prototype.flatmap": "^1.3.2",
"array.prototype.toreversed": "^1.1.2",
"array.prototype.tosorted": "^1.1.2",
"doctrine": "^2.1.0",
"es-iterator-helpers": "^1.0.15",
Expand Down

0 comments on commit ca162fd

Please sign in to comment.