tdz.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.visitor = undefined;
  4. var _babelTypes = require("babel-types");
  5. var t = _interopRequireWildcard(_babelTypes);
  6. 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; } }
  7. function getTDZStatus(refPath, bindingPath) {
  8. var executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath);
  9. if (executionStatus === "before") {
  10. return "inside";
  11. } else if (executionStatus === "after") {
  12. return "outside";
  13. } else {
  14. return "maybe";
  15. }
  16. }
  17. function buildTDZAssert(node, file) {
  18. return t.callExpression(file.addHelper("temporalRef"), [node, t.stringLiteral(node.name), file.addHelper("temporalUndefined")]);
  19. }
  20. function isReference(node, scope, state) {
  21. var declared = state.letReferences[node.name];
  22. if (!declared) return false;
  23. return scope.getBindingIdentifier(node.name) === declared;
  24. }
  25. var visitor = exports.visitor = {
  26. ReferencedIdentifier: function ReferencedIdentifier(path, state) {
  27. if (!this.file.opts.tdz) return;
  28. var node = path.node,
  29. parent = path.parent,
  30. scope = path.scope;
  31. if (path.parentPath.isFor({ left: node })) return;
  32. if (!isReference(node, scope, state)) return;
  33. var bindingPath = scope.getBinding(node.name).path;
  34. var status = getTDZStatus(path, bindingPath);
  35. if (status === "inside") return;
  36. if (status === "maybe") {
  37. var assert = buildTDZAssert(node, state.file);
  38. bindingPath.parent._tdzThis = true;
  39. path.skip();
  40. if (path.parentPath.isUpdateExpression()) {
  41. if (parent._ignoreBlockScopingTDZ) return;
  42. path.parentPath.replaceWith(t.sequenceExpression([assert, parent]));
  43. } else {
  44. path.replaceWith(assert);
  45. }
  46. } else if (status === "outside") {
  47. path.replaceWith(t.throwStatement(t.inherits(t.newExpression(t.identifier("ReferenceError"), [t.stringLiteral(node.name + " is not defined - temporal dead zone")]), node)));
  48. }
  49. },
  50. AssignmentExpression: {
  51. exit: function exit(path, state) {
  52. if (!this.file.opts.tdz) return;
  53. var node = path.node;
  54. if (node._ignoreBlockScopingTDZ) return;
  55. var nodes = [];
  56. var ids = path.getBindingIdentifiers();
  57. for (var name in ids) {
  58. var id = ids[name];
  59. if (isReference(id, path.scope, state)) {
  60. nodes.push(buildTDZAssert(id, state.file));
  61. }
  62. }
  63. if (nodes.length) {
  64. node._ignoreBlockScopingTDZ = true;
  65. nodes.push(node);
  66. path.replaceWithMultiple(nodes.map(t.expressionStatement));
  67. }
  68. }
  69. }
  70. };