1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.visitor = void 0;
|
7 |
|
8 | var _core = require("@babel/core");
|
9 |
|
10 | function getTDZStatus(refPath, bindingPath) {
|
11 | const executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath);
|
12 |
|
13 | if (executionStatus === "before") {
|
14 | return "outside";
|
15 | } else if (executionStatus === "after") {
|
16 | return "inside";
|
17 | } else {
|
18 | return "maybe";
|
19 | }
|
20 | }
|
21 |
|
22 | function buildTDZAssert(node, state) {
|
23 | return _core.types.callExpression(state.addHelper("temporalRef"), [node, _core.types.stringLiteral(node.name)]);
|
24 | }
|
25 |
|
26 | function isReference(node, scope, state) {
|
27 | const declared = state.letReferences.get(node.name);
|
28 | if (!declared) return false;
|
29 | return scope.getBindingIdentifier(node.name) === declared;
|
30 | }
|
31 |
|
32 | const visitedMaybeTDZNodes = new WeakSet();
|
33 | const visitor = {
|
34 | ReferencedIdentifier(path, state) {
|
35 | if (!state.tdzEnabled) return;
|
36 | const {
|
37 | node,
|
38 | parent,
|
39 | scope
|
40 | } = path;
|
41 | if (path.parentPath.isFor({
|
42 | left: node
|
43 | })) return;
|
44 | if (!isReference(node, scope, state)) return;
|
45 | const bindingPath = scope.getBinding(node.name).path;
|
46 | if (bindingPath.isFunctionDeclaration()) return;
|
47 | const status = getTDZStatus(path, bindingPath);
|
48 | if (status === "outside") return;
|
49 |
|
50 | if (status === "maybe") {
|
51 | if (visitedMaybeTDZNodes.has(node)) {
|
52 | return;
|
53 | }
|
54 |
|
55 | visitedMaybeTDZNodes.add(node);
|
56 | const assert = buildTDZAssert(node, state);
|
57 | bindingPath.parent._tdzThis = true;
|
58 |
|
59 | if (path.parentPath.isUpdateExpression()) {
|
60 | if (parent._ignoreBlockScopingTDZ) return;
|
61 | path.parentPath.replaceWith(_core.types.sequenceExpression([assert, parent]));
|
62 | } else {
|
63 | path.replaceWith(assert);
|
64 | }
|
65 | } else if (status === "inside") {
|
66 | path.replaceWith(_core.template.ast`${state.addHelper("tdz")}("${node.name}")`);
|
67 | }
|
68 | },
|
69 |
|
70 | AssignmentExpression: {
|
71 | exit(path, state) {
|
72 | if (!state.tdzEnabled) return;
|
73 | const {
|
74 | node
|
75 | } = path;
|
76 | if (node._ignoreBlockScopingTDZ) return;
|
77 | const nodes = [];
|
78 | const ids = path.getBindingIdentifiers();
|
79 |
|
80 | for (const name of Object.keys(ids)) {
|
81 | const id = ids[name];
|
82 |
|
83 | if (isReference(id, path.scope, state)) {
|
84 | nodes.push(id);
|
85 | }
|
86 | }
|
87 |
|
88 | if (nodes.length) {
|
89 | node._ignoreBlockScopingTDZ = true;
|
90 | nodes.push(node);
|
91 | path.replaceWithMultiple(nodes.map(n => _core.types.expressionStatement(n)));
|
92 | }
|
93 | }
|
94 |
|
95 | }
|
96 | };
|
97 | exports.visitor = visitor; |
\ | No newline at end of file |