no-template-curly-in-string

禁止在常规字符串中使用模板字面占位符语法

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.

规则详情

¥Rule Details

此规则旨在在常规字符串包含看起来像模板字面占位符的内容时触发警告。当它发现包含模板字面占位符 (${something}) 的字符串使用 "' 作为引号时,它会触发警告。

¥This rule aims to warn when a regular string contains what looks like a template literal placeholder. It will warn when it finds a string containing the template literal placeholder (${something}) that uses either " or ' for the quotes.

示例

¥Examples

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-template-curly-in-string: "error"*/
"Hello ${name}!";
'Hello ${name}!';
"Time: ${12 * 60 * 60 * 1000}";

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-template-curly-in-string: "error"*/
`Hello ${name}!`;
`Time: ${12 * 60 * 60 * 1000}`;

templateFunction`Hello ${name}`;

何时不使用

¥When Not To Use It

此规则不应在 ES3/5 环境中使用。

¥This rule should not be used in ES3/5 environments.

版本

此规则是在 ESLint v3.3.0 中引入。

资源