|
| 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('eslint').Rule.RuleModule} */ |
| 15 | +module.exports = { |
| 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 | + default: false, |
| 30 | + type: 'boolean', |
| 31 | + }, |
| 32 | + preventRenamingBindings: { |
| 33 | + default: true, |
| 34 | + type: 'boolean', |
| 35 | + }, |
| 36 | + }, |
| 37 | + additionalProperties: false, |
| 38 | + }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + |
| 42 | + create(context) { |
| 43 | + const { |
| 44 | + commonjs = false, |
| 45 | + preventRenamingBindings = true, |
| 46 | + } = context.options[0] || {}; |
| 47 | + |
| 48 | + function getDefaultExportName(targetNode) { |
| 49 | + if (targetNode == null) { |
| 50 | + return; |
| 51 | + } |
| 52 | + switch (targetNode.type) { |
| 53 | + case 'AssignmentExpression': { |
| 54 | + if (!preventRenamingBindings) { |
| 55 | + // Allow assignments to be renamed when the `preventRenamingBindings` |
| 56 | + // option is set to `false`. |
| 57 | + // |
| 58 | + // export default Foo = 1; |
| 59 | + return; |
| 60 | + } |
| 61 | + return targetNode.left.name; |
| 62 | + } |
| 63 | + case 'CallExpression': { |
| 64 | + const [argumentNode] = targetNode.arguments; |
| 65 | + return getDefaultExportName(argumentNode); |
| 66 | + } |
| 67 | + case 'ClassDeclaration': { |
| 68 | + if (targetNode.id && typeof targetNode.id.name === 'string') { |
| 69 | + return targetNode.id.name; |
| 70 | + } |
| 71 | + // Here we have an anonymous class. We can skip here. |
| 72 | + return; |
| 73 | + } |
| 74 | + case 'ExportSpecifier': { |
| 75 | + return targetNode.local.name; |
| 76 | + } |
| 77 | + case 'FunctionDeclaration': { |
| 78 | + return targetNode.id.name; |
| 79 | + } |
| 80 | + case 'Identifier': { |
| 81 | + if (!preventRenamingBindings) { |
| 82 | + // Allow identifier to be renamed when the `preventRenamingBindings` |
| 83 | + // option is set to `false`. |
| 84 | + // |
| 85 | + // const foo = 'foo'; |
| 86 | + // export default foo; |
| 87 | + return; |
| 88 | + } |
| 89 | + return targetNode.name; |
| 90 | + } |
| 91 | + default: |
| 92 | + // This type of node is not handled. |
| 93 | + // Returning `undefined` here signifies this and causes the check to |
| 94 | + // exit early. |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + function getDefaultExportNode(exportMap) { |
| 99 | + const defaultExportNode = exportMap.exports.get('default'); |
| 100 | + if (defaultExportNode == null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (defaultExportNode.type === 'ExportDefaultDeclaration') { |
| 105 | + return defaultExportNode.declaration; |
| 106 | + } |
| 107 | + |
| 108 | + if (defaultExportNode.type === 'ExportNamedDeclaration') { |
| 109 | + return defaultExportNode.specifiers.find((specifier) => specifier.exported.name === 'default'); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + function getExportMap(source, context) { |
| 114 | + const exportMap = ExportMapBuilder.get(source.value, context); |
| 115 | + if (exportMap == null) { |
| 116 | + return; |
| 117 | + } |
| 118 | + if (exportMap.errors.length > 0) { |
| 119 | + exportMap.reportErrors(context, source.value); |
| 120 | + return; |
| 121 | + } |
| 122 | + return exportMap; |
| 123 | + } |
| 124 | + |
| 125 | + function handleImport(node) { |
| 126 | + const exportMap = getExportMap(node.parent.source, context); |
| 127 | + if (exportMap == null) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + const defaultExportNode = getDefaultExportNode(exportMap); |
| 132 | + if (defaultExportNode == null) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + const defaultExportName = getDefaultExportName(defaultExportNode); |
| 137 | + if (defaultExportName === undefined) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + const importTarget = node.parent.source.value; |
| 142 | + const importBasename = path.basename(exportMap.path); |
| 143 | + |
| 144 | + if (node.type === 'ImportDefaultSpecifier') { |
| 145 | + const importName = node.local.name; |
| 146 | + |
| 147 | + if (importName === defaultExportName) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + context.report({ |
| 152 | + node, |
| 153 | + message: `Caution: \`{{importBasename}}\` has a default export \`{{defaultExportName}}\`. This imports \`{{defaultExportName}}\` as \`{{importName}}\`. Check if you meant to write \`import {{defaultExportName} from '{{importTarget}}'\` instead.`, |
| 154 | + data: { |
| 155 | + defaultExportName, |
| 156 | + importBasename, |
| 157 | + importName, |
| 158 | + importTarget, |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + if (node.type !== 'ImportSpecifier' || node.imported.name !== 'default') { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + const actualImportedName = node.local.name; |
| 170 | + |
| 171 | + if (actualImportedName === defaultExportName) { |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + context.report({ |
| 176 | + node, |
| 177 | + 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.`, |
| 178 | + data: { |
| 179 | + actualImportedName, |
| 180 | + defaultExportName, |
| 181 | + importBasename, |
| 182 | + importTarget, |
| 183 | + }, |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + function handleRequire(node) { |
| 188 | + if ( |
| 189 | + !commonjs |
| 190 | + || node.type !== 'VariableDeclarator' |
| 191 | + || !node.id |
| 192 | + || !(node.id.type === 'Identifier' |
| 193 | + || node.id.type === 'ObjectPattern') |
| 194 | + || !node.init |
| 195 | + || node.init.type !== 'CallExpression' |
| 196 | + ) { |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + let defaultDestructure; |
| 201 | + if (node.id.type === 'ObjectPattern') { |
| 202 | + defaultDestructure = node.id.properties.find((property) => property.key.name === 'default'); |
| 203 | + if (defaultDestructure === undefined) { |
| 204 | + return; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + const call = node.init; |
| 209 | + const [source] = call.arguments; |
| 210 | + |
| 211 | + if ( |
| 212 | + call.callee.type !== 'Identifier' |
| 213 | + || call.callee.name !== 'require' |
| 214 | + || call.arguments.length !== 1 |
| 215 | + || source.type !== 'Literal' |
| 216 | + ) { |
| 217 | + return; |
| 218 | + } |
| 219 | + |
| 220 | + const exportMap = getExportMap(source, context); |
| 221 | + if (exportMap == null) { |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + const defaultExportNode = getDefaultExportNode(exportMap); |
| 226 | + if (defaultExportNode == null) { |
| 227 | + return; |
| 228 | + } |
| 229 | + |
| 230 | + const defaultExportName = getDefaultExportName(defaultExportNode); |
| 231 | + const requireTarget = source.value; |
| 232 | + const requireBasename = path.basename(exportMap.path); |
| 233 | + const requireName = node.id.type === 'Identifier' ? node.id.name : defaultDestructure.value.name; |
| 234 | + |
| 235 | + if (defaultExportName === undefined) { |
| 236 | + return; |
| 237 | + } |
| 238 | + |
| 239 | + if (requireName === defaultExportName) { |
| 240 | + return; |
| 241 | + } |
| 242 | + |
| 243 | + const data = { |
| 244 | + defaultExportName, |
| 245 | + requireBasename, |
| 246 | + requireName, |
| 247 | + requireTarget, |
| 248 | + }; |
| 249 | + |
| 250 | + if (node.id.type === 'Identifier') { |
| 251 | + context.report({ |
| 252 | + node, |
| 253 | + message: `Caution: \`{{requireBasename}}\` has a default export \`{{defaultExportName}}\`. This requires \`{{defaultExportName}}\` as \`{{requireName}}\`. Check if you meant to write \`const {{defaultExportName}} = require('{{requireTarget}}')\` instead.`, |
| 254 | + data, |
| 255 | + }); |
| 256 | + return; |
| 257 | + } |
| 258 | + |
| 259 | + context.report({ |
| 260 | + node, |
| 261 | + 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.`, |
| 262 | + data, |
| 263 | + }); |
| 264 | + } |
| 265 | + |
| 266 | + return { |
| 267 | + ImportDefaultSpecifier(node) { |
| 268 | + handleImport(node); |
| 269 | + }, |
| 270 | + ImportSpecifier(node) { |
| 271 | + handleImport(node); |
| 272 | + }, |
| 273 | + VariableDeclarator(node) { |
| 274 | + handleRequire(node); |
| 275 | + }, |
| 276 | + }; |
| 277 | + }, |
| 278 | +}; |
0 commit comments