eslint/no-template-curly-in-string Style
作用
禁止在普通字符串中使用模板字面量占位符语法。此规则确保像 ${variable} 这样的表达式只在模板字面量中使用,避免在普通字符串中出现错误 用法。
为什么这很糟糕?
ECMAScript 6 允许程序员使用模板字面量来创建包含变量或表达式的字符串。 这是通过在反引号之间嵌入 ${variable} 之类的表达式来实现的。 如果将普通引号(' 或 ")与模板字面量语法混用,结果会得到字面字符串 "${variable}",而不是对表达式求值。此规则有助于避免这种错误, 确保表达式在模板字面量中被正确求值。
示例
以下是此规则的错误代码示例:
javascript
"Hello ${name}!";
"Hello ${name}!";
"Time: ${12 * 60 * 60 * 1000}";以下是此规则的正确代码示例:
javascript
`Hello ${name}!`;
`Time: ${12 * 60 * 60 * 1000}`;
templateFunction`Hello ${name}`;如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-template-curly-in-string": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-template-curly-in-string": "error",
},
});bash
oxlint --deny no-template-curly-in-string版本
此规则是在 v0.2.14 中添加的。
