简体   繁体   中英

Is it safe to apply autofix from ESLint?

I'd like to format several files but I'm worried about breaking code .

When I'm saying if it is safe for not, I'm saying that the execution of the code before and after is exactly the same.

For instance, there are rules that look pretty safe to be applied (like indenting). I suppose that changing the number of spaces won't affect the execution of the code.

Then, there are rules that don't look so safe. For instance, changing var to let or const could cause a different execution as var is not exactly the same as let.

So, I'm wondering if there are any auto-fix rule from ESLint that can change the code so the execution is different when applied the --fix thing.

Yes, it's safe, because the --fix flag does not fix all your JS issues [1]. So you have to fix some eslint warnings/errors yourself.

[1]https://eslint.org/docs/user-guide/command-line-interface#--fix

Mostly yes, but I recently came across an issue when using the autofix option. The codebase I work with uses Mithril v.0.2, which initializes the component's controller using the new keyword. The new keyword only works on constructors, but the eslint --fix command (specifically the prefer-arrow-callback replaced some of my anonymous functions with arrow functions. Which caused some errors, since arrow functions are not constructors.

So my case was like this:

 const component = {}; // My method in this object used to be a regular function like the one below component.normalFunction = function () { return { test: true }; } // Eslint autofix changed it to be an arrow function component.arrowFunction = () => { return { test: true }; } try { new component.normalFunction(); new component.arrowFunction(); } catch (err) { document.body.prepend(err); }

So far that was the only issue I have found with the autofix rule. In my case I ran eslint autofix excluding that rule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM