|
| 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 getDefaultExportName(defaultExportNode) { |
| 39 | + return defaultExportNode.declaration.name; |
| 40 | + } |
| 41 | + |
| 42 | + function getDefaultExportNode(exportMap) { |
| 43 | + const defaultExportNode = exportMap.exports.get('default'); |
| 44 | + if (defaultExportNode == null) { |
| 45 | + return; |
| 46 | + } |
| 47 | + return defaultExportNode; |
| 48 | + } |
| 49 | + |
| 50 | + function getExportMap(source, context) { |
| 51 | + const exportMap = ExportMapBuilder.get(source.value, context); |
| 52 | + if (exportMap == null) { |
| 53 | + return; |
| 54 | + } |
| 55 | + if (exportMap.errors.length > 0) { |
| 56 | + exportMap.reportErrors(context, source.value); |
| 57 | + return; |
| 58 | + } |
| 59 | + return exportMap; |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + ImportDeclaration(node) { |
| 64 | + const exportMap = getExportMap(node.source, context); |
| 65 | + if (exportMap == null) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const defaultExportNode = getDefaultExportNode(exportMap); |
| 70 | + if (defaultExportNode == null) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const defaultExportName = getDefaultExportName(defaultExportNode); |
| 75 | + const importTarget = node.source.value; |
| 76 | + const importBasename = path.basename(exportMap.path); |
| 77 | + |
| 78 | + node.specifiers.forEach((importClause) => { |
| 79 | + const importName = importClause.local.name; |
| 80 | + |
| 81 | + // No named default export |
| 82 | + if (defaultExportName === undefined) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // The name of the import matches the name of the default export. |
| 87 | + if (importName === defaultExportName) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + context.report({ |
| 92 | + node: importClause, |
| 93 | + message: `Caution: \`${importBasename}\` has a default export \`${defaultExportName}\`. This imports \`${defaultExportName}\` as \`${importName}\`. Check if you meant to write \`import ${defaultExportName} from '${importTarget}'\` instead.`, |
| 94 | + }); |
| 95 | + }); |
| 96 | + }, |
| 97 | + VariableDeclarator(node) { |
| 98 | + const options = context.options[0] || {}; |
| 99 | + |
| 100 | + if ( |
| 101 | + !options.commonjs |
| 102 | + || node.type !== 'VariableDeclarator' |
| 103 | + // return if it's not an object destructure or it's an empty object destructure |
| 104 | + || !node.id || node.id.type !== 'Identifier' |
| 105 | + // return if there is no call expression on the right side |
| 106 | + || !node.init || node.init.type !== 'CallExpression' |
| 107 | + ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const call = node.init; |
| 112 | + const [source] = call.arguments; |
| 113 | + |
| 114 | + if ( |
| 115 | + // return if it's not a commonjs require statement |
| 116 | + call.callee.type !== 'Identifier' || call.callee.name !== 'require' || call.arguments.length !== 1 |
| 117 | + // return if it's not a string source |
| 118 | + || source.type !== 'Literal' |
| 119 | + ) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const exportMap = getExportMap(source, context); |
| 124 | + if (exportMap == null) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + const defaultExportNode = getDefaultExportNode(exportMap); |
| 129 | + if (defaultExportNode == null) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + const defaultExportName = getDefaultExportName(defaultExportNode); |
| 134 | + const requireTarget = source.value; |
| 135 | + const requireBasename = path.basename(exportMap.path); |
| 136 | + const requireName = node.id.name; |
| 137 | + |
| 138 | + // No named default export |
| 139 | + if (defaultExportName === undefined) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + // The name of the require matches the name of the default export. |
| 144 | + if (requireName === defaultExportName) { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + context.report({ |
| 149 | + node, |
| 150 | + message: `Caution: \`${requireBasename}\` has a default export \`${defaultExportName}\`. This requires \`${defaultExportName}\` as \`${requireName}\`. Check if you meant to write \`const ${defaultExportName} = require('${requireTarget}')\` instead.`, |
| 151 | + }); |
| 152 | + }, |
| 153 | + }; |
| 154 | + }, |
| 155 | +}; |
| 156 | + |
| 157 | +module.exports = rule; |
0 commit comments