Index

semi

要求或禁止使用分号而不是 ASI

🔧 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

JavaScript 不要求在每个语句的末尾使用分号。在许多情况下,JavaScript 引擎可以判断某个位置应该有分号,并会自动添加它。这个特性被称为自动分号插入 (ASI),并被认为是 JavaScript 中较具争议的特性之一。例如,以下几行都是有效的:

🌐 JavaScript doesn’t require semicolons at the end of each statement. In many cases, the JavaScript engine can determine that a semicolon should be in a certain spot and will automatically add it. This feature is known as automatic semicolon insertion (ASI) and is considered one of the more controversial features of JavaScript. For example, the following lines are both valid:

var name = "ESLint"
var website = "eslint.org";

在第一行,JavaScript 引擎会自动插入分号,因此这不被认为是语法错误。JavaScript 引擎仍然知道如何解释这一行,并且知道行末表示语句的结束。

🌐 On the first line, the JavaScript engine will automatically insert a semicolon, so this is not considered a syntax error. The JavaScript engine still knows how to interpret the line and knows that the line end indicates the end of the statement.

在关于ASI的辩论中,通常有两种观点。第一种观点是我们应该把ASI当作不存在,并始终手动添加分号。其理由是,总是添加分号比尝试记住何时需要或不需要分号更容易,从而减少引入错误的可能性。

🌐 In the debate over ASI, there are generally two schools of thought. The first is that we should treat ASI as if it didn’t exist and always include semicolons manually. The rationale is that it’s easier to always include semicolons than to try to remember when they are or are not required, and thus decreases the possibility of introducing an error.

然而,对于使用分号的人来说,ASI 机制有时可能会很棘手。例如,考虑以下代码:

🌐 However, the ASI mechanism can sometimes be tricky to people who are using semicolons. For example, consider this code:

return
{
    name: "ESLint"
};

这可能看起来像一个返回对象字面量的 return 语句,然而,JavaScript 引擎将把这段代码解释为:

🌐 This may look like a return statement that returns an object literal, however, the JavaScript engine will interpret this code as:

return;
{
    name: "ESLint";
}

实际上,在 return 语句之后插入了一个分号,导致其下方的代码(块内的带标签的字面量)无法访问。该规则和 no-unreachable 规则将保护你的代码免受此类情况影响。

🌐 Effectively, a semicolon is inserted after the return statement, causing the code below it (a labeled literal inside a block) to be unreachable. This rule and the no-unreachable rule will protect your code from such cases.

争论的另一方则认为,由于分号是自动插入的,它们是可选的,不需要手动插入。然而,ASI 机制对于不使用分号的人来说也可能很棘手。例如,考虑以下代码:

🌐 On the other side of the argument are those who say that since semicolons are inserted automatically, they are optional and do not need to be inserted manually. However, the ASI mechanism can also be tricky to people who don’t use semicolons. For example, consider this code:

var globalCounter = { }

(function () {
    var n = 0
    globalCounter.increment = function () {
        return ++n
    }
})()

在这个例子中,第一行之后不会插入分号,导致运行时错误(因为将一个空对象当作函数调用)。no-unexpected-multiline 规则可以保护你的代码免受此类情况的影响。

🌐 In this example, a semicolon will not be inserted after the first line, causing a run-time error (because an empty object is called as if it’s a function). The no-unexpected-multiline rule can protect your code from such cases.

虽然 ASI 允许你对编码风格有更多自由,但无论你是否使用分号,它也可能导致代码表现出意想不到的行为。因此,最好了解 ASI 何时发生、何时不发生,并让 ESLint 保护你的代码免受这些潜在的意外情况的影响。简而言之,正如 Isaac Schlueter 曾描述的,\n 字符总是结束一个语句(就像分号一样),除非以下情况之一为真:

🌐 Although ASI allows for more freedom over your coding style, it can also make your code behave in an unexpected way, whether you use semicolons or not. Therefore, it is best to know when ASI takes place and when it does not, and have ESLint protect your code from these potentially unexpected cases. In short, as once described by Isaac Schlueter, a \n character always ends a statement (just like a semicolon) unless one of the following is true:

  1. 该语句有未闭合的括号、数组字面量或对象字面量,或者以其他不是有效语句结束方式的方式结束。(例如,以 ., 结尾。)
  2. 这一行是 --++(在这种情况下,它将递减/递增下一个标记。)
  3. 它是一个for()while()doif()else,并且没有{
  4. 下一行以 [(+*/-,. 开头,或者以某个只能出现在单个表达式中两个标记之间的其他二元运算符开头。

规则详情

🌐 Rule Details

此规则强制使用一致的分号。

🌐 This rule enforces consistent use of semicolons.

选项

🌐 Options

该规则有两个选项,一个字符串选项和一个对象选项。

🌐 This rule has two options, a string option and an object option.

字符串选项:

🌐 String option:

  • "always"(默认)要求在语句末尾加分号
  • "never" 不允许在语句末尾使用分号(除非用来消除以 [(/+- 开头的语句的歧义)

对象选项(当 "always" 时):

🌐 Object option (when "always"):

  • "omitLastInOneLineBlock": true 不允许在大括号内的最后一个分号出现,如果大括号(以及因此包含的内容)在同一行内
  • "omitLastInOneLineClassBody": true 不允许在类体的最后一个分号出现,如果类体的大括号(因此类体的内容)在同一行时。

对象选项(当 "never" 时):

🌐 Object option (when "never"):

  • "beforeStatementContinuationChars": "any"(默认)在语句末尾如果缺少分号(或有分号)时会被忽略,前提是下一行以 [(/+- 开头。
  • "beforeStatementContinuationChars": "always"要求在语句末尾加分号,如果下一行以[(/+-开头。
  • "beforeStatementContinuationChars": "never" 不允许在语句末尾使用分号,即使这样不会产生自动分号插入(ASI)问题,即使下一行以 [(/+- 开头。

注意: beforeStatementContinuationChars 不适用于类字段,因为类字段不是语句。

always

使用默认 "always" 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default "always" option:

在线运行
/*eslint semi: ["error", "always"]*/

var name = "ESLint"

object.method = function() {
    // ...
}

class Foo {
    bar = 1
}

使用默认 "always" 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default "always" option:

在线运行
/*eslint semi: "error"*/

var name = "ESLint";

object.method = function() {
    // ...
};

class Foo {
    bar = 1;
}

omitLastInOneLineBlock

使用 "always", { "omitLastInOneLineBlock": true } 选项时,此规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the "always", { "omitLastInOneLineBlock": true } options:

在线运行
/*eslint semi: ["error", "always", { "omitLastInOneLineBlock": true}] */

if (foo) { bar() }

if (foo) { bar(); baz() }

function f() { bar(); baz() }

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

    static { bar(); baz() }
}

omitLastInOneLineClassBody

使用 "always", { "omitLastInOneLineClassBody": true } 选项时,此规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the "always", { "omitLastInOneLineClassBody": true } options:

在线运行
/*eslint semi: ["error", "always", { "omitLastInOneLineClassBody": true}] */

export class SomeClass{
    logType(){
        console.log(this.type);
        console.log(this.anotherType);
    }
}

export class Variant1 extends SomeClass{type=1}
export class Variant2 extends SomeClass{type=2; anotherType=3}

never

使用 "never" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint semi: ["error", "never"]*/

var name = "ESLint";

object.method = function() {
    // ...
};

class Foo {
    bar = 1;
}

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

🌐 Examples of correct code for this rule with the "never" option:

在线运行
/*eslint semi: ["error", "never"]*/

var name = "ESLint"

object.method = function() {
    // ...
}

var name = "ESLint"

;(function() {
    // ...
})()

import a from "a"
(function() {
    // ...
})()

import b from "b"
;(function() {
    // ...
})()

class Foo {
    bar = 1
}

beforeStatementContinuationChars

使用 "never", { "beforeStatementContinuationChars": "always" } 选项时,该规则的其他 错误 代码示例:

🌐 Examples of additional incorrect code for this rule with the "never", { "beforeStatementContinuationChars": "always" } options:

在线运行
/*eslint semi: ["error", "never", { "beforeStatementContinuationChars": "always"}] */
import a from "a"

(function() {
    // ...
})()

使用 "never", { "beforeStatementContinuationChars": "never" } 选项时,此规则的其他 错误 代码示例:

🌐 Examples of additional incorrect code for this rule with the "never", { "beforeStatementContinuationChars": "never" } options:

在线运行
/*eslint semi: ["error", "never", { "beforeStatementContinuationChars": "never"}] */
import a from "a"

;(function() {
    // ...
})()

何时不使用

🌐 When Not To Use It

如果你不想以任何特定方式强制使用分号(或省略),则可以关闭此规则。

🌐 If you do not want to enforce semicolon usage (or omission) in any particular way, then you can turn this rule off.

版本

此规则是在 ESLint v0.0.6 中引入。

进阶读物

资源