Skip to content
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

Rule proposal: no-empty-array-push #1382

Open
Zamiell opened this issue Jun 27, 2021 · 2 comments
Open

Rule proposal: no-empty-array-push #1382

Zamiell opened this issue Jun 27, 2021 · 2 comments

Comments

@Zamiell
Copy link
Contributor

Zamiell commented Jun 27, 2021

I just found the following bug in some of my TypeScript code:

myArray.push();

This was a typo, and I had actually intended to write:

myArray.push(foo);

I can't imagine that a JavaScript/TypeScript programmer would ever intend to call the push method with 0 arguments.
Thus, it seems to me that this would always be a bug.
Subsequently, some kind of linter rule should automatically catch this bug.


Fail

myArray.push();

Pass

myArray.push(1);
// Even though the array is empty, this still does not count as an "empty" array push,
// because we are still explicitly providing at least one argument
const anotherArray = [];
myArray.push(...anotherArray);
@sindresorhus
Copy link
Owner

I think #1356 would solve this too.

@Zamiell
Copy link
Contributor Author

Zamiell commented Feb 25, 2023

Today I realized that this pitfall applies to all variadic functions, not just Array.push.
So, I made the rule: https://github.com/IsaacScript/isaacscript/blob/main/packages/eslint-plugin-isaacscript/docs/rules/require-variadic-function-argument.md

Feel free to copy paste it into unicorn if you want, as I think it is a really good rule.

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

No branches or pull requests

2 participants