index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (node, nodes, file, scope, allowedSingleIdent) {
  4. var obj = void 0;
  5. if (t.isIdentifier(node) && allowedSingleIdent) {
  6. obj = node;
  7. } else {
  8. obj = getObjRef(node, nodes, file, scope);
  9. }
  10. var ref = void 0,
  11. uid = void 0;
  12. if (t.isIdentifier(node)) {
  13. ref = node;
  14. uid = obj;
  15. } else {
  16. var prop = getPropRef(node, nodes, file, scope);
  17. var computed = node.computed || t.isLiteral(prop);
  18. uid = ref = t.memberExpression(obj, prop, computed);
  19. }
  20. return {
  21. uid: uid,
  22. ref: ref
  23. };
  24. };
  25. var _babelTypes = require("babel-types");
  26. var t = _interopRequireWildcard(_babelTypes);
  27. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  28. function getObjRef(node, nodes, file, scope) {
  29. var ref = void 0;
  30. if (t.isSuper(node)) {
  31. return node;
  32. } else if (t.isIdentifier(node)) {
  33. if (scope.hasBinding(node.name)) {
  34. return node;
  35. } else {
  36. ref = node;
  37. }
  38. } else if (t.isMemberExpression(node)) {
  39. ref = node.object;
  40. if (t.isSuper(ref) || t.isIdentifier(ref) && scope.hasBinding(ref.name)) {
  41. return ref;
  42. }
  43. } else {
  44. throw new Error("We can't explode this node type " + node.type);
  45. }
  46. var temp = scope.generateUidIdentifierBasedOnNode(ref);
  47. nodes.push(t.variableDeclaration("var", [t.variableDeclarator(temp, ref)]));
  48. return temp;
  49. }
  50. function getPropRef(node, nodes, file, scope) {
  51. var prop = node.property;
  52. var key = t.toComputedKey(node, prop);
  53. if (t.isLiteral(key) && t.isPureish(key)) return key;
  54. var temp = scope.generateUidIdentifierBasedOnNode(prop);
  55. nodes.push(t.variableDeclaration("var", [t.variableDeclarator(temp, prop)]));
  56. return temp;
  57. }
  58. module.exports = exports["default"];