Skip to content

Commit

Permalink
Fix Error when mapCallback is not an identifier or a function expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ianobermiller authored and freaktechnik committed Sep 12, 2023
1 parent f8d9006 commit 43e4c0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions rules/from-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type"),
{ name: 'index' }
];

function isFunction(node) {
return node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression";
}

module.exports = {
meta: {
docs: {
Expand All @@ -35,8 +39,10 @@ module.exports = {
node = callee.parent;

if(mapCallback.type === "Identifier" ||
mapCallback.params.length > ALL_PARAMS.length ||
mapCallback.params.some((parameter) => parameter.type === "RestElement")
(isFunction(mapCallback) && (
mapCallback.params.length > ALL_PARAMS.length ||
mapCallback.params.some((parameter) => parameter.type === "RestElement")
))
) {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions test/helpers/from-map-test-cases.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ export default {
line: 1
} ],
output: 'Array.from(iterable, (item, index) => ((u, i) => u.name)((mapper).call(this, item, index), index))'
},
{
code: 'Array.from(iterable).map(getValue ? (u, i) => getValue(i) : (u, i) => i)',
errors: [ {
messageId: 'useMapCb',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, getValue ? (u, i) => getValue(i) : (u, i) => i)'
}
]
};

0 comments on commit 43e4c0a

Please sign in to comment.