aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@babel/plugin-transform-block-scoping/lib/tdz.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/@babel/plugin-transform-block-scoping/lib/tdz.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/@babel/plugin-transform-block-scoping/lib/tdz.js')
-rw-r--r--node_modules/@babel/plugin-transform-block-scoping/lib/tdz.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/node_modules/@babel/plugin-transform-block-scoping/lib/tdz.js b/node_modules/@babel/plugin-transform-block-scoping/lib/tdz.js
new file mode 100644
index 00000000..df5b4d41
--- /dev/null
+++ b/node_modules/@babel/plugin-transform-block-scoping/lib/tdz.js
@@ -0,0 +1,96 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+exports.visitor = void 0;
+
+function _core() {
+ const data = require("@babel/core");
+
+ _core = function _core() {
+ return data;
+ };
+
+ return data;
+}
+
+function getTDZStatus(refPath, bindingPath) {
+ const executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath);
+
+ if (executionStatus === "before") {
+ return "inside";
+ } else if (executionStatus === "after") {
+ return "outside";
+ } else {
+ return "maybe";
+ }
+}
+
+function buildTDZAssert(node, state) {
+ return _core().types.callExpression(state.addHelper("temporalRef"), [node, _core().types.stringLiteral(node.name)]);
+}
+
+function isReference(node, scope, state) {
+ const declared = state.letReferences[node.name];
+ if (!declared) return false;
+ return scope.getBindingIdentifier(node.name) === declared;
+}
+
+const visitor = {
+ ReferencedIdentifier(path, state) {
+ if (!state.tdzEnabled) return;
+ const node = path.node,
+ parent = path.parent,
+ scope = path.scope;
+ if (path.parentPath.isFor({
+ left: node
+ })) return;
+ if (!isReference(node, scope, state)) return;
+ const bindingPath = scope.getBinding(node.name).path;
+ if (bindingPath.isFunctionDeclaration()) return;
+ const status = getTDZStatus(path, bindingPath);
+ if (status === "inside") return;
+
+ if (status === "maybe") {
+ const assert = buildTDZAssert(node, state);
+ bindingPath.parent._tdzThis = true;
+ path.skip();
+
+ if (path.parentPath.isUpdateExpression()) {
+ if (parent._ignoreBlockScopingTDZ) return;
+ path.parentPath.replaceWith(_core().types.sequenceExpression([assert, parent]));
+ } else {
+ path.replaceWith(assert);
+ }
+ } else if (status === "outside") {
+ path.replaceWith(_core().types.throwStatement(_core().types.inherits(_core().types.newExpression(_core().types.identifier("ReferenceError"), [_core().types.stringLiteral(`${node.name} is not defined - temporal dead zone`)]), node)));
+ }
+ },
+
+ AssignmentExpression: {
+ exit(path, state) {
+ if (!state.tdzEnabled) return;
+ const node = path.node;
+ if (node._ignoreBlockScopingTDZ) return;
+ const nodes = [];
+ const ids = path.getBindingIdentifiers();
+
+ for (const name in ids) {
+ const id = ids[name];
+
+ if (isReference(id, path.scope, state)) {
+ nodes.push(buildTDZAssert(id, state));
+ }
+ }
+
+ if (nodes.length) {
+ node._ignoreBlockScopingTDZ = true;
+ nodes.push(node);
+ path.replaceWithMultiple(nodes.map(_core().types.expressionStatement));
+ }
+ }
+
+ }
+};
+exports.visitor = visitor; \ No newline at end of file