严格
严格模式指令是脚本或函数体开头的 "use strict"
字面。它启用严格模式语义。
¥A strict mode directive is a "use strict"
literal at the beginning of a script or function body. It enables strict mode semantics.
当指令出现在全局作用域内时,严格模式适用于整个脚本:
¥When a directive occurs in global scope, strict mode applies to the entire script:
"use strict";
// strict mode
function foo() {
// strict mode
}
当指令出现在函数体的开头时,严格模式仅适用于该函数,包括所有包含的函数:
¥When a directive occurs at the beginning of a function body, strict mode applies only to that function, including all contained functions:
function foo() {
"use strict";
// strict mode
}
function foo2() {
// not strict mode
};
(function() {
"use strict";
function bar() {
// strict mode
}
}());
在 CommonJS 模块系统中,隐藏函数封装每个模块并限制 “global” 严格模式指令的范围。
¥In the CommonJS module system, a hidden function wraps each module and limits the scope of a “global” strict mode directive.
在始终具有严格模式语义的 ECMAScript 模块中,指令是不必要的。
¥In ECMAScript modules, which always have strict mode semantics, the directives are unnecessary.
规则详情
¥Rule Details
此规则要求或禁止严格模式指令。
¥This rule requires or disallows strict mode directives.
如果 ESLint 配置将以下任一选项指定为 解析器选项,则无论指定哪个选项,此规则都不允许严格模式指令:
¥This rule disallows strict mode directives, no matter which option is specified, if ESLint configuration specifies either of the following as parser options:
-
"sourceType": "module"
即文件是 ECMAScript 模块¥
"sourceType": "module"
that is, files are ECMAScript modules -
ecmaFeatures
对象中的"impliedStrict": true
属性¥
"impliedStrict": true
property in theecmaFeatures
object
无论指定哪个选项,此规则都不允许在具有非简单参数列表(例如具有默认参数值的参数列表)的函数中使用严格模式指令,因为这是 ECMAScript 2016 及更高版本中的语法错误。请参阅 函数 选项的示例。
¥This rule disallows strict mode directives, no matter which option is specified, in functions with non-simple parameter lists (for example, parameter lists with default parameter values) because that is a syntax error in ECMAScript 2016 and later. See the examples of the function option.
无论指定哪个选项,此规则都不适用于类静态块,因为类静态块没有指令。因此,类静态块中的 "use strict"
语句不是指令,会被 no-unused-expressions 规则报告。
¥This rule does not apply to class static blocks, no matter which option is specified, because class static blocks do not have directives. Therefore, a "use strict"
statement in a class static block is not a directive, and will be reported by the no-unused-expressions rule.
命令行中的 --fix
选项不会插入新的 "use strict"
语句,而只会删除不需要的语句。
¥The --fix
option on the command line does not insert new "use strict"
statements, but only removes unneeded statements.
选项
¥Options
此规则有一个字符串选项:
¥This rule has a string option:
-
"safe"
(默认)对应以下选项之一:¥
"safe"
(default) corresponds either of the following options:-
"global"
如果 ESLint 认为文件是 CommonJS 模块¥
"global"
if ESLint considers a file to be a CommonJS module -
"function"
否则¥
"function"
otherwise
-
-
"global"
需要全局作用域内的一个严格模式指令(并且不允许任何其他严格模式指令)¥
"global"
requires one strict mode directive in the global scope (and disallows any other strict mode directives) -
"function"
要求每个顶层函数声明或表达式中有一个严格模式指令(并且不允许任何其他严格模式指令)¥
"function"
requires one strict mode directive in each top-level function declaration or expression (and disallows any other strict mode directives) -
"never"
不允许严格模式指令¥
"never"
disallows strict mode directives
safe
如果 ESLint 认为文件是 Node.js 或 CommonJS 模块,则 "safe"
选项对应于 "global"
选项,因为配置指定了以下任一内容:
¥The "safe"
option corresponds to the "global"
option if ESLint considers a file to be a Node.js or CommonJS module because the configuration specifies either of the following:
-
解析器选项 的
ecmaFeatures
对象中的"globalReturn": true
属性¥
"globalReturn": true
property in theecmaFeatures
object of parser options
否则,"safe"
选项对应于 "function"
选项。请注意,如果在配置中明确指定了 "globalReturn": false
,则无论指定环境如何,"safe"
选项都将对应 "function"
选项。
¥Otherwise the "safe"
option corresponds to the "function"
option. Note that if "globalReturn": false
is explicitly specified in the configuration, the "safe"
option will correspond to the "function"
option regardless of the specified environment.
global
使用 "global"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "global"
option:
/*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"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "global"
option:
/*eslint strict: ["error", "global"]*/
"use strict";
function foo() {
}
函数
¥function
此选项确保所有函数体都是严格模式代码,而全局代码不是。特别是如果构建步骤连接多个脚本,则一个脚本的全局代码中的严格模式指令可能会无意中在另一个脚本中启用严格模式,而该脚本并非旨在成为严格代码。
¥This option ensures that all function bodies are strict mode code, while global code is not. Particularly if a build step concatenates multiple scripts, a strict mode directive in global code of one script could unintentionally enable strict mode in another script that was not intended to be strict code.
使用 "function"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "function"
option:
/*eslint strict: ["error", "function"]*/
"use strict";
function foo() {
}
/*eslint strict: ["error", "function"]*/
function foo() {
}
(function() {
function bar() {
"use strict";
}
}());
/*eslint strict: ["error", "function"]*/
// 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"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "function"
option:
/*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"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "never"
option:
/*eslint strict: ["error", "never"]*/
"use strict";
function foo() {
}
/*eslint strict: ["error", "never"]*/
function foo() {
"use strict";
}
使用 "never"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "never"
option:
/*eslint strict: ["error", "never"]*/
function foo() {
}
earlier default(已移除)
¥earlier default (removed)
(已删除)该规则的默认选项(即未指定字符串选项)已在 ESLint v1.0 中删除。"function"
选项与删除的选项最相似。
¥(removed) The default option (that is, no string option specified) for this rule was removed in ESLint v1.0. The "function"
option is most similar to the removed option.
此选项确保所有函数都在严格模式下执行。严格模式指令必须出现在全局代码或每个顶层函数声明或表达式中。它不关心已经严格的嵌套函数中不必要的严格模式指令,也不关心同一级别的多个严格模式指令。
¥This option ensures that all functions are executed in strict mode. A strict mode directive must be present in global code or in every top-level function declaration or expression. It does not concern itself with unnecessary strict mode directives in nested functions that are already strict, nor with multiple strict mode directives at the same level.
此规则的错误代码示例(先前的默认选项已被删除):
¥Examples of incorrect code for this rule with the earlier default option which has been removed:
// "strict": "error"
function foo() {
}
// "strict": "error"
(function() {
function bar() {
"use strict";
}
}());
此规则的正确代码示例(已删除先前的默认选项):
¥Examples of correct code for this rule with the earlier default option which has been removed:
// "strict": "error"
"use strict";
function foo() {
}
// "strict": "error"
function foo() {
"use strict";
}
// "strict": "error"
(function() {
"use strict";
function bar() {
"use strict";
}
}());
何时不使用
¥When Not To Use It
在具有严格和非严格代码的代码库中,要么关闭此规则,要么在必要时关闭 选择性地禁用它。例如,引用 arguments.callee
的函数在严格模式下无效。严格模式差异的完整列表 在 MDN 上可用。
¥In a codebase that has both strict and non-strict code, either turn this rule off, or selectively disable it where necessary. For example, functions referencing arguments.callee
are invalid in strict mode. A full list of strict mode differences is available on MDN.