[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to check the spacing around the * in generator functions.
|
---|
| 3 | * @author Jamund Ferguson
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 | // Rule Definition
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
| 13 | const OVERRIDE_SCHEMA = {
|
---|
| 14 | oneOf: [
|
---|
| 15 | {
|
---|
| 16 | enum: ["before", "after", "both", "neither"]
|
---|
| 17 | },
|
---|
| 18 | {
|
---|
| 19 | type: "object",
|
---|
| 20 | properties: {
|
---|
| 21 | before: { type: "boolean" },
|
---|
| 22 | after: { type: "boolean" }
|
---|
| 23 | },
|
---|
| 24 | additionalProperties: false
|
---|
| 25 | }
|
---|
| 26 | ]
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | /** @type {import('../shared/types').Rule} */
|
---|
| 30 | module.exports = {
|
---|
| 31 | meta: {
|
---|
| 32 | deprecated: true,
|
---|
| 33 | replacedBy: [],
|
---|
| 34 | type: "layout",
|
---|
| 35 |
|
---|
| 36 | docs: {
|
---|
| 37 | description: "Enforce consistent spacing around `*` operators in generator functions",
|
---|
| 38 | recommended: false,
|
---|
| 39 | url: "https://eslint.org/docs/latest/rules/generator-star-spacing"
|
---|
| 40 | },
|
---|
| 41 |
|
---|
| 42 | fixable: "whitespace",
|
---|
| 43 |
|
---|
| 44 | schema: [
|
---|
| 45 | {
|
---|
| 46 | oneOf: [
|
---|
| 47 | {
|
---|
| 48 | enum: ["before", "after", "both", "neither"]
|
---|
| 49 | },
|
---|
| 50 | {
|
---|
| 51 | type: "object",
|
---|
| 52 | properties: {
|
---|
| 53 | before: { type: "boolean" },
|
---|
| 54 | after: { type: "boolean" },
|
---|
| 55 | named: OVERRIDE_SCHEMA,
|
---|
| 56 | anonymous: OVERRIDE_SCHEMA,
|
---|
| 57 | method: OVERRIDE_SCHEMA
|
---|
| 58 | },
|
---|
| 59 | additionalProperties: false
|
---|
| 60 | }
|
---|
| 61 | ]
|
---|
| 62 | }
|
---|
| 63 | ],
|
---|
| 64 |
|
---|
| 65 | messages: {
|
---|
| 66 | missingBefore: "Missing space before *.",
|
---|
| 67 | missingAfter: "Missing space after *.",
|
---|
| 68 | unexpectedBefore: "Unexpected space before *.",
|
---|
| 69 | unexpectedAfter: "Unexpected space after *."
|
---|
| 70 | }
|
---|
| 71 | },
|
---|
| 72 |
|
---|
| 73 | create(context) {
|
---|
| 74 |
|
---|
| 75 | const optionDefinitions = {
|
---|
| 76 | before: { before: true, after: false },
|
---|
| 77 | after: { before: false, after: true },
|
---|
| 78 | both: { before: true, after: true },
|
---|
| 79 | neither: { before: false, after: false }
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Returns resolved option definitions based on an option and defaults
|
---|
| 84 | * @param {any} option The option object or string value
|
---|
| 85 | * @param {Object} defaults The defaults to use if options are not present
|
---|
| 86 | * @returns {Object} the resolved object definition
|
---|
| 87 | */
|
---|
| 88 | function optionToDefinition(option, defaults) {
|
---|
| 89 | if (!option) {
|
---|
| 90 | return defaults;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return typeof option === "string"
|
---|
| 94 | ? optionDefinitions[option]
|
---|
| 95 | : Object.assign({}, defaults, option);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | const modes = (function(option) {
|
---|
| 99 | const defaults = optionToDefinition(option, optionDefinitions.before);
|
---|
| 100 |
|
---|
| 101 | return {
|
---|
| 102 | named: optionToDefinition(option.named, defaults),
|
---|
| 103 | anonymous: optionToDefinition(option.anonymous, defaults),
|
---|
| 104 | method: optionToDefinition(option.method, defaults)
|
---|
| 105 | };
|
---|
| 106 | }(context.options[0] || {}));
|
---|
| 107 |
|
---|
| 108 | const sourceCode = context.sourceCode;
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Checks if the given token is a star token or not.
|
---|
| 112 | * @param {Token} token The token to check.
|
---|
| 113 | * @returns {boolean} `true` if the token is a star token.
|
---|
| 114 | */
|
---|
| 115 | function isStarToken(token) {
|
---|
| 116 | return token.value === "*" && token.type === "Punctuator";
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Gets the generator star token of the given function node.
|
---|
| 121 | * @param {ASTNode} node The function node to get.
|
---|
| 122 | * @returns {Token} Found star token.
|
---|
| 123 | */
|
---|
| 124 | function getStarToken(node) {
|
---|
| 125 | return sourceCode.getFirstToken(
|
---|
| 126 | (node.parent.method || node.parent.type === "MethodDefinition") ? node.parent : node,
|
---|
| 127 | isStarToken
|
---|
| 128 | );
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * capitalize a given string.
|
---|
| 133 | * @param {string} str the given string.
|
---|
| 134 | * @returns {string} the capitalized string.
|
---|
| 135 | */
|
---|
| 136 | function capitalize(str) {
|
---|
| 137 | return str[0].toUpperCase() + str.slice(1);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * Checks the spacing between two tokens before or after the star token.
|
---|
| 142 | * @param {string} kind Either "named", "anonymous", or "method"
|
---|
| 143 | * @param {string} side Either "before" or "after".
|
---|
| 144 | * @param {Token} leftToken `function` keyword token if side is "before", or
|
---|
| 145 | * star token if side is "after".
|
---|
| 146 | * @param {Token} rightToken Star token if side is "before", or identifier
|
---|
| 147 | * token if side is "after".
|
---|
| 148 | * @returns {void}
|
---|
| 149 | */
|
---|
| 150 | function checkSpacing(kind, side, leftToken, rightToken) {
|
---|
| 151 | if (!!(rightToken.range[0] - leftToken.range[1]) !== modes[kind][side]) {
|
---|
| 152 | const after = leftToken.value === "*";
|
---|
| 153 | const spaceRequired = modes[kind][side];
|
---|
| 154 | const node = after ? leftToken : rightToken;
|
---|
| 155 | const messageId = `${spaceRequired ? "missing" : "unexpected"}${capitalize(side)}`;
|
---|
| 156 |
|
---|
| 157 | context.report({
|
---|
| 158 | node,
|
---|
| 159 | messageId,
|
---|
| 160 | fix(fixer) {
|
---|
| 161 | if (spaceRequired) {
|
---|
| 162 | if (after) {
|
---|
| 163 | return fixer.insertTextAfter(node, " ");
|
---|
| 164 | }
|
---|
| 165 | return fixer.insertTextBefore(node, " ");
|
---|
| 166 | }
|
---|
| 167 | return fixer.removeRange([leftToken.range[1], rightToken.range[0]]);
|
---|
| 168 | }
|
---|
| 169 | });
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
| 174 | * Enforces the spacing around the star if node is a generator function.
|
---|
| 175 | * @param {ASTNode} node A function expression or declaration node.
|
---|
| 176 | * @returns {void}
|
---|
| 177 | */
|
---|
| 178 | function checkFunction(node) {
|
---|
| 179 | if (!node.generator) {
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | const starToken = getStarToken(node);
|
---|
| 184 | const prevToken = sourceCode.getTokenBefore(starToken);
|
---|
| 185 | const nextToken = sourceCode.getTokenAfter(starToken);
|
---|
| 186 |
|
---|
| 187 | let kind = "named";
|
---|
| 188 |
|
---|
| 189 | if (node.parent.type === "MethodDefinition" || (node.parent.type === "Property" && node.parent.method)) {
|
---|
| 190 | kind = "method";
|
---|
| 191 | } else if (!node.id) {
|
---|
| 192 | kind = "anonymous";
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | // Only check before when preceded by `function`|`static` keyword
|
---|
| 196 | if (!(kind === "method" && starToken === sourceCode.getFirstToken(node.parent))) {
|
---|
| 197 | checkSpacing(kind, "before", prevToken, starToken);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | checkSpacing(kind, "after", starToken, nextToken);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | return {
|
---|
| 204 | FunctionDeclaration: checkFunction,
|
---|
| 205 | FunctionExpression: checkFunction
|
---|
| 206 | };
|
---|
| 207 |
|
---|
| 208 | }
|
---|
| 209 | };
|
---|