Gimy boya
Gimy boya

Reputation: 477

What problems are associated with automating ESLint with the `--fix` flag?

Is it good practice to automate ESLint with the --fix flag, or will that create problems in the long run?

For example, I'm using npm and in the "prestart" script. This means ESLint runs each time I run npm start and fixes any fixable errors in my code. What issues can this cause?

Upvotes: 5

Views: 1340

Answers (1)

Nick Bartlett
Nick Bartlett

Reputation: 4975

The only real problem this could cause is that the code that ends up running on npm start may not be the same code that you've written. You'd have ESLint making changes to your code right before you start your application, which could lead (potentially) to unexpected behavior. This depends on how your project is set up and how your application is deployed.

In the list of ESLint rules the ones that can be autofixed have the wrench next to them; it can change more than just spacing and semicolons. While ESLint (when running with --fix) tries hard not to break your code functionally, any time you're manipulating your code in the time between writing it and running it you are putting yourself at risk of changing the behavior of your codebase. ESLint tries to ensure that they won't autofix your code in a way which will break/change the behavior of anything, but bugs/accidents can occur which may change your code enough to make it behave differently. As an aside, here's an interesting conversation on autofix and how it might change.

If you're running ESLint as part of your npm test suite and not deploying your application unless your lint rules are passing, it's not as much of an issue (since this won't happen on your deployed app, only locally really; you're keeping your app from deploying if it finds code not matching your style guide).

Otherwise, --fix can be useful in allowing people to code how they want to while maintaining a consistent style and avoiding "problematic patterns" in the codebase semi-automatically. While it doesn't fix every rule, it can make development somewhat smoother for teams. If desired, you could even run this autofixing in a git pre-commit hook so that you're sure the code being committed is the fixed version.

Example: Git pre-commit ESLint hook

Upvotes: 4

Related Questions