]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/generator-star-spacing.js
d32b21fff5c54f6a6909367363f7757122d8aa17
2 * @fileoverview Rule to check the spacing around the * in generator functions.
3 * @author Jamund Ferguson
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const OVERRIDE_SCHEMA
= {
15 enum: ["before", "after", "both", "neither"]
20 before
: { type
: "boolean" },
21 after
: { type
: "boolean" }
23 additionalProperties
: false
28 /** @type {import('../shared/types').Rule} */
34 description
: "Enforce consistent spacing around `*` operators in generator functions",
36 url
: "https://eslint.org/docs/rules/generator-star-spacing"
39 fixable
: "whitespace",
45 enum: ["before", "after", "both", "neither"]
50 before
: { type
: "boolean" },
51 after
: { type
: "boolean" },
52 named
: OVERRIDE_SCHEMA
,
53 anonymous
: OVERRIDE_SCHEMA
,
54 method
: OVERRIDE_SCHEMA
56 additionalProperties
: false
63 missingBefore
: "Missing space before *.",
64 missingAfter
: "Missing space after *.",
65 unexpectedBefore
: "Unexpected space before *.",
66 unexpectedAfter
: "Unexpected space after *."
72 const optionDefinitions
= {
73 before
: { before
: true, after
: false },
74 after
: { before
: false, after
: true },
75 both
: { before
: true, after
: true },
76 neither
: { before
: false, after
: false }
80 * Returns resolved option definitions based on an option and defaults
81 * @param {any} option The option object or string value
82 * @param {Object} defaults The defaults to use if options are not present
83 * @returns {Object} the resolved object definition
85 function optionToDefinition(option
, defaults
) {
90 return typeof option
=== "string"
91 ? optionDefinitions
[option
]
92 : Object
.assign({}, defaults
, option
);
95 const modes
= (function(option
) {
96 const defaults
= optionToDefinition(option
, optionDefinitions
.before
);
99 named
: optionToDefinition(option
.named
, defaults
),
100 anonymous
: optionToDefinition(option
.anonymous
, defaults
),
101 method
: optionToDefinition(option
.method
, defaults
)
103 }(context
.options
[0] || {}));
105 const sourceCode
= context
.getSourceCode();
108 * Checks if the given token is a star token or not.
109 * @param {Token} token The token to check.
110 * @returns {boolean} `true` if the token is a star token.
112 function isStarToken(token
) {
113 return token
.value
=== "*" && token
.type
=== "Punctuator";
117 * Gets the generator star token of the given function node.
118 * @param {ASTNode} node The function node to get.
119 * @returns {Token} Found star token.
121 function getStarToken(node
) {
122 return sourceCode
.getFirstToken(
123 (node
.parent
.method
|| node
.parent
.type
=== "MethodDefinition") ? node
.parent
: node
,
129 * capitalize a given string.
130 * @param {string} str the given string.
131 * @returns {string} the capitalized string.
133 function capitalize(str
) {
134 return str
[0].toUpperCase() + str
.slice(1);
138 * Checks the spacing between two tokens before or after the star token.
139 * @param {string} kind Either "named", "anonymous", or "method"
140 * @param {string} side Either "before" or "after".
141 * @param {Token} leftToken `function` keyword token if side is "before", or
142 * star token if side is "after".
143 * @param {Token} rightToken Star token if side is "before", or identifier
144 * token if side is "after".
147 function checkSpacing(kind
, side
, leftToken
, rightToken
) {
148 if (!!(rightToken
.range
[0] - leftToken
.range
[1]) !== modes
[kind
][side
]) {
149 const after
= leftToken
.value
=== "*";
150 const spaceRequired
= modes
[kind
][side
];
151 const node
= after
? leftToken
: rightToken
;
152 const messageId
= `${spaceRequired ? "missing" : "unexpected"}${capitalize(side)}`;
160 return fixer
.insertTextAfter(node
, " ");
162 return fixer
.insertTextBefore(node
, " ");
164 return fixer
.removeRange([leftToken
.range
[1], rightToken
.range
[0]]);
171 * Enforces the spacing around the star if node is a generator function.
172 * @param {ASTNode} node A function expression or declaration node.
175 function checkFunction(node
) {
176 if (!node
.generator
) {
180 const starToken
= getStarToken(node
);
181 const prevToken
= sourceCode
.getTokenBefore(starToken
);
182 const nextToken
= sourceCode
.getTokenAfter(starToken
);
186 if (node
.parent
.type
=== "MethodDefinition" || (node
.parent
.type
=== "Property" && node
.parent
.method
)) {
188 } else if (!node
.id
) {
192 // Only check before when preceded by `function`|`static` keyword
193 if (!(kind
=== "method" && starToken
=== sourceCode
.getFirstToken(node
.parent
))) {
194 checkSpacing(kind
, "before", prevToken
, starToken
);
197 checkSpacing(kind
, "after", starToken
, nextToken
);
201 FunctionDeclaration
: checkFunction
,
202 FunctionExpression
: checkFunction