1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = hoistVariables;
|
7 |
|
8 | var t = _interopRequireWildcard(require("@babel/types"));
|
9 |
|
10 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
11 |
|
12 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
13 |
|
14 | const visitor = {
|
15 | Scope(path, state) {
|
16 | if (state.kind === "let") path.skip();
|
17 | },
|
18 |
|
19 | Function(path) {
|
20 | path.skip();
|
21 | },
|
22 |
|
23 | VariableDeclaration(path, state) {
|
24 | if (state.kind && path.node.kind !== state.kind) return;
|
25 | const nodes = [];
|
26 | const declarations = path.get("declarations");
|
27 | let firstId;
|
28 |
|
29 | for (const declar of declarations) {
|
30 | firstId = declar.node.id;
|
31 |
|
32 | if (declar.node.init) {
|
33 | nodes.push(t.expressionStatement(t.assignmentExpression("=", declar.node.id, declar.node.init)));
|
34 | }
|
35 |
|
36 | for (const name of Object.keys(declar.getBindingIdentifiers())) {
|
37 | state.emit(t.identifier(name), name, declar.node.init !== null);
|
38 | }
|
39 | }
|
40 |
|
41 | if (path.parentPath.isFor({
|
42 | left: path.node
|
43 | })) {
|
44 | path.replaceWith(firstId);
|
45 | } else {
|
46 | path.replaceWithMultiple(nodes);
|
47 | }
|
48 | }
|
49 |
|
50 | };
|
51 |
|
52 | function hoistVariables(path, emit, kind = "var") {
|
53 | path.traverse(visitor, {
|
54 | kind,
|
55 | emit
|
56 | });
|
57 | } |
\ | No newline at end of file |