semi

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

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

JavaScript 不需要在每条语句的末尾使用分号。在许多情况下,JavaScript 引擎可以确定分号应该在某个位置并自动添加它。此功能称为自动分号插入 (ASI),被认为是 JavaScript 中更具争议的功能之一。例如,以下行都是有效的:

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

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

在关于 ASI 的争论中,通常有两种思想流派。首先是我们应该将 ASI 视为不存在,并始终手动包含分号。基本原理是始终包含分号比尝试记住何时需要或不需要分号更容易,从而降低了引入错误的可能性。

jtJsJYiqS3F4zutTZq3/r0Z8iOC/U1XR5CsTp/aGNSSyPNhuz5HDvjsFD5xhwkmrrwB1H8A8gPS/H7svUJK2LkmHloNwGT2MXnbdLN1h5NizpdYcBmpC29LaGWki65b3q15QFHQ21hg1UscW0k9JyQ==

return
{
    name: "ESLint"
};

3JofW7CQoJmrCPsRcsdwd2kaioEoFHBXDjWXDhvCUK9zjnqKs2oVzZXDuOMFvVL4b8ocAwIfYhlZKVU6x0isnW7VDOImkfNZpBKl3cHKZL4QM5/ASGHR8z/43Dvt7HZ7JNLg/uZsyQhuItuJkfuUVtlm4G+x1oAaOzelqxFRrAk7N+bRG4/3cCy7Fczrz67X

return;
{
    name: "ESLint";
}

d6EiZat50njm6bfYR5EGyhUOGoXCAmdVNml5OZpqdcfayI0p+p/uPsaOsAo2fWtGkSD6zzPxkkWuwcB3FHBDb4OHo0nosdqfcjpGis4OY+s6Q8W9OAC3LFErsP+/mo0FrFVq0vnEkcClkp2NMOQGCJGBHR+xdID61mFQo4PqU81ji0lzyRP++1qFgkUNe8UzddW39KTezBjjpjDAxRz+aBa4Zr5lvhzi4jrIdT01TppAhhSzyCvz1fzY98GGEv4dV2oBkNB1N0aQm+Es6DaEnOCwkitV0qT19RUg7FxgeSeuxvj0lPRBzacifV2YTqzMu4h1tILQdMcNC02PTF5ZA6u++QAVoaXYsROr5OyssoIwYQYb3ASyHm0sosKV2MPJ

wLQ3yeub+N7M4FCYX+Bx/y2d0HsZ3crzey7HGMnN/Vv+dPjY7DbZ0lawYy452RFbmfTkZIKWwglxvirQwAXxttdArxo6msvzx/0P3hJqJ9XHCuWMMtFOgDDWdaTXlERSi4Ny3Ehs6oiyJVkmMBNo4RWCdslpIJr71SHseS3Jbjpr+sP7oU2kfTIeMLosj6UU8ZZbIobQchO3fpXx4HtguQ99lrYhE9lC6KVWCTINw3P3bLAyxlum2Ol66d+S6vxmc0z/jciIk9WN6TH1ehlU9JaezkFm607B3apZt1T+sZE=

var globalCounter = { }

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

uPn/1oiDeLqyj51NyRV6BPGduyiV+xQ3t7brWJ+aUhKNYXEZnEXoV1cC6fvnxig+clM6y2Yv9bpcGkhC7aiJxOvpzGJqoe5pqnm320LWbpHhVDJQKjWBl+sfrEqs1t3BTEwV4AZSUdoaWu/TYqeN67+saPwNCMLlgtKevXsdLChpKF6tHCkDcZ0+2zv9PuIeV/IFTrSqAGrWun16vZK+/mKpKYfcmQcdharqHUAXldglU44CVBSt2D3ChqzmW0A9NdNcVPFvv7H7cD7pcB1vSdBn49FEnYRk/VWkK+6bMKVC1yeBX4HH/hy5P7QurvenwEMYX16zMUh2onOfygI2wSkgQTswQM9pL+yfIJu3DUU=

AQmzcN5NIx2FRKHCfBxX5zlk5S5kQ7Yomg2tNtAWf618MKvJSrp2Y1C8iTM5M40XBZAJQEml7Z+VQwtYjiOhQticCgcg3RgY8La6mO+xlH/ES+1hop8eOXhX9dsZfVmlV0W8EgxvXYGIYnzsw3t44trF1F0zgD5935Hc4rI2eD2LDnDqqz/j8EAq84T8fbUAK0Xke5f3IPkQGGBxbyPXL9HS+KeRG8VXnn3RrLRpGAWhPYpNQuLXXOO50k3gXb9ceu4/NbMEMK2XGBTIr0EfjMPkhgadnTtzVtGxBuiL89YYiFo0iLyXfFoBwe6nrFs/DBlWBDLbmazQEl6NUZBp9V4ef9u7d93fwVjlvebRWkKgrvbw7MnjYwXirGp13Bi2d/D6o4aP/88e0OcOWuAFuIO2UUDWH+B98nmTHWSop8n8TYQYTq1hS8CpT13q4GiD583roNvUPJZ3+VOCcmCbk1Ue70fXIlgm+dUM+EcX0/onxA63ifDxvOSFCsX16vvu5cw2fhplWvLDyssEHbQA22/gQsDJ21hRcqadGViWzihpN/5oT6ZAPIW/aIoWX0hXk9gQ8HHF48dPCoPGLqEjn15gVt39H6O94aeTUxIpwxo=

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

规则详情

Qfd+Kqvb7xhPVA5CAMZeGjHULThgewCtM8eb8myMnZ+Km8MdXjb8YZcwVrRDJI/u

选项

t7TqPxPv1EUwsYBaPhBllFmla5LEBrKYyKdvblQMuzGr+sDg1OkpbUsCvOg953QSErzBjo0VA+Q2rhehOAEE4dOSTj/yu1uCQmjU/P5L198=

23S1pJ3ZG0WKE8rk/B4jYWVJGFgKOTEvKZNFjNm5nIs=

    Ea+s3QJASFaHl1uNHE6uI/P4eFq/W6rNRx66EQcgCIOuyw8+JaIzP9KC/cupiQTDu/H5losF1ird3z8xeZOR5adAw8pSByVX0nhp0hEpZ+mENSD+zAm7CBZpybErR36xfU9QNFNl8B7j6aPgpm1t3bz1cY4CU3d4+fNsQzdSzdlC72g4n1H9cSNe5b+uQFFfodLm/B/s7TDug/AR/tN+emvGZ0nPo+9SeELfbklRWgdsWxPGQfIrLevfU1vio2JRauBYn3FBdzXS1qtZnOettUsMR57N2ekU0rdHJWfNxDoehJMqHbUNGK3OsdPWH0vfxLtCYdBd7mtcmgPu3hzO7W34vkaJF4pXRyi3+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SBxO2F6/hPEa1O9ybU3LrD72YTaIMYkffKfcVWkj7XwMuiLd9kMJ3VFAEkFglaw4F9bY5P4xAwdJHqsU42oko1uxi0kVNPeqOmX2gW27dARLzfCmiOqMd2D8RuOAnIc5r4Ck06lswepDzQJCGLkW6FD8afAr4gS3lv53TBKDly/dbmcWVQhJDD3z8bKuXaLK8oNbnk1PKbDDChN10VczZ/DvEU9Ov2koTCrXByR6969ejdPagSXf73mk/BQsj4+GgvvWhN/zaZXTh0jR0xXAy3QeiZB43HpzuHQY77b2XeZwomftTnppQcCjFs8uTZ8e2o+XpIrIXN60NcB+Fa7qQ==

pMMv75sauTq/41Yb56jz+AjeqMq3OS1QrWeE/bFSLMO2z0u90XeTSMAnrciMWczlWiRoJqibGC7YcQujZJ7gWctBkLJlYKJpQ+XdFtPhiSDQTwcLzSA+DVHFtYqwAPPHpZgb4Z7DDNrAW8LdNcCwVAuq5LQbyn+saDJF5MvrFIs=

always

xj2n5z23kdHxAkOtETOwXq01kJ2HxWRzYVXS15ZBH3rG/VeJ31Vl9Xfk9QNluZ5XeFJAo7+4y4GkHqXI2DAnEAegkO1t7UUU0SIJRNmie2Q=

/*eslint semi: ["error", "always"]*/

var name = "ESLint"

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

class Foo {
    bar = 1
}

xj2n5z23kdHxAkOtETOwXq01kJ2HxWRzYVXS15ZBH3rG/VeJ31Vl9Xfk9QNluZ5XLdq5Ds6JyfrG1vg/gy8RCZoi90FOWHH3tWcM0lg0Zhg=

/*eslint semi: "error"*/

var name = "ESLint";

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

class Foo {
    bar = 1;
}

never

60VRYXbEBTe/2ZCncdIS4dECanVanhFbUVjEs87lct0Lv4jJbKEWk90+POVHV5I32rsBJ8Gedw/quySBFXL08ZRfTDbJAdGfFfoy4+YVkdw=

/*eslint semi: ["error", "never"]*/

var name = "ESLint";

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

class Foo {
    bar = 1;
}

60VRYXbEBTe/2ZCncdIS4dECanVanhFbUVjEs87lct3wOS+60qsg7bEvcUOj2y27/yo4YHf8xvD6u7nuV1Xzt63grixWZ0hkqhJZfolcXa8=

/*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
}

omitLastInOneLineBlock

60VRYXbEBTe/2ZCncdIS4RNzwIrBehpepJTXcVKSKjpOjzPjgvsw2eWG/bVJc1g88gTkAn+ERVy388yeH4gKra//NMZp1CUqfqJXqGx/DSD+JhAXBCfCq4hKPUW7Bi17SfEoIJa7q8E1BEfJeJl3FQ==

/*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() }
}

beforeStatementContinuationChars

fdwOTwSq+vT+BEFfBAK9hyKuypTuL3nQSyURcWcg2YxL66jAAK0KR/OF0pZhh+OCNejHedcdJCtOFZGG/KHXMP6RHwiS3dQ6O7HgaIi8lcrQUyz+7MXTUKnzHq9PgbZY5UVVdmKkgysfozl7NCdo3s+l5C/NY/VD0KFIsWaI5vE=

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

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

fdwOTwSq+vT+BEFfBAK9hyKuypTuL3nQSyURcWcg2YxL66jAAK0KR/OF0pZhh+OCsRayW+vKqwNGdNKw8sFsgze1y5aQRcRobzHPNE43I7udBaE280zQoVU2BWAR9IpJ7cksDn3+OlKqeiGaHh3MgkcNzH548fkAPmK8dEjkdMQ=

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

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

何时不使用

MKQSrlX1Eps+rrfKbhL3UE0t7x4Q2BG1Sm29VXYkqDyZtbAh/omEeJkvpHQ32lyQiLCzNhsL0StVnNeCIJeJUdx2lrFqzsSfKHrZ2aLJG/AOQ05BJAEQ7nPtoYLOxLUDx0MGUdZWvKh9l33t9ajtsA==