]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/generator-star-spacing.js
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
33 description
: "enforce consistent spacing around `*` operators in generator functions",
35 url
: "https://eslint.org/docs/rules/generator-star-spacing"
38 fixable
: "whitespace",
44 enum: ["before", "after", "both", "neither"]
49 before
: { type
: "boolean" },
50 after
: { type
: "boolean" },
51 named
: OVERRIDE_SCHEMA
,
52 anonymous
: OVERRIDE_SCHEMA
,
53 method
: OVERRIDE_SCHEMA
55 additionalProperties
: false
62 missingBefore
: "Missing space before *.",
63 missingAfter
: "Missing space after *.",
64 unexpectedBefore
: "Unexpected space before *.",
65 unexpectedAfter
: "Unexpected space after *."
71 const optionDefinitions
= {
72 before
: { before
: true, after
: false },
73 after
: { before
: false, after
: true },
74 both
: { before
: true, after
: true },
75 neither
: { before
: false, after
: false }
79 * Returns resolved option definitions based on an option and defaults
80 * @param {any} option The option object or string value
81 * @param {Object} defaults The defaults to use if options are not present
82 * @returns {Object} the resolved object definition
84 function optionToDefinition(option
, defaults
) {
89 return typeof option
=== "string"
90 ? optionDefinitions
[option
]
91 : Object
.assign({}, defaults
, option
);
94 const modes
= (function(option
) {
95 const defaults
= optionToDefinition(option
, optionDefinitions
.before
);
98 named
: optionToDefinition(option
.named
, defaults
),
99 anonymous
: optionToDefinition(option
.anonymous
, defaults
),
100 method
: optionToDefinition(option
.method
, defaults
)
102 }(context
.options
[0] || {}));
104 const sourceCode
= context
.getSourceCode();
107 * Checks if the given token is a star token or not.
108 * @param {Token} token The token to check.
109 * @returns {boolean} `true` if the token is a star token.
111 function isStarToken(token
) {
112 return token
.value
=== "*" && token
.type
=== "Punctuator";
116 * Gets the generator star token of the given function node.
117 * @param {ASTNode} node The function node to get.
118 * @returns {Token} Found star token.
120 function getStarToken(node
) {
121 return sourceCode
.getFirstToken(
122 (node
.parent
.method
|| node
.parent
.type
=== "MethodDefinition") ? node
.parent
: node
,
128 * capitalize a given string.
129 * @param {string} str the given string.
130 * @returns {string} the capitalized string.
132 function capitalize(str
) {
133 return str
[0].toUpperCase() + str
.slice(1);
137 * Checks the spacing between two tokens before or after the star token.
138 * @param {string} kind Either "named", "anonymous", or "method"
139 * @param {string} side Either "before" or "after".
140 * @param {Token} leftToken `function` keyword token if side is "before", or
141 * star token if side is "after".
142 * @param {Token} rightToken Star token if side is "before", or identifier
143 * token if side is "after".
146 function checkSpacing(kind
, side
, leftToken
, rightToken
) {
147 if (!!(rightToken
.range
[0] - leftToken
.range
[1]) !== modes
[kind
][side
]) {
148 const after
= leftToken
.value
=== "*";
149 const spaceRequired
= modes
[kind
][side
];
150 const node
= after
? leftToken
: rightToken
;
151 const messageId
= `${spaceRequired ? "missing" : "unexpected"}${capitalize(side)}`;
159 return fixer
.insertTextAfter(node
, " ");
161 return fixer
.insertTextBefore(node
, " ");
163 return fixer
.removeRange([leftToken
.range
[1], rightToken
.range
[0]]);
170 * Enforces the spacing around the star if node is a generator function.
171 * @param {ASTNode} node A function expression or declaration node.
174 function checkFunction(node
) {
175 if (!node
.generator
) {
179 const starToken
= getStarToken(node
);
180 const prevToken
= sourceCode
.getTokenBefore(starToken
);
181 const nextToken
= sourceCode
.getTokenAfter(starToken
);
185 if (node
.parent
.type
=== "MethodDefinition" || (node
.parent
.type
=== "Property" && node
.parent
.method
)) {
187 } else if (!node
.id
) {
191 // Only check before when preceded by `function`|`static` keyword
192 if (!(kind
=== "method" && starToken
=== sourceCode
.getFirstToken(node
.parent
))) {
193 checkSpacing(kind
, "before", prevToken
, starToken
);
196 checkSpacing(kind
, "after", starToken
, nextToken
);
200 FunctionDeclaration
: checkFunction
,
201 FunctionExpression
: checkFunction