Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 21, 2020
1 parent 8ef866e commit 356f78c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 15 additions & 10 deletions rules/prefer-array-find.js
Expand Up @@ -132,14 +132,14 @@ const getDestructuringLeftAndRight = node => {
}

return {};
}
};

const fixDestructuring = (node, source, fixer) => {
const {left} = getDestructuringLeftAndRight(node);
const [element] = left.elements;

const leftText = source.getText(element.type === 'AssignmentPattern' ? element.left : element);
const fixes = [ fixer.replaceText(left, leftText) ];
const fixes = [fixer.replaceText(left, leftText)];

// `AssignmentExpression` always starts with `[` or `(`, so we don't need check ASI
if (assignmentNeedParenthesize(node, source)) {
Expand All @@ -149,7 +149,9 @@ const fixDestructuring = (node, source, fixer) => {

return fixes;
};

const hasDefaultValue = node => getDestructuringLeftAndRight(node).left.elements[0].type === 'AssignmentPattern';

const fixDestructuringDefaultValue = (node, source, fixer, operator) => {
const {left, right} = getDestructuringLeftAndRight(node);
const [element] = left.elements;
Expand All @@ -164,31 +166,31 @@ const fixDestructuringDefaultValue = (node, source, fixer, operator) => {
};

const fixDestructuringAndReplaceFilter = (source, node) => {
const property = getDestructuringLeftAndRight(node).right.callee.property;
const {property} = getDestructuringLeftAndRight(node).right.callee;

let suggest;
let fix;

if (hasDefaultValue(node)) {
suggest = [
{ messageId: MESSAGE_ID_USE_NULLISH_COALESCING_OPERATOR, operator: '??' },
{ messageId: MESSAGE_ID_USE_LOGICAL_OR_OPERATOR, operator: '||' },
{operator: '??', messageId: MESSAGE_ID_USE_NULLISH_COALESCING_OPERATOR},
{operator: '||', messageId: MESSAGE_ID_USE_LOGICAL_OR_OPERATOR}
].map(({messageId, operator}) => ({
messageId,
fix: fixer => [
fixer.replaceText(property, 'find'),
fixDestructuringDefaultValue(node, source, fixer, operator),
...fixDestructuring(node, source, fixer)
]
}))
}));
} else {
fix = fixer => [
fixer.replaceText(property, 'find'),
...fixDestructuring(node, source, fixer)
]
];
}

return { fix, suggest };
return {fix, suggest};
};

const isAccessingZeroIndex = node =>
Expand Down Expand Up @@ -280,12 +282,15 @@ const create = context => {
const fixes = [
fixer.replaceText(node.init.callee.property, 'find')
];

for (const node of zeroIndexNodes) {
fixes.push(fixer.removeRange([node.object.range[1], node.range[1]]))
fixes.push(fixer.removeRange([node.object.range[1], node.range[1]]));
}

for (const node of destructuringNodes) {
fixes.push(...fixDestructuring(node, source, fixer))
fixes.push(...fixDestructuring(node, source, fixer));
}

return fixes;
};
}
Expand Down
4 changes: 4 additions & 0 deletions test/prefer-array-find.js
Expand Up @@ -606,6 +606,10 @@ ruleTester.run('prefer-array-find', rule, {
'const foo = array.filter(bar); [,first] = foo;',
'const foo = array.filter(bar); const [...first] = foo;',
'const foo = array.filter(bar); [...first] = foo;',
outdent`
const foo = array.filter(bar);
function a([bar] = foo) {}
`,

// Test `.filter()`
// Not `CallExpression`
Expand Down

0 comments on commit 356f78c

Please sign in to comment.