Skip to content

Commit 6f10722

Browse files
MrHensindresorhus
authored andcommitted
Do not warn if .flat() has more than one parameter or if the one parameter is not 1 (#317)
1 parent 1e3bb8f commit 6f10722

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

rules/prefer-flat-map.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ const create = context => ({
9898
return;
9999
}
100100

101+
if (node.arguments.length > 1) {
102+
return;
103+
}
104+
105+
if (node.arguments.length === 1 && node.arguments[0].type === 'Literal' && node.arguments[0].value !== 1) {
106+
return;
107+
}
108+
101109
const parent = node.callee.object;
102110

103111
if (!isMethodNamed(parent, 'map')) {

test/prefer-flat-map.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ ruleTester.run('prefer-flat-map', rule, {
2626
outdent`
2727
let bar = [1,2,3].map(i => [i]);
2828
bar = bar.flat();
29-
`
29+
`,
30+
'const bar = [[1],[2],[3]].map(i => [i]).flat(2)',
31+
'const bar = [[1],[2],[3]].map(i => [i]).flat(1, null)'
3032
],
3133
invalid: [
3234
{
@@ -157,6 +159,11 @@ ruleTester.run('prefer-flat-map', rule, {
157159
output: 'let bar = [1,2,3] . flatMap( x => y ) // 🤪',
158160
errors: [error]
159161
},
162+
{
163+
code: 'const bar = [1,2,3].map(i => [i]).flat(1);',
164+
output: 'const bar = [1,2,3].flatMap(i => [i]);',
165+
errors: [error]
166+
},
160167
{
161168
code: outdent`
162169
const foo = bars

0 commit comments

Comments
 (0)