# 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 模块
  • ecmaFeatures 对象中的 "impliedStrict": true 属性

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

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

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

# 选项

此规则有一个字符串选项:

  • "safe"(默认)对应以下任一选项:"global" 如果 ESLint 认为一个文件是一个 CommonJS 模块 "function" 否则

  • "global" 在全局范围内需要一个严格模式指令(并且不允许任何其他严格模式指令)

  • "function" 在每个顶级函数声明或表达式中都需要一个严格模式指令(并且不允许任何其他严格模式指令)

  • "never" 不允许严格模式指令

# safe

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

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

否则,"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

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

此规则使用 "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() {
}

# earlier default(已移除)

(已移除)此规则的默认选项(即未指定字符串选项)在 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 上可用。

Last Updated: 5/13/2023, 8:55:38 PM