| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.annexB33FunctionsVisitor = void 0;
|
|---|
| 7 | exports.isVarScope = isVarScope;
|
|---|
| 8 | var _core = require("@babel/core");
|
|---|
| 9 | const annexB33FunctionsVisitor = exports.annexB33FunctionsVisitor = Object.assign({
|
|---|
| 10 | VariableDeclaration(path) {
|
|---|
| 11 | if (isStrict(path)) return;
|
|---|
| 12 | if (path.node.kind !== "var") return;
|
|---|
| 13 | const varScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
|---|
| 14 | varScope.path.traverse(functionsToVarVisitor, {
|
|---|
| 15 | names: Object.keys(path.getBindingIdentifiers())
|
|---|
| 16 | });
|
|---|
| 17 | }
|
|---|
| 18 | }, {
|
|---|
| 19 | BlockStatement(path) {
|
|---|
| 20 | if (isStrict(path)) return;
|
|---|
| 21 | if (_core.types.isFunction(path.parent, {
|
|---|
| 22 | body: path.node
|
|---|
| 23 | })) return;
|
|---|
| 24 | transformStatementList(path.get("body"));
|
|---|
| 25 | },
|
|---|
| 26 | SwitchCase(path) {
|
|---|
| 27 | if (isStrict(path)) return;
|
|---|
| 28 | transformStatementList(path.get("consequent"));
|
|---|
| 29 | }
|
|---|
| 30 | });
|
|---|
| 31 | function transformStatementList(paths) {
|
|---|
| 32 | outer: for (const path of paths) {
|
|---|
| 33 | if (!path.isFunctionDeclaration()) continue;
|
|---|
| 34 | if (path.node.async || path.node.generator) return;
|
|---|
| 35 | const {
|
|---|
| 36 | scope
|
|---|
| 37 | } = path.parentPath;
|
|---|
| 38 | if (isVarScope(scope)) return;
|
|---|
| 39 | const {
|
|---|
| 40 | name
|
|---|
| 41 | } = path.node.id;
|
|---|
| 42 | let currScope = scope;
|
|---|
| 43 | do {
|
|---|
| 44 | if (currScope.parent.hasOwnBinding(name)) continue outer;
|
|---|
| 45 | currScope = currScope.parent;
|
|---|
| 46 | } while (!isVarScope(currScope));
|
|---|
| 47 | maybeTransformBlockScopedFunction(path);
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 | function maybeTransformBlockScopedFunction(path) {
|
|---|
| 51 | const {
|
|---|
| 52 | node,
|
|---|
| 53 | parentPath: {
|
|---|
| 54 | scope
|
|---|
| 55 | }
|
|---|
| 56 | } = path;
|
|---|
| 57 | const {
|
|---|
| 58 | id
|
|---|
| 59 | } = node;
|
|---|
| 60 | scope.removeOwnBinding(id.name);
|
|---|
| 61 | node.id = null;
|
|---|
| 62 | const varNode = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(id, _core.types.toExpression(node))]);
|
|---|
| 63 | varNode._blockHoist = 2;
|
|---|
| 64 | const [varPath] = path.replaceWith(varNode);
|
|---|
| 65 | scope.registerDeclaration(varPath);
|
|---|
| 66 | }
|
|---|
| 67 | const functionsToVarVisitor = {
|
|---|
| 68 | Scope(path, {
|
|---|
| 69 | names
|
|---|
| 70 | }) {
|
|---|
| 71 | for (const name of names) {
|
|---|
| 72 | const binding = path.scope.getOwnBinding(name);
|
|---|
| 73 | if ((binding == null ? void 0 : binding.kind) === "hoisted") {
|
|---|
| 74 | maybeTransformBlockScopedFunction(binding.path);
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | },
|
|---|
| 78 | "Expression|Declaration"(path) {
|
|---|
| 79 | path.skip();
|
|---|
| 80 | }
|
|---|
| 81 | };
|
|---|
| 82 | function isVarScope(scope) {
|
|---|
| 83 | return scope.path.isFunctionParent() || scope.path.isProgram();
|
|---|
| 84 | }
|
|---|
| 85 | function isStrict(path) {
|
|---|
| 86 | return !!path.find(({
|
|---|
| 87 | node
|
|---|
| 88 | }) => {
|
|---|
| 89 | var _node$directives;
|
|---|
| 90 | if (_core.types.isProgram(node)) {
|
|---|
| 91 | if (node.sourceType === "module") return true;
|
|---|
| 92 | } else if (_core.types.isClass(node)) {
|
|---|
| 93 | return true;
|
|---|
| 94 | } else if (!_core.types.isBlockStatement(node)) {
|
|---|
| 95 | return false;
|
|---|
| 96 | }
|
|---|
| 97 | return (_node$directives = node.directives) == null ? void 0 : _node$directives.some(directive => directive.value.value === "use strict");
|
|---|
| 98 | });
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | //# sourceMappingURL=annex-B_3_3.js.map
|
|---|