no-nonoctal-decimal-escape

禁止在字符串字面中使用 \8\9 转义序列

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

尽管直到 ECMAScript 2021 才在语言中指定,但大多数 JavaScript 引擎都允许字符串字面中的 \8\9 转义序列,并将其视为 “useless” 转义:

¥Although not being specified in the language until ECMAScript 2021, \8 and \9 escape sequences in string literals were allowed in most JavaScript engines, and treated as “useless” escapes:

"\8" === "8"; // true
"\9" === "9"; // true

自 ECMAScript 2021 起,这些转义序列被指定为 非八进制十进制转义序列,保持相同的行为。

¥Since ECMAScript 2021, these escape sequences are specified as non-octal decimal escape sequences, retaining the same behavior.

然而,ECMAScript 规范将字符串字面中的 \8\9 视为旧版特性。如果 ECMAScript 主机不是 Web 浏览器,则此语法是可选的。浏览器仍然必须支持它,但仅限于非严格模式。

¥Nevertheless, the ECMAScript specification treats \8 and \9 in string literals as a legacy feature. This syntax is optional if the ECMAScript host is not a web browser. Browsers still have to support it, but only in non-strict mode.

无论你的目标环境如何,在编写新代码时都不应使用这些转义序列。

¥Regardless of your targeted environment, these escape sequences shouldn’t be used when writing new code.

规则详情

¥Rule Details

此规则不允许字符串字面中的 \8\9 转义序列。

¥This rule disallows \8 and \9 escape sequences in string literals.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-nonoctal-decimal-escape: "error"*/

"\8";

"\9";

var foo = "w\8less";

var bar = "December 1\9";

var baz = "Don't use \8 and \9 escapes.";

var quux = "\0\8";

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-nonoctal-decimal-escape: "error"*/

"8";

"9";

var foo = "w8less";

var bar = "December 19";

var baz = "Don't use \\8 and \\9 escapes.";

var quux = "\0\u0038";

版本

此规则是在 ESLint v7.14.0 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号