非常教程

Eslint参考手册

规则 | Rules

strict

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

严格模式指令是"use strict"脚本或函数体开头的文字。它支持严格的模式语义。

在全局范围内发生指令时,严格模式适用于整个脚本:

"use strict";

// strict mode

function foo() {
    // strict mode
}

当一个指令发生在函数体的开始处时,严格模式仅适用于该函数,包括所有包含的函数:

function foo() {
    "use strict";
    // strict mode
}

function foo2() {
    // not strict mode
};

(function() {
    "use strict";
    function bar() {
        // strict mode
    }
}());

CommonJS模块系统中,隐藏函数包装每个模块并限制“global”严格模式指令的范围。

ECMAScript模块中,它们总是有严格的模式语义,所以这些指令是不必要的。

规则细节

此规则要求或不允许严格的模式指令。

如果ESLint配置指定以下任一解析器选项,则此规则不允许使用严格模式指令,无论指定哪个选项:

  • "sourceType": "module"即文件是ECMAScript模块
  • "impliedStrict": trueecmaFeatures对象中的属性

此规则在具有非简单参数列表的函数(例如,具有默认参数值的参数列表)中禁止使用严格模式指令,无论指定了哪个选项,因为这是ECMAScript 2016及更高版本中的语法错误。请参阅功能选项的示例。

--fix命令行选项不插入新的"use strict"声明,但只删除不需要的语句。

选项

这条规则有一个字符串选项:

  • "safe" (默认)对应以下任一选项:
    • "global"如果ESLint认为文件是CommonJS模块
    • "function" 除此以外
  • "global" 在全局范围内需要一个严格模式指令(并且不允许任何其他严格模式指令)
  • "function" 在每个顶级函数声明或表达式中需要一个严格模式指令(并且不允许任何其他严格模式指令)
  • "never" 禁止严格的模式指令

safe

如果ESLint认为文件是Node.jsCommonJS模块,则该"safe"选项对应于该"global"选项,因为该配置指定了以下任一项:

  • nodecommonjs环境
  • "globalReturn": trueecmaFeatures解析器选项对象中的属性

否则,该"safe"选项对应于该"function"选项。请注意,如果"globalReturn": false在配置中明确指定,则无论指定的环境如何,该"safe"选项都将对应于该"function"选项。

global

此规则的错误代码示例包含以下"global"选项:

/*eslint strict: ["error", "global"]*/

function foo() {
}
/*eslint strict: ["error", "global"]*/

function foo() {
    "use strict";
}
/*eslint strict: ["error", "global"]*/

"use strict";

function foo() {
    "use strict";
}

此规则的正确代码示例包含以下"global"选项:

/*eslint strict: ["error", "global"]*/

"use strict";

function foo() {
}

函数

该选项确保所有函数体都是严格的模式代码,而全局代码不是。特别是如果构建步骤连接多个脚本,则一个脚本的全局代码中的严格模式指令可能会无意中在另一个脚本中启用严格模式,而该脚本并不是严格的代码。

此规则的错误代码示例包含以下"function"选项:

/*eslint strict: ["error", "function"]*/

"use strict";

function foo() {
}
/*eslint strict: ["error", "function"]*/

function foo() {
}

(function() {
    function bar() {
        "use strict";
    }
}());
/*eslint strict: ["error", "function"]*/
/*eslint-env es6*/

// Illegal "use strict" directive in function with non-simple parameter list.
// This is a syntax error since ES2016.
function foo(a = 1) {
    "use strict";
}

// We cannot write "use strict" directive in this function.
// So we have to wrap this function with a function with "use strict" directive.
function foo(a = 1) {
}

此规则的正确代码示例包含以下"function"选项:

/*eslint strict: ["error", "function"]*/

function foo() {
    "use strict";
}

(function() {
    "use strict";

    function bar() {
    }

    function baz(a = 1) {
    }
}());

var foo = (function() {
    "use strict";

    return function foo(a = 1) {
    };
}());

never

此规则的错误代码示例包含以下"never"选项:

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

"use strict";

function foo() {
}
/*eslint strict: ["error", "never"]*/

function foo() {
    "use strict";
}

此规则的正确代码示例包含以下"never"选项:

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

function foo() {
}

之前的默认(删除)

ESLint v1.0 中删除了此规则的默认选项(即未指定字符串选项)。"function"选项与删除的选项最为相似。

该选项确保所有功能都以严格模式执行。严格的模式指令必须存在于全局代码或每个顶层函数声明或表达式中。它不关注在已经严格的嵌套函数中不必要的严格模式指令,也不在同一级别使用多个严格模式指令。

此规则的不正确代码示例与早先的默认选项已被删除:

// "strict": "error"

function foo() {
}
// "strict": "error"

(function() {
    function bar() {
        "use strict";
    }
}());

此规则的正确代码示例,其中包含已删除的较早的默认选项:

// "strict": "error"

"use strict";

function foo() {
}
// "strict": "error"

function foo() {
    "use strict";
}
// "strict": "error"

(function() {
    "use strict";
    function bar() {
        "use strict";
    }
}());

何时不使用

在具有严格和非严格代码的代码库中,可以关闭此规则,或者在必要时选择性地禁用它。例如,引用函数arguments.callee在严格模式下无效。一个严格的模式差异完整列表,请在MDN。

版本

该规则在ESLint 0.1.0中引入。

资源

  • Rule source
  • Documentation source

规则 | Rules相关

1.accessor-pairs
2.array-bracket-newline
3.array-bracket-spacing
4.array-callback-return
5.array-element-newline
6.arrow-body-style
7.arrow-parens
8.arrow-spacing
9.block-scoped-var
10.block-spacing
11.brace-style
12.callback-return
13.camelcase
14.capitalized-comments
15.class-methods-use-this
16.comma-dangle
17.comma-spacing
18.comma-style
19.complexity
20.computed-property-spacing
21.consistent-return
22.consistent-this
23.constructor-super
24.curly
25.default-case
26.dot-location
27.dot-notation
28.eol-last
29.eqeqeq
30.for-direction
31.func-call-spacing
32.func-name-matching
33.func-names
34.func-style
35.function-paren-newline
36.generator-star
37.generator-star-spacing
38.getter-return
39.global-require
40.global-strict
41.guard-for-in
42.handle-callback-err
43.id-blacklist
44.id-length
45.id-match
46.implicit-arrow-linebreak
47.indent
48.indent-legacy
49.init-declarations
50.jsx-quotes
51.key-spacing
52.keyword-spacing
53.line-comment-position
54.linebreak-style
55.lines-around-comment
56.lines-around-directive
57.lines-between-class-members
58.max-depth
59.max-len
60.max-lines
61.max-nested-callbacks
62.max-params
63.max-statements
64.max-statements-per-line
65.multiline-comment-style
66.multiline-ternary
67.new-cap
68.new-parens
69.newline-after-var
70.newline-before-return
71.newline-per-chained-call
72.no-alert
73.no-array-constructor
74.no-arrow-condition
75.no-await-in-loop
76.no-bitwise
77.no-buffer-constructor
78.no-caller
79.no-case-declarations
80.no-catch-shadow
81.no-class-assign
82.no-comma-dangle
83.no-compare-neg-zero
84.no-cond-assign
85.no-confusing-arrow
86.no-console
87.no-const-assign
88.no-constant-condition
89.no-continue
90.no-control-regex
91.no-debugger
92.no-delete-var
93.no-div-regex
94.no-dupe-args
95.no-dupe-class-members
96.no-dupe-keys
97.no-duplicate-case
98.no-duplicate-imports
99.no-else-return
100.no-empty
Eslint

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。

主页 https://eslint.org/
源码 https://github.com/eslint/eslint
发布版本 4.12.0

Eslint目录

1.指南 | Guide
2.规则 | Rules