Skip to content

Instantly share code, notes, and snippets.

@faustienf
Created July 9, 2024 12:00
Show Gist options
  • Save faustienf/5fbafd0106bd4ffc17bb369892a56e91 to your computer and use it in GitHub Desktop.
Save faustienf/5fbafd0106bd4ffc17bb369892a56e91 to your computer and use it in GitHub Desktop.
module.exports = {
create: function (context) {
const checkSpreadStatement = (node) => {
if (!['ObjectExpression', 'ArrayExpression'].includes(node.type)) {
return;
}
const properties = node.properties || node.elements;
properties.forEach((property) => {
if (property.type === 'SpreadElement') {
context.report({
node,
message: 'Spread operator for accumulator in reduce is not allowed',
});
}
});
};
const checkIsReduce = (node) =>
node.callee && node.callee.property && node.callee.property.name === 'reduce';
return {
CallExpression(node) {
if (!checkIsReduce(node)) {
return;
}
const callback = node.arguments[0];
const isCallbackeExpression =
callback && ['FunctionExpression', 'ArrowFunctionExpression'].includes(callback.type);
if (!isCallbackeExpression) {
return;
}
/**
* () => {
* return ...
* }
*/
if (callback.body.type === 'BlockStatement') {
const callbackBody = callback.body.body;
callbackBody.forEach((statement) => {
const isReturnExpression = statement.type === 'ReturnStatement' && statement.argument;
if (!isReturnExpression) {
return;
}
checkSpreadStatement(statement.argument);
});
}
/**
* () => [...]
* () => ({...})
*/
checkSpreadStatement(callback.body);
},
};
},
};
@faustienf
Copy link
Author


Object.keys({}).reduce((acc, key) => [...acc, key], []);


Object.keys({}).reduce((acc, key) => {
  return [...acc, key];
}, []);


Object.keys({}).reduce(
  (acc, key) => ({
    ...acc,
    key
  }),
  {}
);


Object.keys({}).reduce((acc, key) => {
  return {
    ...acc,
    key
  };
}, {});


Object.keys({}).reduce((acc, key) => {
  acc[key] = key;
  return acc;
}, {});


Object.keys({}).reduce((acc, key) => {
  acc.push(key);
  return acc;
}, []);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment