简体   繁体   English

为什么 ESLint 会警告我认为有用的分号?

[英]Why is ESLint warning about a semicolon where I think it's useful?

    fetch_roles () {
      axios.get('/roles')
        .then((response) => {
          this.is_loaded = true; <-- ES lint hate this semicolon?! 🤔
          this.pageData = response
        })
        .catch((error) => {
          alert(`Error: ${error}`)
        })
    }

在此处输入图像描述

I get ESLint: Extra semicolon.(semi) for the semicolon shown above.我得到ESLint: Extra semicolon.(semi)用于上面显示的分号。 Isn't the semicolon good to have there in case we minify?如果我们缩小,分号不是很好吗?

I get ESLint: Extra semicolon.(semi) for the above pointed semicolon.我得到 ESLint: Extra semicolon.(semi) 用于上面的分号。

ESLint can be configured to require, ignore, or forbid optional semi-colons . ESLint 可以配置为要求、忽略或禁止可选的分号 Your configuration has set them to forbidden.您的配置已将它们设置为禁止。

Isn't the semicolon better be there incase we minify?如果我们缩小,分号不是更好吗?

If minifying causes a problem when optional semi-colons are absent then you need a better minifier.如果在缺少可选分号时缩小导致问题,那么您需要一个更好的缩小器。

It sounds like your linter configuration is currently set up to forbid semicolons.听起来您的 linter 配置当前设置为禁止使用分号。

If you're an expert and can identify potential bugs due to Automatic Semicolon Insertion rules at a glance, you can keep your current rules and delete the semicolon from that line safely.如果您是专家并且可以一眼识别由于自动分号插入规则而导致的潜在错误,您可以保留当前规则并安全地删除该行中的分号。

Otherwise, you might find it more beneficial to change the semi rule to require semicolons when appropriate instead, which can prevent hard-to-understand bugs from popping up.否则,您可能会发现在适当的时候将semi规则更改为要求使用分号会更有益,这可以防止出现难以理解的错误。

To do this, change the semi rule to always instead of never .为此,请将semi规则更改always而不是never You'll also need to add semicolons to the following lines:您还需要在以下行中添加分号:

this.pageData = response
alert(`Error: ${error}`)

The existence (or lack thereof) of a semicolon will not really affect minification.分号的存在(或缺乏)不会真正影响缩小。 If no semicolon on a line is causing a bug due to unintuitive ASI behavior, that bug will be present regardless if the source file is run or if the minified file is run.如果由于不直观的 ASI 行为而导致一行中没有分号导致错误,则无论是运行源文件还是运行缩小文件,都会出现该错误。

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

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