Created
July 9, 2024 12:00
-
-
Save faustienf/5fbafd0106bd4ffc17bb369892a56e91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}, | |
}; | |
}, | |
}; |
Author
faustienf
commented
Jul 10, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment