template-curly-spacing

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

🔧 Fixable

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

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

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

¥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"(默认情况下) - 不允许大括号对内有空格。

    ¥"never" (by default) - Disallows spaces inside of the curly brace pair.

  • "always" - 大括号对内需要一个或多个空格。

    ¥"always" - Requires one or more spaces inside of the curly brace pair.

示例

¥Examples

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 中引入。

资源

ESLint 中文网
粤ICP备13048890号