|  | 
|  | 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 | +import includes from 'array-includes' | 
|  | 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 = includes(exportedIdentifiers, importAlias) | 
|  | 51 | + | 
|  | 52 | +  context.report({ | 
|  | 53 | +    node: node, | 
|  | 54 | +    message: `Default import from \`${packageName}\` should be bound to \`${mappings[packageName]}\`, not \`${importAlias}\``, | 
|  | 55 | +    fix: skipFixing ? null : fixImportOrRequire(specifierOrDeclaration, mappings[packageName]), | 
|  | 56 | +  }) | 
|  | 57 | + | 
|  | 58 | +  for (const variableReference of references) { | 
|  | 59 | +    if (specifierOrDeclaration.type === 'VariableDeclarator' && variableReference.init) { | 
|  | 60 | +      continue | 
|  | 61 | +    } | 
|  | 62 | + | 
|  | 63 | +    context.report({ | 
|  | 64 | +      node: variableReference.identifier, | 
|  | 65 | +      message: `Using incorrect binding name \`${variableReference.identifier.name}\` instead of \`${mappings[packageName]}\` for default import from package \`${packageName}\``, | 
|  | 66 | +      fix: fixer => { | 
|  | 67 | +        if (skipFixing) { | 
|  | 68 | +          return | 
|  | 69 | +        } | 
|  | 70 | + | 
|  | 71 | +        return fixer.replaceText(variableReference.identifier, mappings[packageName]) | 
|  | 72 | +      }, | 
|  | 73 | +    }) | 
|  | 74 | +  } | 
|  | 75 | +} | 
|  | 76 | + | 
|  | 77 | +function fixImportOrRequire(node, text) { | 
|  | 78 | +  return function(fixer) { | 
|  | 79 | +    let newAlias = text | 
|  | 80 | +    let nodeOrToken | 
|  | 81 | +    if (node.type === 'VariableDeclarator') { | 
|  | 82 | +      nodeOrToken = node.id | 
|  | 83 | +      newAlias = text | 
|  | 84 | +    } else { | 
|  | 85 | +      nodeOrToken = node | 
|  | 86 | +      if (node.imported && node.imported.name === 'default') { | 
|  | 87 | +        newAlias = `default as ${text}` | 
|  | 88 | +      } else { | 
|  | 89 | +        newAlias = text | 
|  | 90 | +      } | 
|  | 91 | +    } | 
|  | 92 | + | 
|  | 93 | +    return fixer.replaceText(nodeOrToken, newAlias) | 
|  | 94 | +  } | 
|  | 95 | +} | 
|  | 96 | + | 
|  | 97 | +module.exports = { | 
|  | 98 | +  meta: { | 
|  | 99 | +    type: 'suggestion', | 
|  | 100 | +    docs: { | 
|  | 101 | +      url: docsUrl('rename-default-import'), | 
|  | 102 | +      recommended: false, | 
|  | 103 | +    }, | 
|  | 104 | +    fixable: 'code', | 
|  | 105 | +    schema: [ | 
|  | 106 | +      { | 
|  | 107 | +        type: 'object', | 
|  | 108 | +        minProperties: 1, | 
|  | 109 | +        additionalProperties: { | 
|  | 110 | +          type: 'string', | 
|  | 111 | +        }, | 
|  | 112 | +      }, | 
|  | 113 | +    ], | 
|  | 114 | +  }, | 
|  | 115 | +  create: function(context) { | 
|  | 116 | +    const exportedIdentifiers = [] | 
|  | 117 | +    return { | 
|  | 118 | +      'Program': function(programNode) { | 
|  | 119 | +        const {body} = programNode | 
|  | 120 | + | 
|  | 121 | +        body.forEach((node) => { | 
|  | 122 | +          if (node.type === 'ExportNamedDeclaration') { | 
|  | 123 | +            node.specifiers.forEach((specifier) => { | 
|  | 124 | +              const {exported: {name}} = specifier | 
|  | 125 | +              if (!includes(exportedIdentifiers, name)) { | 
|  | 126 | +                exportedIdentifiers.push(name) | 
|  | 127 | +              } | 
|  | 128 | +            }) | 
|  | 129 | +          } | 
|  | 130 | +        }) | 
|  | 131 | +      }, | 
|  | 132 | +      'ImportDeclaration:exit': function(node) { | 
|  | 133 | +        const {source, specifiers} = node | 
|  | 134 | +        const {options} = context | 
|  | 135 | + | 
|  | 136 | +        if (options.length === 0) { | 
|  | 137 | +          return | 
|  | 138 | +        } | 
|  | 139 | + | 
|  | 140 | +        for (const specifier of specifiers) { | 
|  | 141 | +          if (!isDefaultImport(specifier)) { | 
|  | 142 | +            continue | 
|  | 143 | +          } | 
|  | 144 | + | 
|  | 145 | +          handleImport( | 
|  | 146 | +            context, | 
|  | 147 | +            source, | 
|  | 148 | +            specifier, | 
|  | 149 | +            source.value, | 
|  | 150 | +            specifier.local.name, | 
|  | 151 | +            exportedIdentifiers | 
|  | 152 | +          ) | 
|  | 153 | +        } | 
|  | 154 | +      }, | 
|  | 155 | +      'VariableDeclaration:exit': function(node) { | 
|  | 156 | +        const {declarations} = node | 
|  | 157 | +        const {options} = context | 
|  | 158 | + | 
|  | 159 | +        if (options.length === 0) { | 
|  | 160 | +          return | 
|  | 161 | +        } | 
|  | 162 | + | 
|  | 163 | +        for (const declaration of declarations) { | 
|  | 164 | +          if (!isCommonJSImport(declaration) || context.getScope(declaration).type !== 'module') { | 
|  | 165 | +            continue | 
|  | 166 | +          } | 
|  | 167 | + | 
|  | 168 | +          handleImport( | 
|  | 169 | +            context, | 
|  | 170 | +            node, | 
|  | 171 | +            declaration, | 
|  | 172 | +            declaration.init.arguments[0].value, | 
|  | 173 | +            declaration.id.name, | 
|  | 174 | +            exportedIdentifiers | 
|  | 175 | +          ) | 
|  | 176 | +        } | 
|  | 177 | +      }, | 
|  | 178 | +    } | 
|  | 179 | +  }, | 
|  | 180 | +} | 
0 commit comments