Index

template-curly-spacing

要求或不允许模板字符串的嵌入表达式周围有空格

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

我们可以在模板字符串中使用一对 ${} 来嵌入表达式。

🌐 We can embed expressions in template strings with using a pair of ${ and }.

此规则可以根据风格指南强制在大括号对内使用空格。

🌐 This rule can force usage of spacing within the curly brace pair according to style guides.

let hello = `hello, ${people.name}!`;

规则详情

🌐 Rule Details

此规则旨在保持模板字面内部间距的一致性。

🌐 This rule aims to maintain consistency around the spacing inside of template literals.

选项

🌐 Options

{
    "template-curly-spacing": ["error", "never"]
}

此规则有一个选项,其值为 "never""always"

🌐 This rule has one option which has either "never" or "always" as value.

  • "never"(默认情况下)- 不允许大括号对内有空格。
  • "always" - 大括号对内需要一个或多个空格。

never

使用默认 "never" 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default "never" option:

在线运行
/*eslint template-curly-spacing: "error"*/

`hello, ${ people.name}!`;
`hello, ${people.name }!`;

`hello, ${ people.name }!`;

使用默认 "never" 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default "never" option:

在线运行
/*eslint template-curly-spacing: "error"*/

`hello, ${people.name}!`;

`hello, ${
    people.name
}!`;

always

使用 "always" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "always" option:

在线运行
/*eslint template-curly-spacing: ["error", "always"]*/

`hello, ${ people.name}!`;
`hello, ${people.name }!`;

`hello, ${people.name}!`;

使用 "always" 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "always" option:

在线运行
/*eslint template-curly-spacing: ["error", "always"]*/

`hello, ${ people.name }!`;

`hello, ${
    people.name
}!`;

何时不使用

🌐 When Not To Use It

如果你不想收到有关模板字符串内使用间距的通知,那么禁用此规则是安全的。

🌐 If you don’t want to be notified about usage of spacing inside of template strings, then it’s safe to disable this rule.

版本

此规则是在 ESLint v2.0.0-rc.0 中引入。

资源