All files / src index.js

100% Statements 30/30
95.45% Branches 21/22
100% Functions 5/5
100% Lines 30/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82  2x 2x 2x 2x       5x 5x       1x   4x 4x 4x 5x       6x 6x 4x   2x 2x 2x 2x 2x 1x           7x 20x 20x         17x   3x 3x   3x 1x     3x                                     6x                  
export default function(babel) {
  const {types: t} = babel
  const identifiers = new Set()
  const declarators = new Set()
  return {
    name: 'babel-plugin-glamorous-displayName',
    visitor: {
      ImportDeclaration(path) {
        const defaultSpecifierPath = path.get('specifiers')[0]
        if (
          path.node.source.value !== 'glamorous' ||
          !t.isImportDefaultSpecifier(defaultSpecifierPath)
        ) {
          return
        }
        const {node: {local: {name}}} = defaultSpecifierPath
        const {referencePaths} = path.scope.getBinding(name)
        referencePaths.forEach(reference => {
          identifiers.add(reference)
        })
      },
      VariableDeclarator(path) {
        const {node} = path
        if (!isRequireCall(node.init) || !t.isIdentifier(node.id)) {
          return
        }
        const {id: {name}} = node
        const binding = path.scope.getBinding(name)
        Eif (binding) {
          const {referencePaths} = binding
          referencePaths.forEach(reference => {
            identifiers.add(reference)
          })
        }
      },
      Program: {
        exit(path) {
          Array.from(identifiers).forEach(identifier => {
            let declarator = identifier.findParent(t.isVariableDeclarator)
            if (
              !declarator ||
              declarators.has(declarator) ||
              !t.isCallExpression(declarator.node.init)
            ) {
              return
            }
            declarators.add(declarator)
            const {node: {id: {name: displayName}}} = declarator
 
            if (declarator.parentPath.findParent(t.isExportNamedDeclaration)) {
              declarator = declarator.parentPath
            }
 
            declarator.parentPath.insertAfter(
              t.expressionStatement(
                t.assignmentExpression(
                  '=',
                  t.memberExpression(
                    t.identifier(displayName),
                    t.identifier('displayName')
                  ),
                  t.stringLiteral(displayName)
                )
              )
            )
          })
        }
      }
    }
  }
 
  function isRequireCall(callExpression) {
    return (
      callExpression &&
      callExpression.type === 'CallExpression' &&
      callExpression.callee.name === 'require' &&
      callExpression.arguments.length === 1 &&
      callExpression.arguments[0].value === 'glamorous'
    )
  }
}