generator-star-spacing
在生成器函数中围绕 *
运算符强制执行一致的间距
此规则报告的一些问题可通过 --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
.
生成器是 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
,则需要空格,否则不允许空格。¥
"before"
enforces spacing between the*
and thefunction
keyword. If it istrue
, a space is required, otherwise spaces are disallowed.在对象字面速记方法中,不检查
*
之前的间距,因为它们缺少function
关键字。¥In object literal shorthand methods, spacing before the
*
is not checked, as they lack afunction
keyword. -
"after"
强制*
和函数名称之间存在空格(或匿名生成器函数的左括号)。如果是true
,则需要空格,否则不允许空格。¥
"after"
enforces spacing between the*
and the function name (or the opening parenthesis for anonymous generator functions). If it istrue
, a space is required, otherwise spaces are disallowed.
默认值为 {"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
为命名函数提供覆盖¥
named
provides overrides for named functions -
anonymous
为匿名函数提供覆盖¥
anonymous
provides overrides for anonymous functions -
method
为类方法或属性函数简写提供覆盖¥
method
provides overrides for class methods or property function shorthand
具有覆盖的配置示例:
¥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.
示例
¥Examples
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 中引入。