Index

template-tag-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

使用 ES6,可以创建称为 标记模板字面量 的函数,这些函数的参数由模板字面量的字符串和表达式组成。

在使用带标签的模板字面量时,可以在标签函数和模板字面量之间插入空白。由于这个空白是可选的,以下几行是等效的:

🌐 When using tagged template literals, it’s possible to insert whitespace between the tag function and the template literal. Since this whitespace is optional, the following lines are equivalent:

let hello = func`Hello world`;
let hello = func `Hello world`;

规则详情

🌐 Rule Details

此规则旨在保持模板标记函数与其模板字面之间的间距的一致性。

🌐 This rule aims to maintain consistency around the spacing between template tag functions and their template literals.

选项

🌐 Options

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

此规则有一个选项,其值可以设置为 "never""always"

🌐 This rule has one option whose value can be set to "never" or "always"

  • "never"(default)- 标签函数与其模板字面量之间不允许有空格。
  • "always" - 标签函数与其模板字面之间需要一个或多个空格。

never

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

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

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

func `Hello world`;

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

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

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

func`Hello world`;

always

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

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

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

func`Hello world`;

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

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

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

func `Hello world`;

何时不使用

🌐 When Not To Use It

如果你不想收到有关标签函数及其模板字面之间的间距使用情况的通知,那么禁用此规则是安全的。

🌐 If you don’t want to be notified about usage of spacing between tag functions and their template literals, then it’s safe to disable this rule.

版本

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

进阶读物

资源