generator-star-spacing
在生成器函数中围绕 * 运算符强制执行一致的间距
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
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.
生成器是 ECMAScript 6 中一种新的函数类型,可以随时间返回多个值。这些特殊函数通过在 function 关键字后放置 * 来表示。
🌐 Generators are a new type of function in ECMAScript 6 that can return multiple values over time.
These special functions are indicated by placing an * after the function keyword.
以下是生成器函数的示例:
🌐 Here is an example of a generator function:
function* generator() {
yield "44";
yield "55";
}
这也是有效的:
🌐 This is also valid:
function *generator() {
yield "44";
yield "55";
}
这也是有效的:
🌐 This is valid as well:
function * generator() {
yield "44";
yield "55";
}
为了在使用生成器时保持一致性,这条规则强制 * 只能出现在一个位置。
🌐 To keep a sense of consistency when using generators this rule enforces a single position for the *.
规则详情
🌐 Rule Details
此规则旨在强制在生成器函数的 * 周围使用空格。
🌐 This rule aims to enforce spacing around the * of generator functions.
选项
🌐 Options
该规则接受一个选项对象,该对象有两个键 "before" 和 "after",其布尔值为 true 或 false。
🌐 The rule takes one option, an object, which has two keys "before" and "after" having boolean values true or false.
-
"before"强制在*和function关键字之间保持间距。如果它是true,则需要空格,否则不允许空格。在对象字面量简写方法中,
*之前的空格不会被检查,因为它们缺少function关键字。 -
"after"强制在*和函数名(或匿名生成器函数的开括号)之间保持空格。如果是true,则需要一个空格,否则不允许空格。
默认值是 {"before": true, "after": false}。
🌐 The default is {"before": true, "after": false}.
一个示例配置:
🌐 An example configuration:
"generator-star-spacing": ["error", {"before": true, "after": false}]
并且该选项具有速记作为字符串关键字:
🌐 And the option has shorthand as a string keyword:
{"before": true, "after": false}→"before"{"before": false, "after": true}→"after"{"before": true, "after": true}→"both"{"before": false, "after": false}→"neither"
简写配置示例:
🌐 An example of shorthand configuration:
"generator-star-spacing": ["error", "after"]
此外,此规则允许通过覆盖每个功能类型来进一步配置。
🌐 Additionally, this rule allows further configurability via overrides per function type.
named为命名函数提供了覆盖anonymous为匿名函数提供了重写method提供类方法或属性函数简写的重写
具有覆盖的配置示例:
🌐 An example of a configuration with overrides:
"generator-star-spacing": ["error", {
"before": false,
"after": true,
"anonymous": "neither",
"method": {"before": true, "after": true}
}]
在上面的示例配置中,顶层的 "before" 和 "after" 选项定义了规则的默认行为,而 "anonymous" 和 "method" 选项则覆盖默认行为。覆盖可以是包含 "before" 和 "after" 的对象,也可以像上面一样是简写字符串。
🌐 In the example configuration above, the top level "before" and "after" options define the default behavior of
the rule, while the "anonymous" and "method" options override the default behavior.
Overrides can be either an object with "before" and "after", or a shorthand string as above.
before
使用 "before" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "before" option:
/*eslint generator-star-spacing: ["error", {"before": true, "after": false}]*/
function *generator() {}
var anonymous = function *() {};
var shorthand = { *generator() {} };
after
使用 "after" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "after" option:
/*eslint generator-star-spacing: ["error", {"before": false, "after": true}]*/
function* generator() {}
var anonymous = function* () {};
var shorthand = { * generator() {} };
both
使用 "both" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "both" option:
/*eslint generator-star-spacing: ["error", {"before": true, "after": true}]*/
function * generator() {}
var anonymous = function * () {};
var shorthand = { * generator() {} };
neither
使用 "neither" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "neither" option:
/*eslint generator-star-spacing: ["error", {"before": false, "after": false}]*/
function*generator() {}
var anonymous = function*() {};
var shorthand = { *generator() {} };
存在覆盖时该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with overrides present:
/*eslint generator-star-spacing: ["error", {
"before": false,
"after": true,
"anonymous": "neither",
"method": {"before": true, "after": true}
}]*/
function * generator() {}
var anonymous = function* () {};
var shorthand = { *generator() {} };
class Class { static* method() {} }
包含覆盖的此规则的正确代码示例:
🌐 Examples of correct code for this rule with overrides present:
/*eslint generator-star-spacing: ["error", {
"before": false,
"after": true,
"anonymous": "neither",
"method": {"before": true, "after": true}
}]*/
function* generator() {}
var anonymous = function*() {};
var shorthand = { * generator() {} };
class Class { static * method() {} }
何时不使用
🌐 When Not To Use It
如果你的项目不使用生成器,或者你不关心间距一致性,则不需要此规则。
🌐 If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule.
版本
此规则是在 ESLint v0.17.0 中引入。