|
| 1 | +/** |
| 2 | + * @fileoverview Rule to enforce aliases for default imports |
| 3 | + * @author Michał Kołodziejski |
| 4 | + */ |
| 5 | + |
| 6 | +import docsUrl from '../docsUrl' |
| 7 | +import has from 'has' |
| 8 | + |
| 9 | + |
| 10 | +function isDefaultImport(specifier) { |
| 11 | + if (specifier.type === 'ImportDefaultSpecifier') { |
| 12 | + return true |
| 13 | + } |
| 14 | + if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'default') { |
| 15 | + return true |
| 16 | + } |
| 17 | + return false |
| 18 | +} |
| 19 | + |
| 20 | +function isCommonJSImport(declaration) { |
| 21 | + const variableInit = declaration.init |
| 22 | + if (variableInit.type === 'CallExpression') { |
| 23 | + return variableInit.callee.name === 'require' |
| 24 | + } |
| 25 | + return false |
| 26 | +} |
| 27 | + |
| 28 | +function handleImport( |
| 29 | + context, |
| 30 | + node, |
| 31 | + specifierOrDeclaration, |
| 32 | + packageName, |
| 33 | + importAlias, |
| 34 | + exportedIdentifiers |
| 35 | +) { |
| 36 | + const mappings = context.options[0] || {} |
| 37 | + |
| 38 | + if (!has(mappings, packageName) || mappings[packageName] === importAlias) { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + let declaredVariables |
| 43 | + if (specifierOrDeclaration.type === 'VariableDeclarator') { |
| 44 | + declaredVariables = context.getDeclaredVariables(specifierOrDeclaration.parent)[0] |
| 45 | + } else { |
| 46 | + declaredVariables = context.getDeclaredVariables(specifierOrDeclaration)[0] |
| 47 | + } |
| 48 | + |
| 49 | + const references = declaredVariables ? declaredVariables.references : [] |
| 50 | + const skipFixing = exportedIdentifiers.indexOf(importAlias) !== -1 |
| 51 | + |
| 52 | + context.report({ |
| 53 | + node: node, |
| 54 | + message: `Default import from '${packageName}' should be aliased to ` |
| 55 | + + `${mappings[packageName]}, not ${importAlias}`, |
| 56 | + fix: skipFixing ? null : fixImportOrRequire(specifierOrDeclaration, mappings[packageName]), |
| 57 | + }) |
| 58 | + |
| 59 | + for (const variableReference of references) { |
| 60 | + if (specifierOrDeclaration.type === 'VariableDeclarator' && variableReference.init) { |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + context.report({ |
| 65 | + node: variableReference.identifier, |
| 66 | + message: `Using incorrect binding name '${variableReference.identifier.name}' ` |
| 67 | + + `instead of ${mappings[packageName]} for ` |
| 68 | + + `default import from package ${packageName}`, |
| 69 | + fix: fixer => { |
| 70 | + if (skipFixing) { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + return fixer.replaceText(variableReference.identifier, mappings[packageName]) |
| 75 | + }, |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function fixImportOrRequire(node, text) { |
| 81 | + return function(fixer) { |
| 82 | + let newAlias = text |
| 83 | + let nodeOrToken |
| 84 | + if (node.type === 'VariableDeclarator') { |
| 85 | + nodeOrToken = node.id |
| 86 | + newAlias = text |
| 87 | + } else { |
| 88 | + nodeOrToken = node |
| 89 | + if (node.imported && node.imported.name === 'default') { |
| 90 | + newAlias = `default as ${text}` |
| 91 | + } else { |
| 92 | + newAlias = text |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return fixer.replaceText(nodeOrToken, newAlias) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + meta: { |
| 102 | + type: 'suggestion', |
| 103 | + docs: { |
| 104 | + url: docsUrl('rename-default-import'), |
| 105 | + recommended: false, |
| 106 | + }, |
| 107 | + fixable: 'code', |
| 108 | + schema: [ |
| 109 | + { |
| 110 | + type: 'object', |
| 111 | + minProperties: 1, |
| 112 | + additionalProperties: { |
| 113 | + type: 'string', |
| 114 | + }, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + create: function(context) { |
| 119 | + const exportedIdentifiers = [] |
| 120 | + return { |
| 121 | + 'Program': function(programNode) { |
| 122 | + const {body} = programNode |
| 123 | + |
| 124 | + body.forEach((node) => { |
| 125 | + if (node.type === 'ExportNamedDeclaration') { |
| 126 | + node.specifiers.forEach((specifier) => { |
| 127 | + const {exported: {name}} = specifier |
| 128 | + if (exportedIdentifiers.indexOf(name) === -1) { |
| 129 | + exportedIdentifiers.push(name) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | + }) |
| 134 | + }, |
| 135 | + 'ImportDeclaration:exit': function(node) { |
| 136 | + const {source, specifiers} = node |
| 137 | + const {options} = context |
| 138 | + |
| 139 | + if (options.length === 0) { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + for (const specifier of specifiers) { |
| 144 | + if (!isDefaultImport(specifier)) { |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + handleImport( |
| 149 | + context, |
| 150 | + source, |
| 151 | + specifier, |
| 152 | + source.value, |
| 153 | + specifier.local.name, |
| 154 | + exportedIdentifiers |
| 155 | + ) |
| 156 | + } |
| 157 | + }, |
| 158 | + 'VariableDeclaration:exit': function(node) { |
| 159 | + const {declarations} = node |
| 160 | + const {options} = context |
| 161 | + |
| 162 | + if (options.length === 0) { |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + for (const declaration of declarations) { |
| 167 | + if (!isCommonJSImport(declaration) || context.getScope(declaration).type !== 'module') { |
| 168 | + continue |
| 169 | + } |
| 170 | + |
| 171 | + handleImport( |
| 172 | + context, |
| 173 | + node, |
| 174 | + declaration, |
| 175 | + declaration.init.arguments[0].value, |
| 176 | + declaration.id.name, |
| 177 | + exportedIdentifiers |
| 178 | + ) |
| 179 | + } |
| 180 | + }, |
| 181 | + } |
| 182 | + }, |
| 183 | +} |
0 commit comments