Enforce Usage of Spacing in Template Strings (template-curly-spacing)

强制模板字符串中空格的使用 (template-curly-spacing)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

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"]
}

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

该规则有一个选项:

Examples

never

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

默认选项 "never"错误 代码示例:

/*eslint template-curly-spacing: "error"*/

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

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

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

默认选项 "never"正确 代码示例:

/*eslint template-curly-spacing: "error"*/

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

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

always

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

选项 "always"错误 代码示例:

/*eslint template-curly-spacing: ["error", "always"]*/

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

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

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

选项 "always"正确 代码示例:

/*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.

如果你不想收到关于模板字符串中空格的使用情况的通知,可以禁用此规则。

Version

This rule was introduced in ESLint 2.0.0-rc.0.

该规则在 ESLint 2.0.0-rc.0 中被引入。

Resources