source: trip-planner-front/node_modules/@webassemblyjs/ast/lib/node-path.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createPath = createPath;
7
8function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
9
10function findParent(_ref, cb) {
11 var parentPath = _ref.parentPath;
12
13 if (parentPath == null) {
14 throw new Error("node is root");
15 }
16
17 var currentPath = parentPath;
18
19 while (cb(currentPath) !== false) {
20 // Hit the root node, stop
21 // $FlowIgnore
22 if (currentPath.parentPath == null) {
23 return null;
24 } // $FlowIgnore
25
26
27 currentPath = currentPath.parentPath;
28 }
29
30 return currentPath.node;
31}
32
33function insertBefore(context, newNode) {
34 return insert(context, newNode);
35}
36
37function insertAfter(context, newNode) {
38 return insert(context, newNode, 1);
39}
40
41function insert(_ref2, newNode) {
42 var node = _ref2.node,
43 inList = _ref2.inList,
44 parentPath = _ref2.parentPath,
45 parentKey = _ref2.parentKey;
46 var indexOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
47
48 if (!inList) {
49 throw new Error('inList' + " error: " + ("insert can only be used for nodes that are within lists" || "unknown"));
50 }
51
52 if (!(parentPath != null)) {
53 throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
54 }
55
56 // $FlowIgnore
57 var parentList = parentPath.node[parentKey];
58 var indexInList = parentList.findIndex(function (n) {
59 return n === node;
60 });
61 parentList.splice(indexInList + indexOffset, 0, newNode);
62}
63
64function remove(_ref3) {
65 var node = _ref3.node,
66 parentKey = _ref3.parentKey,
67 parentPath = _ref3.parentPath;
68
69 if (!(parentPath != null)) {
70 throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
71 }
72
73 // $FlowIgnore
74 var parentNode = parentPath.node; // $FlowIgnore
75
76 var parentProperty = parentNode[parentKey];
77
78 if (Array.isArray(parentProperty)) {
79 // $FlowIgnore
80 parentNode[parentKey] = parentProperty.filter(function (n) {
81 return n !== node;
82 });
83 } else {
84 // $FlowIgnore
85 delete parentNode[parentKey];
86 }
87
88 node._deleted = true;
89}
90
91function stop(context) {
92 context.shouldStop = true;
93}
94
95function replaceWith(context, newNode) {
96 // $FlowIgnore
97 var parentNode = context.parentPath.node; // $FlowIgnore
98
99 var parentProperty = parentNode[context.parentKey];
100
101 if (Array.isArray(parentProperty)) {
102 var indexInList = parentProperty.findIndex(function (n) {
103 return n === context.node;
104 });
105 parentProperty.splice(indexInList, 1, newNode);
106 } else {
107 // $FlowIgnore
108 parentNode[context.parentKey] = newNode;
109 }
110
111 context.node._deleted = true;
112 context.node = newNode;
113} // bind the context to the first argument of node operations
114
115
116function bindNodeOperations(operations, context) {
117 var keys = Object.keys(operations);
118 var boundOperations = {};
119 keys.forEach(function (key) {
120 boundOperations[key] = operations[key].bind(null, context);
121 });
122 return boundOperations;
123}
124
125function createPathOperations(context) {
126 // $FlowIgnore
127 return bindNodeOperations({
128 findParent: findParent,
129 replaceWith: replaceWith,
130 remove: remove,
131 insertBefore: insertBefore,
132 insertAfter: insertAfter,
133 stop: stop
134 }, context);
135}
136
137function createPath(context) {
138 var path = _extends({}, context); // $FlowIgnore
139
140
141 Object.assign(path, createPathOperations(path)); // $FlowIgnore
142
143 return path;
144}
Note: See TracBrowser for help on using the repository browser.