[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to define spacing before/after arrow function's arrow.
|
---|
| 3 | * @author Jxck
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Requirements
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | const astUtils = require("./utils/ast-utils");
|
---|
| 13 |
|
---|
| 14 | //------------------------------------------------------------------------------
|
---|
| 15 | // Rule Definition
|
---|
| 16 | //------------------------------------------------------------------------------
|
---|
| 17 |
|
---|
| 18 | /** @type {import('../shared/types').Rule} */
|
---|
| 19 | module.exports = {
|
---|
| 20 | meta: {
|
---|
| 21 | deprecated: true,
|
---|
| 22 | replacedBy: [],
|
---|
| 23 | type: "layout",
|
---|
| 24 |
|
---|
| 25 | docs: {
|
---|
| 26 | description: "Enforce consistent spacing before and after the arrow in arrow functions",
|
---|
| 27 | recommended: false,
|
---|
| 28 | url: "https://eslint.org/docs/latest/rules/arrow-spacing"
|
---|
| 29 | },
|
---|
| 30 |
|
---|
| 31 | fixable: "whitespace",
|
---|
| 32 |
|
---|
| 33 | schema: [
|
---|
| 34 | {
|
---|
| 35 | type: "object",
|
---|
| 36 | properties: {
|
---|
| 37 | before: {
|
---|
| 38 | type: "boolean",
|
---|
| 39 | default: true
|
---|
| 40 | },
|
---|
| 41 | after: {
|
---|
| 42 | type: "boolean",
|
---|
| 43 | default: true
|
---|
| 44 | }
|
---|
| 45 | },
|
---|
| 46 | additionalProperties: false
|
---|
| 47 | }
|
---|
| 48 | ],
|
---|
| 49 |
|
---|
| 50 | messages: {
|
---|
| 51 | expectedBefore: "Missing space before =>.",
|
---|
| 52 | unexpectedBefore: "Unexpected space before =>.",
|
---|
| 53 |
|
---|
| 54 | expectedAfter: "Missing space after =>.",
|
---|
| 55 | unexpectedAfter: "Unexpected space after =>."
|
---|
| 56 | }
|
---|
| 57 | },
|
---|
| 58 |
|
---|
| 59 | create(context) {
|
---|
| 60 |
|
---|
| 61 | // merge rules with default
|
---|
| 62 | const rule = Object.assign({}, context.options[0]);
|
---|
| 63 |
|
---|
| 64 | rule.before = rule.before !== false;
|
---|
| 65 | rule.after = rule.after !== false;
|
---|
| 66 |
|
---|
| 67 | const sourceCode = context.sourceCode;
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Get tokens of arrow(`=>`) and before/after arrow.
|
---|
| 71 | * @param {ASTNode} node The arrow function node.
|
---|
| 72 | * @returns {Object} Tokens of arrow and before/after arrow.
|
---|
| 73 | */
|
---|
| 74 | function getTokens(node) {
|
---|
| 75 | const arrow = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken);
|
---|
| 76 |
|
---|
| 77 | return {
|
---|
| 78 | before: sourceCode.getTokenBefore(arrow),
|
---|
| 79 | arrow,
|
---|
| 80 | after: sourceCode.getTokenAfter(arrow)
|
---|
| 81 | };
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Count spaces before/after arrow(`=>`) token.
|
---|
| 86 | * @param {Object} tokens Tokens before/after arrow.
|
---|
| 87 | * @returns {Object} count of space before/after arrow.
|
---|
| 88 | */
|
---|
| 89 | function countSpaces(tokens) {
|
---|
| 90 | const before = tokens.arrow.range[0] - tokens.before.range[1];
|
---|
| 91 | const after = tokens.after.range[0] - tokens.arrow.range[1];
|
---|
| 92 |
|
---|
| 93 | return { before, after };
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * Determines whether space(s) before after arrow(`=>`) is satisfy rule.
|
---|
| 98 | * if before/after value is `true`, there should be space(s).
|
---|
| 99 | * if before/after value is `false`, there should be no space.
|
---|
| 100 | * @param {ASTNode} node The arrow function node.
|
---|
| 101 | * @returns {void}
|
---|
| 102 | */
|
---|
| 103 | function spaces(node) {
|
---|
| 104 | const tokens = getTokens(node);
|
---|
| 105 | const countSpace = countSpaces(tokens);
|
---|
| 106 |
|
---|
| 107 | if (rule.before) {
|
---|
| 108 |
|
---|
| 109 | // should be space(s) before arrow
|
---|
| 110 | if (countSpace.before === 0) {
|
---|
| 111 | context.report({
|
---|
| 112 | node: tokens.before,
|
---|
| 113 | messageId: "expectedBefore",
|
---|
| 114 | fix(fixer) {
|
---|
| 115 | return fixer.insertTextBefore(tokens.arrow, " ");
|
---|
| 116 | }
|
---|
| 117 | });
|
---|
| 118 | }
|
---|
| 119 | } else {
|
---|
| 120 |
|
---|
| 121 | // should be no space before arrow
|
---|
| 122 | if (countSpace.before > 0) {
|
---|
| 123 | context.report({
|
---|
| 124 | node: tokens.before,
|
---|
| 125 | messageId: "unexpectedBefore",
|
---|
| 126 | fix(fixer) {
|
---|
| 127 | return fixer.removeRange([tokens.before.range[1], tokens.arrow.range[0]]);
|
---|
| 128 | }
|
---|
| 129 | });
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | if (rule.after) {
|
---|
| 134 |
|
---|
| 135 | // should be space(s) after arrow
|
---|
| 136 | if (countSpace.after === 0) {
|
---|
| 137 | context.report({
|
---|
| 138 | node: tokens.after,
|
---|
| 139 | messageId: "expectedAfter",
|
---|
| 140 | fix(fixer) {
|
---|
| 141 | return fixer.insertTextAfter(tokens.arrow, " ");
|
---|
| 142 | }
|
---|
| 143 | });
|
---|
| 144 | }
|
---|
| 145 | } else {
|
---|
| 146 |
|
---|
| 147 | // should be no space after arrow
|
---|
| 148 | if (countSpace.after > 0) {
|
---|
| 149 | context.report({
|
---|
| 150 | node: tokens.after,
|
---|
| 151 | messageId: "unexpectedAfter",
|
---|
| 152 | fix(fixer) {
|
---|
| 153 | return fixer.removeRange([tokens.arrow.range[1], tokens.after.range[0]]);
|
---|
| 154 | }
|
---|
| 155 | });
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return {
|
---|
| 161 | ArrowFunctionExpression: spaces
|
---|
| 162 | };
|
---|
| 163 | }
|
---|
| 164 | };
|
---|