Skip to content
← Back to rules

unicorn/prefer-string-raw Style

🛠️ 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`);

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-string-raw": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-string-raw": "error",
  },
});
bash
oxlint --deny unicorn/prefer-string-raw

版本

此规则是在 v0.12.0 中添加的。

参考资料