Skip to content

Commit 43e4c0a

Browse files
ianobermillerfreaktechnik
authored andcommitted
Fix Error when mapCallback is not an identifier or a function expression
1 parent f8d9006 commit 43e4c0a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

rules/from-map.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type"),
1010
{ name: 'index' }
1111
];
1212

13+
function isFunction(node) {
14+
return node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression";
15+
}
16+
1317
module.exports = {
1418
meta: {
1519
docs: {
@@ -35,8 +39,10 @@ module.exports = {
3539
node = callee.parent;
3640

3741
if(mapCallback.type === "Identifier" ||
38-
mapCallback.params.length > ALL_PARAMS.length ||
39-
mapCallback.params.some((parameter) => parameter.type === "RestElement")
42+
(isFunction(mapCallback) && (
43+
mapCallback.params.length > ALL_PARAMS.length ||
44+
mapCallback.params.some((parameter) => parameter.type === "RestElement")
45+
))
4046
) {
4147
return;
4248
}

test/helpers/from-map-test-cases.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ export default {
7171
line: 1
7272
} ],
7373
output: 'Array.from(iterable, (item, index) => ((u, i) => u.name)((mapper).call(this, item, index), index))'
74+
},
75+
{
76+
code: 'Array.from(iterable).map(getValue ? (u, i) => getValue(i) : (u, i) => i)',
77+
errors: [ {
78+
messageId: 'useMapCb',
79+
column: 1,
80+
line: 1
81+
} ],
82+
output: 'Array.from(iterable, getValue ? (u, i) => getValue(i) : (u, i) => i)'
7483
}
7584
]
7685
};

0 commit comments

Comments
 (0)