no-nonoctal-decimal-escape
禁止在字符串字面中使用 \8 和 \9 转义序列
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
此规则报告的一些问题可通过编辑器 建议 手动修复
尽管在 ECMAScript 2021 之前语言中没有指定,字符串字面量中的 \8 和 \9 转义序列在大多数 JavaScript 引擎中是允许的,并被视为“无用”的转义:
🌐 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";
const foo = "w\8less";
const bar = "December 1\9";
const baz = "Don't use \8 and \9 escapes.";
const quux = "\0\8";
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-nonoctal-decimal-escape: "error"*/
"8";
"9";
const foo = "w8less";
const bar = "December 19";
const baz = "Don't use \\8 and \\9 escapes.";
const quux = "\0\u0038";
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
相关规则
版本
此规则是在 ESLint v7.14.0 中引入。