[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Enforce spacing between rest and spread operators and their expressions.
|
---|
| 3 | * @author Kai Cataldo
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 | // Rule Definition
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
| 13 | /** @type {import('../shared/types').Rule} */
|
---|
| 14 | module.exports = {
|
---|
| 15 | meta: {
|
---|
| 16 | deprecated: true,
|
---|
| 17 | replacedBy: [],
|
---|
| 18 | type: "layout",
|
---|
| 19 |
|
---|
| 20 | docs: {
|
---|
| 21 | description: "Enforce spacing between rest and spread operators and their expressions",
|
---|
| 22 | recommended: false,
|
---|
| 23 | url: "https://eslint.org/docs/latest/rules/rest-spread-spacing"
|
---|
| 24 | },
|
---|
| 25 |
|
---|
| 26 | fixable: "whitespace",
|
---|
| 27 |
|
---|
| 28 | schema: [
|
---|
| 29 | {
|
---|
| 30 | enum: ["always", "never"]
|
---|
| 31 | }
|
---|
| 32 | ],
|
---|
| 33 |
|
---|
| 34 | messages: {
|
---|
| 35 | unexpectedWhitespace: "Unexpected whitespace after {{type}} operator.",
|
---|
| 36 | expectedWhitespace: "Expected whitespace after {{type}} operator."
|
---|
| 37 | }
|
---|
| 38 | },
|
---|
| 39 |
|
---|
| 40 | create(context) {
|
---|
| 41 | const sourceCode = context.sourceCode,
|
---|
| 42 | alwaysSpace = context.options[0] === "always";
|
---|
| 43 |
|
---|
| 44 | //--------------------------------------------------------------------------
|
---|
| 45 | // Helpers
|
---|
| 46 | //--------------------------------------------------------------------------
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Checks whitespace between rest/spread operators and their expressions
|
---|
| 50 | * @param {ASTNode} node The node to check
|
---|
| 51 | * @returns {void}
|
---|
| 52 | */
|
---|
| 53 | function checkWhiteSpace(node) {
|
---|
| 54 | const operator = sourceCode.getFirstToken(node),
|
---|
| 55 | nextToken = sourceCode.getTokenAfter(operator),
|
---|
| 56 | hasWhitespace = sourceCode.isSpaceBetweenTokens(operator, nextToken);
|
---|
| 57 | let type;
|
---|
| 58 |
|
---|
| 59 | switch (node.type) {
|
---|
| 60 | case "SpreadElement":
|
---|
| 61 | type = "spread";
|
---|
| 62 | if (node.parent.type === "ObjectExpression") {
|
---|
| 63 | type += " property";
|
---|
| 64 | }
|
---|
| 65 | break;
|
---|
| 66 | case "RestElement":
|
---|
| 67 | type = "rest";
|
---|
| 68 | if (node.parent.type === "ObjectPattern") {
|
---|
| 69 | type += " property";
|
---|
| 70 | }
|
---|
| 71 | break;
|
---|
| 72 | case "ExperimentalSpreadProperty":
|
---|
| 73 | type = "spread property";
|
---|
| 74 | break;
|
---|
| 75 | case "ExperimentalRestProperty":
|
---|
| 76 | type = "rest property";
|
---|
| 77 | break;
|
---|
| 78 | default:
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (alwaysSpace && !hasWhitespace) {
|
---|
| 83 | context.report({
|
---|
| 84 | node,
|
---|
| 85 | loc: operator.loc,
|
---|
| 86 | messageId: "expectedWhitespace",
|
---|
| 87 | data: {
|
---|
| 88 | type
|
---|
| 89 | },
|
---|
| 90 | fix(fixer) {
|
---|
| 91 | return fixer.replaceTextRange([operator.range[1], nextToken.range[0]], " ");
|
---|
| 92 | }
|
---|
| 93 | });
|
---|
| 94 | } else if (!alwaysSpace && hasWhitespace) {
|
---|
| 95 | context.report({
|
---|
| 96 | node,
|
---|
| 97 | loc: {
|
---|
| 98 | start: operator.loc.end,
|
---|
| 99 | end: nextToken.loc.start
|
---|
| 100 | },
|
---|
| 101 | messageId: "unexpectedWhitespace",
|
---|
| 102 | data: {
|
---|
| 103 | type
|
---|
| 104 | },
|
---|
| 105 | fix(fixer) {
|
---|
| 106 | return fixer.removeRange([operator.range[1], nextToken.range[0]]);
|
---|
| 107 | }
|
---|
| 108 | });
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | //--------------------------------------------------------------------------
|
---|
| 113 | // Public
|
---|
| 114 | //--------------------------------------------------------------------------
|
---|
| 115 |
|
---|
| 116 | return {
|
---|
| 117 | SpreadElement: checkWhiteSpace,
|
---|
| 118 | RestElement: checkWhiteSpace,
|
---|
| 119 | ExperimentalSpreadProperty: checkWhiteSpace,
|
---|
| 120 | ExperimentalRestProperty: checkWhiteSpace
|
---|
| 121 | };
|
---|
| 122 | }
|
---|
| 123 | };
|
---|