Index

semi-style

强制分号的位置

🔧 Fixable

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

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

通常,分号位于行末。然而,在无分号风格中,分号位于行首。此规则强制分号位于配置的位置。

🌐 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"(默认)强制要求分号位于语句末尾。
  • "first" 强制要求分号位于语句的开头。即使使用此选项,for 循环头(for(a;b;c){})的分号也应位于行末。

使用 "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 中引入。

资源