index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*istanbul ignore next*/"use strict";
  2. exports.__esModule = true;
  3. exports.default = function (node, nodes, file, scope, allowedSingleIdent) {
  4. var obj = /*istanbul ignore next*/void 0;
  5. if (t.isIdentifier(node) && allowedSingleIdent) {
  6. obj = node;
  7. } else {
  8. obj = getObjRef(node, nodes, file, scope);
  9. }
  10. var ref = /*istanbul ignore next*/void 0,
  11. uid = /*istanbul ignore next*/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 /*istanbul ignore next*/_babelTypes = require("babel-types");
  26. /*istanbul ignore next*/
  27. var t = _interopRequireWildcard(_babelTypes);
  28. /*istanbul ignore next*/
  29. 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; } }
  30. function getObjRef(node, nodes, file, scope) {
  31. var ref = /*istanbul ignore next*/void 0;
  32. if (t.isIdentifier(node)) {
  33. if (scope.hasBinding(node.name)) {
  34. // this variable is declared in scope so we can be 100% sure
  35. // that evaluating it multiple times wont trigger a getter
  36. // or something else
  37. return node;
  38. } else {
  39. // could possibly trigger a getter so we need to only evaluate
  40. // it once
  41. ref = node;
  42. }
  43. } else if (t.isMemberExpression(node)) {
  44. ref = node.object;
  45. if (t.isIdentifier(ref) && scope.hasBinding(ref.name)) {
  46. // the object reference that we need to save is locally declared
  47. // so as per the previous comment we can be 100% sure evaluating
  48. // it multiple times will be safe
  49. return ref;
  50. }
  51. } else {
  52. throw new Error( /*istanbul ignore next*/"We can't explode this node type " + node.type);
  53. }
  54. var temp = scope.generateUidIdentifierBasedOnNode(ref);
  55. nodes.push(t.variableDeclaration("var", [t.variableDeclarator(temp, ref)]));
  56. return temp;
  57. }
  58. function getPropRef(node, nodes, file, scope) {
  59. var prop = node.property;
  60. var key = t.toComputedKey(node, prop);
  61. if (t.isLiteral(key)) return key;
  62. var temp = scope.generateUidIdentifierBasedOnNode(prop);
  63. nodes.push(t.variableDeclaration("var", [t.variableDeclarator(temp, prop)]));
  64. return temp;
  65. }
  66. /*istanbul ignore next*/module.exports = exports["default"];