[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to require grouped accessor pairs in object literals and classes
|
---|
| 3 | * @author Milos Djermanovic
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Requirements
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | const astUtils = require("./utils/ast-utils");
|
---|
| 13 |
|
---|
| 14 | //------------------------------------------------------------------------------
|
---|
| 15 | // Typedefs
|
---|
| 16 | //------------------------------------------------------------------------------
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Property name if it can be computed statically, otherwise the list of the tokens of the key node.
|
---|
| 20 | * @typedef {string|Token[]} Key
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Accessor nodes with the same key.
|
---|
| 25 | * @typedef {Object} AccessorData
|
---|
| 26 | * @property {Key} key Accessor's key
|
---|
| 27 | * @property {ASTNode[]} getters List of getter nodes.
|
---|
| 28 | * @property {ASTNode[]} setters List of setter nodes.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | //------------------------------------------------------------------------------
|
---|
| 32 | // Helpers
|
---|
| 33 | //------------------------------------------------------------------------------
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * Checks whether or not the given lists represent the equal tokens in the same order.
|
---|
| 37 | * Tokens are compared by their properties, not by instance.
|
---|
| 38 | * @param {Token[]} left First list of tokens.
|
---|
| 39 | * @param {Token[]} right Second list of tokens.
|
---|
| 40 | * @returns {boolean} `true` if the lists have same tokens.
|
---|
| 41 | */
|
---|
| 42 | function areEqualTokenLists(left, right) {
|
---|
| 43 | if (left.length !== right.length) {
|
---|
| 44 | return false;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | for (let i = 0; i < left.length; i++) {
|
---|
| 48 | const leftToken = left[i],
|
---|
| 49 | rightToken = right[i];
|
---|
| 50 |
|
---|
| 51 | if (leftToken.type !== rightToken.type || leftToken.value !== rightToken.value) {
|
---|
| 52 | return false;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Checks whether or not the given keys are equal.
|
---|
| 61 | * @param {Key} left First key.
|
---|
| 62 | * @param {Key} right Second key.
|
---|
| 63 | * @returns {boolean} `true` if the keys are equal.
|
---|
| 64 | */
|
---|
| 65 | function areEqualKeys(left, right) {
|
---|
| 66 | if (typeof left === "string" && typeof right === "string") {
|
---|
| 67 |
|
---|
| 68 | // Statically computed names.
|
---|
| 69 | return left === right;
|
---|
| 70 | }
|
---|
| 71 | if (Array.isArray(left) && Array.isArray(right)) {
|
---|
| 72 |
|
---|
| 73 | // Token lists.
|
---|
| 74 | return areEqualTokenLists(left, right);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return false;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Checks whether or not a given node is of an accessor kind ('get' or 'set').
|
---|
| 82 | * @param {ASTNode} node A node to check.
|
---|
| 83 | * @returns {boolean} `true` if the node is of an accessor kind.
|
---|
| 84 | */
|
---|
| 85 | function isAccessorKind(node) {
|
---|
| 86 | return node.kind === "get" || node.kind === "set";
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | //------------------------------------------------------------------------------
|
---|
| 90 | // Rule Definition
|
---|
| 91 | //------------------------------------------------------------------------------
|
---|
| 92 |
|
---|
| 93 | /** @type {import('../shared/types').Rule} */
|
---|
| 94 | module.exports = {
|
---|
| 95 | meta: {
|
---|
| 96 | type: "suggestion",
|
---|
| 97 |
|
---|
| 98 | docs: {
|
---|
| 99 | description: "Require grouped accessor pairs in object literals and classes",
|
---|
| 100 | recommended: false,
|
---|
| 101 | url: "https://eslint.org/docs/latest/rules/grouped-accessor-pairs"
|
---|
| 102 | },
|
---|
| 103 |
|
---|
| 104 | schema: [
|
---|
| 105 | {
|
---|
| 106 | enum: ["anyOrder", "getBeforeSet", "setBeforeGet"]
|
---|
| 107 | }
|
---|
| 108 | ],
|
---|
| 109 |
|
---|
| 110 | messages: {
|
---|
| 111 | notGrouped: "Accessor pair {{ formerName }} and {{ latterName }} should be grouped.",
|
---|
| 112 | invalidOrder: "Expected {{ latterName }} to be before {{ formerName }}."
|
---|
| 113 | }
|
---|
| 114 | },
|
---|
| 115 |
|
---|
| 116 | create(context) {
|
---|
| 117 | const order = context.options[0] || "anyOrder";
|
---|
| 118 | const sourceCode = context.sourceCode;
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Reports the given accessor pair.
|
---|
| 122 | * @param {string} messageId messageId to report.
|
---|
| 123 | * @param {ASTNode} formerNode getter/setter node that is defined before `latterNode`.
|
---|
| 124 | * @param {ASTNode} latterNode getter/setter node that is defined after `formerNode`.
|
---|
| 125 | * @returns {void}
|
---|
| 126 | * @private
|
---|
| 127 | */
|
---|
| 128 | function report(messageId, formerNode, latterNode) {
|
---|
| 129 | context.report({
|
---|
| 130 | node: latterNode,
|
---|
| 131 | messageId,
|
---|
| 132 | loc: astUtils.getFunctionHeadLoc(latterNode.value, sourceCode),
|
---|
| 133 | data: {
|
---|
| 134 | formerName: astUtils.getFunctionNameWithKind(formerNode.value),
|
---|
| 135 | latterName: astUtils.getFunctionNameWithKind(latterNode.value)
|
---|
| 136 | }
|
---|
| 137 | });
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * Checks accessor pairs in the given list of nodes.
|
---|
| 142 | * @param {ASTNode[]} nodes The list to check.
|
---|
| 143 | * @param {Function} shouldCheck – Predicate that returns `true` if the node should be checked.
|
---|
| 144 | * @returns {void}
|
---|
| 145 | * @private
|
---|
| 146 | */
|
---|
| 147 | function checkList(nodes, shouldCheck) {
|
---|
| 148 | const accessors = [];
|
---|
| 149 | let found = false;
|
---|
| 150 |
|
---|
| 151 | for (let i = 0; i < nodes.length; i++) {
|
---|
| 152 | const node = nodes[i];
|
---|
| 153 |
|
---|
| 154 | if (shouldCheck(node) && isAccessorKind(node)) {
|
---|
| 155 |
|
---|
| 156 | // Creates a new `AccessorData` object for the given getter or setter node.
|
---|
| 157 | const name = astUtils.getStaticPropertyName(node);
|
---|
| 158 | const key = (name !== null) ? name : sourceCode.getTokens(node.key);
|
---|
| 159 |
|
---|
| 160 | // Merges the given `AccessorData` object into the given accessors list.
|
---|
| 161 | for (let j = 0; j < accessors.length; j++) {
|
---|
| 162 | const accessor = accessors[j];
|
---|
| 163 |
|
---|
| 164 | if (areEqualKeys(accessor.key, key)) {
|
---|
| 165 | accessor.getters.push(...node.kind === "get" ? [node] : []);
|
---|
| 166 | accessor.setters.push(...node.kind === "set" ? [node] : []);
|
---|
| 167 | found = true;
|
---|
| 168 | break;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | if (!found) {
|
---|
| 172 | accessors.push({
|
---|
| 173 | key,
|
---|
| 174 | getters: node.kind === "get" ? [node] : [],
|
---|
| 175 | setters: node.kind === "set" ? [node] : []
|
---|
| 176 | });
|
---|
| 177 | }
|
---|
| 178 | found = false;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | for (const { getters, setters } of accessors) {
|
---|
| 183 |
|
---|
| 184 | // Don't report accessor properties that have duplicate getters or setters.
|
---|
| 185 | if (getters.length === 1 && setters.length === 1) {
|
---|
| 186 | const [getter] = getters,
|
---|
| 187 | [setter] = setters,
|
---|
| 188 | getterIndex = nodes.indexOf(getter),
|
---|
| 189 | setterIndex = nodes.indexOf(setter),
|
---|
| 190 | formerNode = getterIndex < setterIndex ? getter : setter,
|
---|
| 191 | latterNode = getterIndex < setterIndex ? setter : getter;
|
---|
| 192 |
|
---|
| 193 | if (Math.abs(getterIndex - setterIndex) > 1) {
|
---|
| 194 | report("notGrouped", formerNode, latterNode);
|
---|
| 195 | } else if (
|
---|
| 196 | (order === "getBeforeSet" && getterIndex > setterIndex) ||
|
---|
| 197 | (order === "setBeforeGet" && getterIndex < setterIndex)
|
---|
| 198 | ) {
|
---|
| 199 | report("invalidOrder", formerNode, latterNode);
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | return {
|
---|
| 206 | ObjectExpression(node) {
|
---|
| 207 | checkList(node.properties, n => n.type === "Property");
|
---|
| 208 | },
|
---|
| 209 | ClassBody(node) {
|
---|
| 210 | checkList(node.body, n => n.type === "MethodDefinition" && !n.static);
|
---|
| 211 | checkList(node.body, n => n.type === "MethodDefinition" && n.static);
|
---|
| 212 | }
|
---|
| 213 | };
|
---|
| 214 | }
|
---|
| 215 | };
|
---|