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