Index

no-template-curly-in-string

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

ECMAScript 6 允许程序员使用模板字面量创建包含变量或表达式的字符串,而不是使用字符串连接,通过在两个反引号之间编写表达式,如 ${variable} 中的表达式 (). 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}`”),而不是包含注入表达式值的字符串。

🌐 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 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}`;

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 When Not To Use It

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

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

版本

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

资源