.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-useless-rename.js
1 /**
2  * @fileoverview Disallow renaming import, export, and destructured assignments to the same name.
3  * @author Kai Cataldo
4  */
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 module.exports = {
19     meta: {
20         type: "suggestion",
21
22         docs: {
23             description: "disallow renaming import, export, and destructured assignments to the same name",
24             category: "ECMAScript 6",
25             recommended: false,
26             url: "https://eslint.org/docs/rules/no-useless-rename"
27         },
28
29         fixable: "code",
30
31         schema: [
32             {
33                 type: "object",
34                 properties: {
35                     ignoreDestructuring: { type: "boolean", default: false },
36                     ignoreImport: { type: "boolean", default: false },
37                     ignoreExport: { type: "boolean", default: false }
38                 },
39                 additionalProperties: false
40             }
41         ],
42
43         messages: {
44             unnecessarilyRenamed: "{{type}} {{name}} unnecessarily renamed."
45         }
46     },
47
48     create(context) {
49         const sourceCode = context.getSourceCode(),
50             options = context.options[0] || {},
51             ignoreDestructuring = options.ignoreDestructuring === true,
52             ignoreImport = options.ignoreImport === true,
53             ignoreExport = options.ignoreExport === true;
54
55         //--------------------------------------------------------------------------
56         // Helpers
57         //--------------------------------------------------------------------------
58
59         /**
60          * Reports error for unnecessarily renamed assignments
61          * @param {ASTNode} node node to report
62          * @param {ASTNode} initial node with initial name value
63          * @param {string} type the type of the offending node
64          * @returns {void}
65          */
66         function reportError(node, initial, type) {
67             const name = initial.type === "Identifier" ? initial.name : initial.value;
68
69             return context.report({
70                 node,
71                 messageId: "unnecessarilyRenamed",
72                 data: {
73                     name,
74                     type
75                 },
76                 fix(fixer) {
77                     const replacementNode = node.type === "Property" ? node.value : node.local;
78
79                     if (sourceCode.getCommentsInside(node).length > sourceCode.getCommentsInside(replacementNode).length) {
80                         return null;
81                     }
82
83                     // Don't autofix code such as `({foo: (foo) = a} = obj);`, parens are not allowed in shorthand properties.
84                     if (
85                         replacementNode.type === "AssignmentPattern" &&
86                         astUtils.isParenthesised(sourceCode, replacementNode.left)
87                     ) {
88                         return null;
89                     }
90
91                     return fixer.replaceText(node, sourceCode.getText(replacementNode));
92                 }
93             });
94         }
95
96         /**
97          * Checks whether a destructured assignment is unnecessarily renamed
98          * @param {ASTNode} node node to check
99          * @returns {void}
100          */
101         function checkDestructured(node) {
102             if (ignoreDestructuring) {
103                 return;
104             }
105
106             for (const property of node.properties) {
107
108                 /**
109                  * Properties using shorthand syntax and rest elements can not be renamed.
110                  * If the property is computed, we have no idea if a rename is useless or not.
111                  */
112                 if (property.type !== "Property" || property.shorthand || property.computed) {
113                     continue;
114                 }
115
116                 const key = (property.key.type === "Identifier" && property.key.name) || (property.key.type === "Literal" && property.key.value);
117                 const renamedKey = property.value.type === "AssignmentPattern" ? property.value.left.name : property.value.name;
118
119                 if (key === renamedKey) {
120                     reportError(property, property.key, "Destructuring assignment");
121                 }
122             }
123         }
124
125         /**
126          * Checks whether an import is unnecessarily renamed
127          * @param {ASTNode} node node to check
128          * @returns {void}
129          */
130         function checkImport(node) {
131             if (ignoreImport) {
132                 return;
133             }
134
135             if (node.imported.name === node.local.name &&
136                     node.imported.range[0] !== node.local.range[0]) {
137                 reportError(node, node.imported, "Import");
138             }
139         }
140
141         /**
142          * Checks whether an export is unnecessarily renamed
143          * @param {ASTNode} node node to check
144          * @returns {void}
145          */
146         function checkExport(node) {
147             if (ignoreExport) {
148                 return;
149             }
150
151             if (node.local.name === node.exported.name &&
152                     node.local.range[0] !== node.exported.range[0]) {
153                 reportError(node, node.local, "Export");
154             }
155
156         }
157
158         //--------------------------------------------------------------------------
159         // Public
160         //--------------------------------------------------------------------------
161
162         return {
163             ObjectPattern: checkDestructured,
164             ImportSpecifier: checkImport,
165             ExportSpecifier: checkExport
166         };
167     }
168 };