Skip to content
← Back to rules

unicorn/prefer-string-raw 风格

An auto-fix is available for this rule.

它的作用

优先使用 String.raw 以避免转义反斜杠 \

为什么这是不好的?

过多的反斜杠会使字符串值的可读性降低,通过使用 String.raw 可以避免此问题。

示例

此规则的错误代码示例:

javascript
const file = "C:\\windows\\style\\path\\to\\file.js";
const regexp = new RegExp("foo\\.bar");

此规则的正确代码示例:

javascript
const file = String.raw`C:\windows\style\path\to\file.js`;
const regexp = new RegExp(String.raw`foo\.bar`);

如何使用

要通过配置文件或在 CLI 中启用此规则,可以使用:

json
{
  "rules": {
    "unicorn/prefer-string-raw": "error"
  }
}
bash
oxlint --deny unicorn/prefer-string-raw

参考资料