Skip to content

noTemplateCurlyInString

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noTemplateCurlyInString": "error"
}
}
}
}

¥Description

禁止在普通字符串中使用模板字面量占位符语法。

¥Disallow template literal placeholder syntax in regular strings.

ECMAScript 6 允许程序员使用模板字面量(而非字符串连接)创建包含变量或表达式的字符串,方法是将表达式(例如 ${variable})写在两个反引号 (`) 之间。在使用模板字面量时,很容易因为使用了错误的引号而导致代码出现错误。例如,如果写成 "${variable}",最终得到的是字面值 "${variable}",而不是包含注入表达式值的字符串。

¥ECMAScript 6 allows programmers to create strings containing variable or expressions using template literals, instead of string concatenation, by writing expressions like ${variable} between two backtick quotes (`). It can be easy to use the wrong quotes when wanting to use template literals, by writing "${variable}", and end up with the literal value "${variable}" instead of a string containing the value of the injected expressions.

¥Examples

¥Invalid

const a = "Hello ${name}!";
code-block.js:1:18 lint/suspicious/noTemplateCurlyInString ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected template string placeholder.

> 1 │ const a = “Hello ${name}!”;
^^^^^^^
2 │

Turn the string into a template string.

const a = 'Hello ${name}!';
code-block.js:1:18 lint/suspicious/noTemplateCurlyInString ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected template string placeholder.

> 1 │ const a = ‘Hello ${name}!’;
^^^^^^^
2 │

Turn the string into a template string.

const a = "Time: ${12 * 60 * 60 * 1000}";
code-block.js:1:18 lint/suspicious/noTemplateCurlyInString ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected template string placeholder.

> 1 │ const a = “Time: ${12 * 60 * 60 * 1000}”;
^^^^^^^^^^^^^^^^^^^^^^
2 │

Turn the string into a template string.

¥Valid

const a = `Hello ${name}!`;
const a = `Time: ${12 * 60 * 60 * 1000}`;
const a = templateFunction`Hello ${name}`;

¥Related links