Skip to content

Commit

Permalink
Do not warn if .flat() has more than one parameter or if the one pa…
Browse files Browse the repository at this point in the history
…rameter is not `1` (#317)
  • Loading branch information
MrHen authored and sindresorhus committed Jun 10, 2019
1 parent 1e3bb8f commit 6f10722
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions rules/prefer-flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ const create = context => ({
return;
}

if (node.arguments.length > 1) {
return;
}

if (node.arguments.length === 1 && node.arguments[0].type === 'Literal' && node.arguments[0].value !== 1) {
return;
}

const parent = node.callee.object;

if (!isMethodNamed(parent, 'map')) {
Expand Down
9 changes: 8 additions & 1 deletion test/prefer-flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ ruleTester.run('prefer-flat-map', rule, {
outdent`
let bar = [1,2,3].map(i => [i]);
bar = bar.flat();
`
`,
'const bar = [[1],[2],[3]].map(i => [i]).flat(2)',
'const bar = [[1],[2],[3]].map(i => [i]).flat(1, null)'
],
invalid: [
{
Expand Down Expand Up @@ -157,6 +159,11 @@ ruleTester.run('prefer-flat-map', rule, {
output: 'let bar = [1,2,3] . flatMap( x => y ) // 🤪',
errors: [error]
},
{
code: 'const bar = [1,2,3].map(i => [i]).flat(1);',
output: 'const bar = [1,2,3].flatMap(i => [i]);',
errors: [error]
},
{
code: outdent`
const foo = bars
Expand Down

0 comments on commit 6f10722

Please sign in to comment.