semi-style

强制分号的位置

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

通常,分号位于行尾。但是,在无分号样式中,分号位于行首。此规则强制分号位于配置的位置。

¥Generally, semicolons are at the end of lines. However, in semicolon-less style, semicolons are at the beginning of lines. This rule enforces that semicolons are at the configured location.

规则详情

¥Rule Details

此规则报告分号周围的行终止符。

¥This rule reports line terminators around semicolons.

该规则有一个选项。

¥This rule has an option.

{
    "semi-style": ["error", "last"],
}
  • "last"(默认)强制分号位于语句末尾。

    ¥"last" (Default) enforces that semicolons are at the end of statements.

  • "first" 强制分号位于语句开头。即使你使用此选项,for 循环头 (for(a;b;c){}) 的分号也应位于行尾。

    ¥"first" enforces that semicolons are at the beginning of statements. Semicolons of for loop heads (for(a;b;c){}) should be at the end of lines even if you use this option.

使用 "last" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with "last" option:

在线运行
/*eslint semi-style: ["error", "last"]*/

foo()
;[1, 2, 3].forEach(bar)

for (
    var i = 0
    ; i < 10
    ; ++i
) {
    foo()
}

class C {
    static {
        foo()
        ;bar()
    }
}

使用 "last" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with "last" option:

在线运行
/*eslint semi-style: ["error", "last"]*/

foo();
[1, 2, 3].forEach(bar)

for (
    var i = 0;
    i < 10;
    ++i
) {
    foo()
}

class C {
    static {
        foo();
        bar()
    }
}

使用 "first" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with "first" option:

在线运行
/*eslint semi-style: ["error", "first"]*/

foo();
[1, 2, 3].forEach(bar)

for (
    var i = 0
    ; i < 10
    ; ++i
) {
    foo()
}

class C {
    static {
        foo();
        bar()
    }
}

使用 "first" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with "first" option:

在线运行
/*eslint semi-style: ["error", "first"]*/

foo()
;[1, 2, 3].forEach(bar)

for (
    var i = 0;
    i < 10;
    ++i
) {
    foo()
}

class C {
    static {
        foo()
        ;bar()
    }
}

何时不使用

¥When Not To Use It

如果你不想通知分号的位置,那么禁用此规则是安全的。

¥If you don’t want to notify the location of semicolons, then it’s safe to disable this rule.

版本

此规则是在 ESLint v4.0.0-beta.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号