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