版本

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 模块系统中,一个隐藏函数包装每个模块并限制了“全局”严格模式指令的作用域。

ECMAScript 模块中,它们始终具有严格模式语义,因此这些指令是不必要的。

规则详情

此规则要求或禁止使用严格模式指令。

如果 ESLint 配置指定以下任一项作为 解析器选项,则无论指定哪个选项,此规则都禁止使用严格模式指令

  • "sourceType": "module",即文件是 ECMAScript 模块。
  • ecmaFeatures 对象中的 "impliedStrict": true 属性。

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

无论指定哪个选项,此规则都不适用于类静态块,因为类静态块没有指令。因此,类静态块中的 "use strict" 语句不是指令,并且将由 no-unused-expressions 规则报告。

命令行上的 --fix 选项不会插入新的 "use strict" 语句,而只会删除不必要的语句。

选项

此规则有一个字符串选项

  • "safe" (默认) 对应于以下任一选项
    • 如果 ESLint 认为文件是 CommonJS 模块,则对应 "global"
    • 否则对应 "function"
  • "global" 要求在全局作用域中有一个严格模式指令(并禁止任何其他严格模式指令)
  • "function" 要求在每个顶层函数声明或表达式中有一个严格模式指令(并禁止任何其他严格模式指令)
  • "never" 禁止使用严格模式指令

safe

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

否则,"safe" 选项对应于 "function" 选项。

global

使用 "global" 选项时,错误 代码的示例

在 Playground 中打开
/*eslint strict: ["error", "global"]*/

function foo() {
}
在 Playground 中打开
/*eslint strict: ["error", "global"]*/

function foo() {
    "use strict";
}
在 Playground 中打开
/*eslint strict: ["error", "global"]*/

"use strict";

function foo() {
    "use strict";
}

使用 "global" 选项时,正确 代码的示例

在 Playground 中打开
/*eslint strict: ["error", "global"]*/

"use strict";

function foo() {
}

function

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

使用 "function" 选项时,错误 代码的示例

在 Playground 中打开
/*eslint strict: ["error", "function"]*/

"use strict";

function foo() {
}
在 Playground 中打开
/*eslint strict: ["error", "function"]*/

function foo() {
}

(function() {
    function bar() {
        "use strict";
    }
}());
在 Playground 中打开
/*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" 选项时,正确 代码的示例

在 Playground 中打开
/*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" 选项时,错误 代码的示例

在 Playground 中打开
/*eslint strict: ["error", "never"]*/

"use strict";

function foo() {
}
在 Playground 中打开
/*eslint strict: ["error", "never"]*/

function foo() {
    "use strict";
}

使用 "never" 选项时,正确 代码的示例

在 Playground 中打开
/*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 v0.1.0 中引入。

资源

更改语言