no-useless-escape

禁止不必要的转义字符

Recommended

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

💡 hasSuggestions

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

转义字符串、模板字面和正则表达式中的非特殊字符没有任何效果,如下例所示:

¥Escaping non-special characters in strings, template literals, and regular expressions doesn’t have any effect, as demonstrated in the following example:

let foo = "hol\a"; // > foo = "hola"
let bar = `${foo}\!`; // > bar = "hola!"
let baz = /\:/ // same functionality with /:/

规则详情

¥Rule Details

此规则标记可以在不改变行为的情况下安全删除的转义。

¥This rule flags escapes that can be safely removed without changing behavior.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

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

"\'";
'\"';
"\#";
"\e";
`\"`;
`\"${foo}\"`;
`\#{foo}`;
/\!/;
/\@/;
/[\[]/;
/[a-z\-]/;

此规则的正确代码示例:

¥Examples of correct code for this rule:

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

"\"";
'\'';
"\x12";
"\u00a9";
"\371";
"xs\u2111";
`\``;
`\${${foo}}`;
`$\{${foo}}`;
/\\/g;
/\t/g;
/\w\$\*\^\./;
/[[]/;
/[\]]/;
/[a-z-]/;

何时不使用

¥When Not To Use It

如果你不想收到有关不必要转义的通知,你可以安全地禁用此规则。

¥If you don’t want to be notified about unnecessary escapes, you can safely disable this rule.

版本

此规则是在 ESLint v2.5.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号