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

prefer-array-find and prefer-at: Fix crash on LHS zero index access #1373

Merged
merged 2 commits into from
Jun 21, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion rules/prefer-array-find.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';
const {isParenthesized, findVariable} = require('eslint-utils');
const {not, methodCallSelector} = require('./selectors/index.js');
const {
not,
methodCallSelector,
notLeftHandSideSelector
} = require('./selectors/index.js');
const getVariableIdentifiers = require('./utils/get-variable-identifiers.js');
const renameVariable = require('./utils/rename-variable.js');
const avoidCapture = require('./utils/avoid-capture.js');
Expand Down Expand Up @@ -51,6 +55,7 @@ const zeroIndexSelector = [
'[computed!=false]',
'[property.type="Literal"]',
'[property.raw="0"]',
notLeftHandSideSelector(),
methodCallSelector({
...filterMethodSelectorOptions,
path: 'object'
Expand Down
5 changes: 5 additions & 0 deletions rules/prefer-at.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
const {isNodeMatchesNameOrPath} = require('./utils/is-node-matches.js');
const needsSemicolon = require('./utils/needs-semicolon.js');
const shouldAddParenthesesToMemberExpressionObject = require('./utils/should-add-parentheses-to-member-expression-object.js');
const isLeftSideHand = require('./utils/is-left-hand-side.js');
const {
getNegativeIndexLengthNode,
removeLengthNode
Expand Down Expand Up @@ -85,6 +86,10 @@ function checkSliceCall(node) {

let firstElementGetMethod = '';
if (isZeroIndexAccess(node)) {
if (isLeftSideHand(node.parent)) {
return;
}

firstElementGetMethod = 'zero-index';
} else if (isArrayShiftCall(node)) {
firstElementGetMethod = 'shift';
Expand Down
1 change: 1 addition & 0 deletions rules/selectors/not-left-hand-side.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const not = require('./negation.js');
function notLeftHandSideSelector(path) {
const prefix = path ? `${path}.` : '';

// Keep logic sync with `../utils/is-left-hand-side.js`
return not([
`[${prefix}type="AssignmentExpression"] > .left`,
`[${prefix}type="UpdateExpression"] > .argument`,
Expand Down
13 changes: 13 additions & 0 deletions rules/utils/is-left-hand-side.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

// Keep logic sync with `../selector/not-left-hand-side.js`
const isLeftHandSide = node =>
(node.parent.type === 'AssignmentExpression' && node.parent.left === node) ||
(node.parent.type === 'UpdateExpression' && node.parent.argument === node) ||
(
node.parent.type === 'UnaryExpression' &&
node.parent.operator === 'delete' &&
node.parent.argument === node
);

module.exports = isLeftHandSide;
7 changes: 6 additions & 1 deletion test/prefer-array-find.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ test({
// More or less argument(s)
'array.filter()[0]',
'array.filter(foo, thisArgument, extraArgument)[0]',
'array.filter(...foo)[0]'
'array.filter(...foo)[0]',
// LHS
'array.filter(foo)[0] += 1',
'++ array.filter(foo)[0]',
'array.filter(foo)[0]--',
'delete array.filter(foo)[0]'
],
invalid: [
{
Expand Down
7 changes: 6 additions & 1 deletion test/prefer-at.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ test.snapshot({
'array.slice(-1).shift?.()',
'array.slice(-1)?.shift()',
'array.slice(-1).shift(...[])',
'new array.slice(-1).shift()'
'new array.slice(-1).shift()',
// LHS
'array.slice(-1)[0] += 1',
'++ array.slice(-1)[0]',
'array.slice(-1)[0] --',
'delete array.slice(-1)[0]'
],
invalid: [
'array.slice(-1)[0]',
Expand Down