[d565449] | 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 | /** @type {import('../shared/types').Rule} */
|
---|
| 19 | module.exports = {
|
---|
| 20 | meta: {
|
---|
| 21 | type: "suggestion",
|
---|
| 22 |
|
---|
| 23 | docs: {
|
---|
| 24 | description: "Disallow renaming import, export, and destructured assignments to the same name",
|
---|
| 25 | recommended: false,
|
---|
| 26 | url: "https://eslint.org/docs/latest/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.sourceCode,
|
---|
| 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 (
|
---|
| 136 | node.imported.range[0] !== node.local.range[0] &&
|
---|
| 137 | astUtils.getModuleExportName(node.imported) === node.local.name
|
---|
| 138 | ) {
|
---|
| 139 | reportError(node, node.imported, "Import");
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * Checks whether an export is unnecessarily renamed
|
---|
| 145 | * @param {ASTNode} node node to check
|
---|
| 146 | * @returns {void}
|
---|
| 147 | */
|
---|
| 148 | function checkExport(node) {
|
---|
| 149 | if (ignoreExport) {
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | if (
|
---|
| 154 | node.local.range[0] !== node.exported.range[0] &&
|
---|
| 155 | astUtils.getModuleExportName(node.local) === astUtils.getModuleExportName(node.exported)
|
---|
| 156 | ) {
|
---|
| 157 | reportError(node, node.local, "Export");
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | //--------------------------------------------------------------------------
|
---|
| 163 | // Public
|
---|
| 164 | //--------------------------------------------------------------------------
|
---|
| 165 |
|
---|
| 166 | return {
|
---|
| 167 | ObjectPattern: checkDestructured,
|
---|
| 168 | ImportSpecifier: checkImport,
|
---|
| 169 | ExportSpecifier: checkExport
|
---|
| 170 | };
|
---|
| 171 | }
|
---|
| 172 | };
|
---|