Skip to content

fix: Additional checks for prefer-flat #46

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 44 additions & 15 deletions rules/prefer-flat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable no-magic-numbers */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, but these rules are there for a reason.

/**
* @author Martin Giger
* @license MIT
Expand All @@ -7,10 +9,9 @@
//TODO no works.

const firstElement = (arr) => {
const [ el ] = arr;
return el;
},
SECOND = 1;
const [ el ] = arr;
return el;
};

module.exports = {
meta: {
Expand All @@ -37,18 +38,46 @@ module.exports = {
});
},
'CallExpression[callee.type="MemberExpression"][callee.property.name="reduce"] > *:function > CallExpression[callee.type="MemberExpression"][callee.property.name="concat"]'(node) {
if(node.parent.parent.arguments.length > SECOND && node.parent.parent.arguments[SECOND].type === "ArrayExpression" &&
firstElement(node.arguments).name === node.parent.params[SECOND].name &&
node.callee.object.name === firstElement(node.parent.params).name) {
context.report({
node: node.parent.parent,
message: "Use flat to flatten an array",
fix(fixer) {
const sourceCode = context.getSourceCode();
return fixer.replaceText(node.parent.parent, `${sourceCode.getText(node.parent.parent.callee.object)}.flat()`);
}
});
const concatFunction = node,
reduceFunction = node.parent.parent,
reduceCallbackParams = node.parent.params;

// reduce function must have two args
if(reduceFunction.arguments.length !== 2) {
return;
}

// reduce callback arguments must be 2 and all must be Identifier
if(reduceCallbackParams.length !== 2 || reduceCallbackParams[0].type !== 'Identifier' || reduceCallbackParams[1].type !== 'Identifier') {
return;
}

// reduce function must have empty array as initial values
if(reduceFunction.arguments[1].type !== "ArrayExpression" || reduceFunction.arguments[1].elements.length !== 0) {
return;
}

// concat function must have one argument which must be variable
if(concatFunction.arguments.length !== 1 || concatFunction.arguments[0].type !== 'Identifier') {
return;
}

// concat callee object name must be same as reducer callback accumulator and
// concat argument must be same as reducer callback item
if(concatFunction.arguments[0].name !== reduceCallbackParams[1].name ||
node.callee.object.name !== reduceCallbackParams[0].name
) {
return;
}

context.report({
node: node.parent.parent,
message: "Use flat to flatten an array",
fix(fixer) {
const sourceCode = context.getSourceCode();
return fixer.replaceText(node.parent.parent, `${sourceCode.getText(node.parent.parent.callee.object)}.flat()`);
}
});
}
};
}
Expand Down
5 changes: 4 additions & 1 deletion test/rules/prefer-flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ ruleTester.run('prefer-flat', rule, {
valid: [
'array.flat()',
'array.reduce((p, n) => n.concat(p), [])',
'array.reduce((p, n) => n + p, 0)'
'array.reduce((p, n) => n + p, 0)',
'array.reduce((p, []) => p.concat({}), [])',
'array.reduce((p, n) => p.concat(n), [1])',
'array.reduce((p, n) => p.concat(n, n), [])'
],
invalid: [
{
Expand Down