|
| 1 | +/** |
| 2 | + * @fileOverview Rule to warn about importing a default export by different name |
| 3 | + * @author James Whitney |
| 4 | + */ |
| 5 | + |
| 6 | +import docsUrl from '../docsUrl'; |
| 7 | +import ExportMapBuilder from '../exportMap/builder'; |
| 8 | +import path from 'path'; |
| 9 | + |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | +// Rule Definition |
| 12 | +//------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +/** @type {import('@typescript-eslint/utils').TSESLint.RuleModule} */ |
| 15 | +const rule = { |
| 16 | + meta: { |
| 17 | + type: 'suggestion', |
| 18 | + docs: { |
| 19 | + category: 'Helpful warnings', |
| 20 | + description: 'Forbid importing a default export by a different name.', |
| 21 | + recommended: false, |
| 22 | + url: docsUrl('no-named-as-default'), |
| 23 | + }, |
| 24 | + schema: [ |
| 25 | + { |
| 26 | + type: 'object', |
| 27 | + properties: { |
| 28 | + commonjs: { |
| 29 | + type: 'boolean', |
| 30 | + }, |
| 31 | + }, |
| 32 | + additionalProperties: false, |
| 33 | + }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + |
| 37 | + create(context) { |
| 38 | + function findDefaultDestructure(properties) { |
| 39 | + const found = properties.find((property) => { |
| 40 | + if (property.key.name === 'default') { |
| 41 | + return property; |
| 42 | + } |
| 43 | + }); |
| 44 | + return found; |
| 45 | + } |
| 46 | + |
| 47 | + function getDefaultExportName(targetNode) { |
| 48 | + switch (targetNode.type) { |
| 49 | + case 'AssignmentExpression': { |
| 50 | + return targetNode.left.name; |
| 51 | + } |
| 52 | + case 'CallExpression': { |
| 53 | + const [argumentNode] = targetNode.arguments; |
| 54 | + return getDefaultExportName(argumentNode); |
| 55 | + } |
| 56 | + case 'ClassDeclaration': { |
| 57 | + if (targetNode.id && typeof targetNode.id.name === 'string') { |
| 58 | + return targetNode.id.name; |
| 59 | + } |
| 60 | + // Here we have an anonymous class. We can skip here. |
| 61 | + return; |
| 62 | + } |
| 63 | + case 'FunctionDeclaration': { |
| 64 | + return targetNode.id.name; |
| 65 | + } |
| 66 | + case 'Identifier': { |
| 67 | + return targetNode.name; |
| 68 | + } |
| 69 | + default: |
| 70 | + // This type of node is not handled. |
| 71 | + // Returning `undefined` here signifies this and causes the check to |
| 72 | + // exit early. |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + function getDefaultExportNode(exportMap) { |
| 77 | + const defaultExportNode = exportMap.exports.get('default'); |
| 78 | + if (defaultExportNode == null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + return defaultExportNode; |
| 82 | + } |
| 83 | + |
| 84 | + function getExportMap(source, context) { |
| 85 | + const exportMap = ExportMapBuilder.get(source.value, context); |
| 86 | + if (exportMap == null) { |
| 87 | + return; |
| 88 | + } |
| 89 | + if (exportMap.errors.length > 0) { |
| 90 | + exportMap.reportErrors(context, source.value); |
| 91 | + return; |
| 92 | + } |
| 93 | + return exportMap; |
| 94 | + } |
| 95 | + |
| 96 | + function handleImport(node) { |
| 97 | + |
| 98 | + const exportMap = getExportMap(node.parent.source, context); |
| 99 | + if (exportMap == null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const defaultExportNode = getDefaultExportNode(exportMap); |
| 104 | + if (defaultExportNode == null) { |
| 105 | + return; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + const defaultExportName = getDefaultExportName(defaultExportNode.declaration); |
| 110 | + if (defaultExportName === undefined) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const importTarget = node.parent.source.value; |
| 115 | + const importBasename = path.basename(exportMap.path); |
| 116 | + |
| 117 | + if (node.type === 'ImportDefaultSpecifier') { |
| 118 | + const importName = node.local.name; |
| 119 | + |
| 120 | + if (importName === defaultExportName) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + context.report({ |
| 125 | + node, |
| 126 | + message: `Caution: \`${importBasename}\` has a default export \`${defaultExportName}\`. This imports \`${defaultExportName}\` as \`${importName}\`. Check if you meant to write \`import ${defaultExportName} from '${importTarget}'\` instead.`, |
| 127 | + }); |
| 128 | + |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (node.type !== 'ImportSpecifier') { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + if (node.imported.name !== 'default') { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + const actualImportedName = node.local.name; |
| 141 | + |
| 142 | + if (actualImportedName === defaultExportName) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + context.report({ |
| 147 | + node, |
| 148 | + message: `Caution: \`${importBasename}\` has a default export \`${defaultExportName}\`. This imports \`${defaultExportName}\` as \`${actualImportedName}\`. Check if you meant to write \`import { default as ${defaultExportName} } from '${importTarget}'\` instead.`, |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + function handleRequire(node) { |
| 153 | + const options = context.options[0] || {}; |
| 154 | + |
| 155 | + if ( |
| 156 | + !options.commonjs |
| 157 | + || node.type !== 'VariableDeclarator' |
| 158 | + || !node.id || !(node.id.type === 'Identifier' || node.id.type === 'ObjectPattern') |
| 159 | + || !node.init || node.init.type !== 'CallExpression' |
| 160 | + ) { |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + let defaultDestructure; |
| 165 | + if (node.id.type === 'ObjectPattern') { |
| 166 | + defaultDestructure = findDefaultDestructure(node.id.properties); |
| 167 | + if (defaultDestructure === undefined) { |
| 168 | + return; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + const call = node.init; |
| 173 | + const [source] = call.arguments; |
| 174 | + |
| 175 | + if ( |
| 176 | + call.callee.type !== 'Identifier' || call.callee.name !== 'require' || call.arguments.length !== 1 |
| 177 | + || source.type !== 'Literal' |
| 178 | + ) { |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + const exportMap = getExportMap(source, context); |
| 183 | + if (exportMap == null) { |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + const defaultExportNode = getDefaultExportNode(exportMap); |
| 188 | + if (defaultExportNode == null) { |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + const defaultExportName = getDefaultExportName(defaultExportNode.declaration); |
| 193 | + const requireTarget = source.value; |
| 194 | + const requireBasename = path.basename(exportMap.path); |
| 195 | + const requireName = node.id.type === 'Identifier' ? node.id.name : defaultDestructure.value.name; |
| 196 | + |
| 197 | + if (defaultExportName === undefined) { |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + if (requireName === defaultExportName) { |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + if (node.id.type === 'Identifier') { |
| 206 | + context.report({ |
| 207 | + node, |
| 208 | + message: `Caution: \`${requireBasename}\` has a default export \`${defaultExportName}\`. This requires \`${defaultExportName}\` as \`${requireName}\`. Check if you meant to write \`const ${defaultExportName} = require('${requireTarget}')\` instead.`, |
| 209 | + }); |
| 210 | + return; |
| 211 | + } |
| 212 | + |
| 213 | + context.report({ |
| 214 | + node, |
| 215 | + message: `Caution: \`${requireBasename}\` has a default export \`${defaultExportName}\`. This requires \`${defaultExportName}\` as \`${requireName}\`. Check if you meant to write \`const { default: ${defaultExportName} } = require('${requireTarget}')\` instead.`, |
| 216 | + }); |
| 217 | + } |
| 218 | + |
| 219 | + return { |
| 220 | + ImportDefaultSpecifier(node) { |
| 221 | + handleImport(node); |
| 222 | + }, |
| 223 | + ImportSpecifier(node) { |
| 224 | + handleImport(node); |
| 225 | + }, |
| 226 | + VariableDeclarator(node) { |
| 227 | + handleRequire(node); |
| 228 | + }, |
| 229 | + }; |
| 230 | + }, |
| 231 | +}; |
| 232 | + |
| 233 | +module.exports = rule; |
0 commit comments