All files / dist index.js

100% Statements 39/39
92% Branches 23/25
100% Functions 9/9
100% Lines 39/39
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 83    1x       1x   1x   1x   1x   1x 2x   2x 2x 2x       4x 4x 1x   3x   3x 3x   3x 4x       5x   5x 3x   2x   2x 2x 2x   2x 1x             6x 14x 14x 11x   3x 3x 3x     3x 1x     3x               5x       2x
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _from = require('babel-runtime/core-js/array/from');
 
var _from2 = _interopRequireDefault(_from);
 
var _set = require('babel-runtime/core-js/set');
 
var _set2 = _interopRequireDefault(_set);
 
exports.default = function (babel) {
  var t = babel.types;
 
  var identifiers = new _set2.default();
  var declarators = new _set2.default();
  return {
    name: 'babel-plugin-glamorous-displayName',
    visitor: {
      ImportDeclaration: function ImportDeclaration(path) {
        var defaultSpecifierPath = path.get('specifiers')[0];
        if (path.node.source.value !== 'glamorous' || !t.isImportDefaultSpecifier(defaultSpecifierPath)) {
          return;
        }
        var name = defaultSpecifierPath.node.local.name;
 
        var _path$scope$getBindin = path.scope.getBinding(name),
            referencePaths = _path$scope$getBindin.referencePaths;
 
        referencePaths.forEach(function (reference) {
          identifiers.add(reference);
        });
      },
      VariableDeclarator: function VariableDeclarator(path) {
        var node = path.node;
 
        if (!isRequireCall(node.init) || !t.isIdentifier(node.id)) {
          return;
        }
        var name = node.id.name;
 
        var binding = path.scope.getBinding(name);
        Eif (binding) {
          var referencePaths = binding.referencePaths;
 
          referencePaths.forEach(function (reference) {
            identifiers.add(reference);
          });
        }
      },
 
      Program: {
        exit: function exit(path) {
          (0, _from2.default)(identifiers).forEach(function (identifier) {
            var declarator = identifier.findParent(t.isVariableDeclarator);
            if (!declarator || declarators.has(declarator)) {
              return;
            }
            declarators.add(declarator);
            var _declarator = declarator,
                displayName = _declarator.node.id.name;
 
 
            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';
  }
};
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }