Skip to content

Rule proposal: Prefer array.flat() over array.flat(1) #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jonahsnider opened this issue Dec 4, 2021 · 5 comments · Fixed by #2618
Closed

Rule proposal: Prefer array.flat() over array.flat(1) #1629

jonahsnider opened this issue Dec 4, 2021 · 5 comments · Fixed by #2618

Comments

@jonahsnider
Copy link
Contributor

jonahsnider commented Dec 4, 2021

Description

Array.prototype.flat uses 1 as the argument if you pass undefined or do not provide a value.
This means that .flat(1) is the same as .flat(), making the argument unnecessary.

Fail

array.flat(1);
const one = 1;
array.flat(one);

Pass

array.flat();
const one = 1;
array.flat();
let levels = 1; // Mutable variable, might be reassigned later
array.flat(levels);
@bmish
Copy link
Contributor

bmish commented Dec 4, 2021

Would it make sense to just add this check to the existing prefer-array-flat rule?

@sindresorhus
Copy link
Owner

Not sure it belongs in prefer-array-flat. That rule is not about call style.

Would maybe be a better fit for #1356?

@fisker
Copy link
Collaborator

fisker commented Dec 17, 2021

We already have require-array-join-separator and require-number-to-fixed-digits-argument doing similar stuff, we can add this before we implement #1356.

@fisker
Copy link
Collaborator

fisker commented Dec 17, 2021

I wonder if someone prefer use array.flat(1), it's explicit, if it does, we can add option for it.

@fregante
Copy link
Collaborator

fregante commented Dec 21, 2021

All the rules mentioned here are a variation of "enforce or avoid the default value".

  • avoids default: this suggestion
  • enforces default: require-array-join-separator
  • enforces default: require-number-to-fixed-digits-argument
  • avoids default: no-useless-undefined

It almost feels like they belong together. I'm not particularly a fan of enforcing default values, but they do make it clearer sometimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants