node-path.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createPath = createPath;
  6. var _debug = _interopRequireDefault(require("debug"));
  7. var _mamacro = require("mamacro");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _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); }
  10. var debug = (0, _debug.default)("webassemblyjs:ast:traverse");
  11. function findParent(_ref, cb) {
  12. var parentPath = _ref.parentPath;
  13. if (parentPath == null) {
  14. throw new Error("node is root");
  15. }
  16. var currentPath = parentPath;
  17. while (cb(currentPath) !== false) {
  18. // Hit the root node, stop
  19. // $FlowIgnore
  20. if (currentPath.parentPath == null) {
  21. return null;
  22. } // $FlowIgnore
  23. currentPath = currentPath.parentPath;
  24. }
  25. return currentPath.node;
  26. }
  27. function insertBefore(context, newNode) {
  28. return insert(context, newNode);
  29. }
  30. function insertAfter(context, newNode) {
  31. return insert(context, newNode, 1);
  32. }
  33. function insert(_ref2, newNode) {
  34. var node = _ref2.node,
  35. inList = _ref2.inList,
  36. parentPath = _ref2.parentPath,
  37. parentKey = _ref2.parentKey;
  38. var indexOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  39. if (!inList) {
  40. throw new Error('inList' + " error: " + ("insert can only be used for nodes that are within lists" || "unknown"));
  41. }
  42. if (!(parentPath != null)) {
  43. throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
  44. }
  45. // $FlowIgnore
  46. var parentList = parentPath.node[parentKey];
  47. var indexInList = parentList.findIndex(function (n) {
  48. return n === node;
  49. });
  50. parentList.splice(indexInList + indexOffset, 0, newNode);
  51. }
  52. function remove(_ref3) {
  53. var node = _ref3.node,
  54. parentKey = _ref3.parentKey,
  55. parentPath = _ref3.parentPath;
  56. if (!(parentPath != null)) {
  57. throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
  58. }
  59. // $FlowIgnore
  60. var parentNode = parentPath.node; // $FlowIgnore
  61. var parentProperty = parentNode[parentKey];
  62. if (Array.isArray(parentProperty)) {
  63. // $FlowIgnore
  64. parentNode[parentKey] = parentProperty.filter(function (n) {
  65. return n !== node;
  66. });
  67. } else {
  68. // $FlowIgnore
  69. delete parentNode[parentKey];
  70. }
  71. node._deleted = true;
  72. debug("delete path %s", node.type);
  73. }
  74. function stop(context) {
  75. context.shouldStop = true;
  76. }
  77. function replaceWith(context, newNode) {
  78. // $FlowIgnore
  79. var parentNode = context.parentPath.node; // $FlowIgnore
  80. var parentProperty = parentNode[context.parentKey];
  81. if (Array.isArray(parentProperty)) {
  82. var indexInList = parentProperty.findIndex(function (n) {
  83. return n === context.node;
  84. });
  85. parentProperty.splice(indexInList, 1, newNode);
  86. } else {
  87. // $FlowIgnore
  88. parentNode[context.parentKey] = newNode;
  89. }
  90. context.node._deleted = true;
  91. context.node = newNode;
  92. } // bind the context to the first argument of node operations
  93. function bindNodeOperations(operations, context) {
  94. var keys = Object.keys(operations);
  95. var boundOperations = {};
  96. keys.forEach(function (key) {
  97. boundOperations[key] = operations[key].bind(null, context);
  98. });
  99. return boundOperations;
  100. }
  101. function createPathOperations(context) {
  102. // $FlowIgnore
  103. return bindNodeOperations({
  104. findParent: findParent,
  105. replaceWith: replaceWith,
  106. remove: remove,
  107. insertBefore: insertBefore,
  108. insertAfter: insertAfter,
  109. stop: stop
  110. }, context);
  111. }
  112. function createPath(context) {
  113. var path = _extends({}, context);
  114. Object.assign(path, createPathOperations(path));
  115. return path;
  116. }