Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = hoistVariables;
|
---|
7 |
|
---|
8 | var _t = require("@babel/types");
|
---|
9 |
|
---|
10 | const {
|
---|
11 | assignmentExpression,
|
---|
12 | expressionStatement,
|
---|
13 | identifier
|
---|
14 | } = _t;
|
---|
15 | const visitor = {
|
---|
16 | Scope(path, state) {
|
---|
17 | if (state.kind === "let") path.skip();
|
---|
18 | },
|
---|
19 |
|
---|
20 | FunctionParent(path) {
|
---|
21 | path.skip();
|
---|
22 | },
|
---|
23 |
|
---|
24 | VariableDeclaration(path, state) {
|
---|
25 | if (state.kind && path.node.kind !== state.kind) return;
|
---|
26 | const nodes = [];
|
---|
27 | const declarations = path.get("declarations");
|
---|
28 | let firstId;
|
---|
29 |
|
---|
30 | for (const declar of declarations) {
|
---|
31 | firstId = declar.node.id;
|
---|
32 |
|
---|
33 | if (declar.node.init) {
|
---|
34 | nodes.push(expressionStatement(assignmentExpression("=", declar.node.id, declar.node.init)));
|
---|
35 | }
|
---|
36 |
|
---|
37 | for (const name of Object.keys(declar.getBindingIdentifiers())) {
|
---|
38 | state.emit(identifier(name), name, declar.node.init !== null);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (path.parentPath.isFor({
|
---|
43 | left: path.node
|
---|
44 | })) {
|
---|
45 | path.replaceWith(firstId);
|
---|
46 | } else {
|
---|
47 | path.replaceWithMultiple(nodes);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | };
|
---|
52 |
|
---|
53 | function hoistVariables(path, emit, kind = "var") {
|
---|
54 | path.traverse(visitor, {
|
---|
55 | kind,
|
---|
56 | emit
|
---|
57 | });
|
---|
58 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.