简体   繁体   English

从 ESLint 应用自动修复是否安全?

[英]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.例如,将 var 更改为 let 或 const 可能会导致不同的执行,因为 var 与 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.所以,我想知道是否有来自 ESLint 的任何自动修复规则可以更改代码,以便在应用--fix东西时执行不同。

Yes, it's safe, because the --fix flag does not fix all your JS issues [1].是的,这是安全的,因为--fix标志并不能解决所有的 JS 问题 [1]。 So you have to fix some eslint warnings/errors yourself.所以你必须自己修复一些 eslint 警告/错误。

[1]https://eslint.org/docs/user-guide/command-line-interface#--fix [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.我使用的代码库使用 Mithril v.0.2,它使用new关键字初始化组件的 controller。 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. new 关键字仅适用于构造函数,但eslint --fix命令(特别是prefer-arrow-callback用箭头函数替换了我的一些匿名函数。这导致了一些错误,因为箭头函数不是构造函数。

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.就我而言,我运行了排除该规则的 eslint autofix。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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