Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-useless-computed-key.js
1 /**
2  * @fileoverview Rule to disallow unnecessary computed property keys in object literals
3  * @author Burak Yigit Kaya
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const lodash = require("lodash");
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Rule Definition
16 //------------------------------------------------------------------------------
17
18 const MESSAGE_UNNECESSARY_COMPUTED = "Unnecessarily computed property [{{property}}] found.";
19
20 module.exports = {
21     meta: {
22         type: "suggestion",
23
24         docs: {
25             description: "disallow unnecessary computed property keys in objects and classes",
26             category: "ECMAScript 6",
27             recommended: false,
28             url: "https://eslint.org/docs/rules/no-useless-computed-key"
29         },
30
31         schema: [{
32             type: "object",
33             properties: {
34                 enforceForClassMembers: {
35                     type: "boolean",
36                     default: false
37                 }
38             },
39             additionalProperties: false
40         }],
41         fixable: "code"
42     },
43     create(context) {
44         const sourceCode = context.getSourceCode();
45         const enforceForClassMembers = context.options[0] && context.options[0].enforceForClassMembers;
46
47         /**
48          * Reports a given node if it violated this rule.
49          * @param {ASTNode} node The node to check.
50          * @returns {void}
51          */
52         function check(node) {
53             if (!node.computed) {
54                 return;
55             }
56
57             const key = node.key,
58                 nodeType = typeof key.value;
59
60             let allowedKey;
61
62             if (node.type === "MethodDefinition") {
63                 allowedKey = node.static ? "prototype" : "constructor";
64             } else {
65                 allowedKey = "__proto__";
66             }
67
68             if (key.type === "Literal" && (nodeType === "string" || nodeType === "number") && key.value !== allowedKey) {
69                 context.report({
70                     node,
71                     message: MESSAGE_UNNECESSARY_COMPUTED,
72                     data: { property: sourceCode.getText(key) },
73                     fix(fixer) {
74                         const leftSquareBracket = sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken);
75                         const rightSquareBracket = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken);
76                         const tokensBetween = sourceCode.getTokensBetween(leftSquareBracket, rightSquareBracket, 1);
77
78                         if (tokensBetween.slice(0, -1).some((token, index) =>
79                             sourceCode.getText().slice(token.range[1], tokensBetween[index + 1].range[0]).trim())) {
80
81                             // If there are comments between the brackets and the property name, don't do a fix.
82                             return null;
83                         }
84
85                         const tokenBeforeLeftBracket = sourceCode.getTokenBefore(leftSquareBracket);
86
87                         // Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} })
88                         const needsSpaceBeforeKey = tokenBeforeLeftBracket.range[1] === leftSquareBracket.range[0] &&
89                             !astUtils.canTokensBeAdjacent(tokenBeforeLeftBracket, sourceCode.getFirstToken(key));
90
91                         const replacementKey = (needsSpaceBeforeKey ? " " : "") + key.raw;
92
93                         return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], replacementKey);
94                     }
95                 });
96             }
97         }
98
99         return {
100             Property: check,
101             MethodDefinition: enforceForClassMembers ? check : lodash.noop
102         };
103     }
104 };