family.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. "use strict";
  2. exports.__esModule = true;
  3. var _create = require("babel-runtime/core-js/object/create");
  4. var _create2 = _interopRequireDefault(_create);
  5. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  6. var _getIterator3 = _interopRequireDefault(_getIterator2);
  7. exports.getStatementParent = getStatementParent;
  8. exports.getOpposite = getOpposite;
  9. exports.getCompletionRecords = getCompletionRecords;
  10. exports.getSibling = getSibling;
  11. exports.getPrevSibling = getPrevSibling;
  12. exports.getNextSibling = getNextSibling;
  13. exports.getAllNextSiblings = getAllNextSiblings;
  14. exports.getAllPrevSiblings = getAllPrevSiblings;
  15. exports.get = get;
  16. exports._getKey = _getKey;
  17. exports._getPattern = _getPattern;
  18. exports.getBindingIdentifiers = getBindingIdentifiers;
  19. exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
  20. exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
  21. exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
  22. var _index = require("./index");
  23. var _index2 = _interopRequireDefault(_index);
  24. var _babelTypes = require("babel-types");
  25. var t = _interopRequireWildcard(_babelTypes);
  26. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. function getStatementParent() {
  29. var path = this;
  30. do {
  31. if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
  32. break;
  33. } else {
  34. path = path.parentPath;
  35. }
  36. } while (path);
  37. if (path && (path.isProgram() || path.isFile())) {
  38. throw new Error("File/Program node, we can't possibly find a statement parent to this");
  39. }
  40. return path;
  41. }
  42. function getOpposite() {
  43. if (this.key === "left") {
  44. return this.getSibling("right");
  45. } else if (this.key === "right") {
  46. return this.getSibling("left");
  47. }
  48. }
  49. function getCompletionRecords() {
  50. var paths = [];
  51. var add = function add(path) {
  52. if (path) paths = paths.concat(path.getCompletionRecords());
  53. };
  54. if (this.isIfStatement()) {
  55. add(this.get("consequent"));
  56. add(this.get("alternate"));
  57. } else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
  58. add(this.get("body"));
  59. } else if (this.isProgram() || this.isBlockStatement()) {
  60. add(this.get("body").pop());
  61. } else if (this.isFunction()) {
  62. return this.get("body").getCompletionRecords();
  63. } else if (this.isTryStatement()) {
  64. add(this.get("block"));
  65. add(this.get("handler"));
  66. add(this.get("finalizer"));
  67. } else {
  68. paths.push(this);
  69. }
  70. return paths;
  71. }
  72. function getSibling(key) {
  73. return _index2.default.get({
  74. parentPath: this.parentPath,
  75. parent: this.parent,
  76. container: this.container,
  77. listKey: this.listKey,
  78. key: key
  79. });
  80. }
  81. function getPrevSibling() {
  82. return this.getSibling(this.key - 1);
  83. }
  84. function getNextSibling() {
  85. return this.getSibling(this.key + 1);
  86. }
  87. function getAllNextSiblings() {
  88. var _key = this.key;
  89. var sibling = this.getSibling(++_key);
  90. var siblings = [];
  91. while (sibling.node) {
  92. siblings.push(sibling);
  93. sibling = this.getSibling(++_key);
  94. }
  95. return siblings;
  96. }
  97. function getAllPrevSiblings() {
  98. var _key = this.key;
  99. var sibling = this.getSibling(--_key);
  100. var siblings = [];
  101. while (sibling.node) {
  102. siblings.push(sibling);
  103. sibling = this.getSibling(--_key);
  104. }
  105. return siblings;
  106. }
  107. function get(key, context) {
  108. if (context === true) context = this.context;
  109. var parts = key.split(".");
  110. if (parts.length === 1) {
  111. return this._getKey(key, context);
  112. } else {
  113. return this._getPattern(parts, context);
  114. }
  115. }
  116. function _getKey(key, context) {
  117. var _this = this;
  118. var node = this.node;
  119. var container = node[key];
  120. if (Array.isArray(container)) {
  121. return container.map(function (_, i) {
  122. return _index2.default.get({
  123. listKey: key,
  124. parentPath: _this,
  125. parent: node,
  126. container: container,
  127. key: i
  128. }).setContext(context);
  129. });
  130. } else {
  131. return _index2.default.get({
  132. parentPath: this,
  133. parent: node,
  134. container: node,
  135. key: key
  136. }).setContext(context);
  137. }
  138. }
  139. function _getPattern(parts, context) {
  140. var path = this;
  141. for (var _iterator = parts, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  142. var _ref;
  143. if (_isArray) {
  144. if (_i >= _iterator.length) break;
  145. _ref = _iterator[_i++];
  146. } else {
  147. _i = _iterator.next();
  148. if (_i.done) break;
  149. _ref = _i.value;
  150. }
  151. var part = _ref;
  152. if (part === ".") {
  153. path = path.parentPath;
  154. } else {
  155. if (Array.isArray(path)) {
  156. path = path[part];
  157. } else {
  158. path = path.get(part, context);
  159. }
  160. }
  161. }
  162. return path;
  163. }
  164. function getBindingIdentifiers(duplicates) {
  165. return t.getBindingIdentifiers(this.node, duplicates);
  166. }
  167. function getOuterBindingIdentifiers(duplicates) {
  168. return t.getOuterBindingIdentifiers(this.node, duplicates);
  169. }
  170. function getBindingIdentifierPaths() {
  171. var duplicates = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  172. var outerOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  173. var path = this;
  174. var search = [].concat(path);
  175. var ids = (0, _create2.default)(null);
  176. while (search.length) {
  177. var id = search.shift();
  178. if (!id) continue;
  179. if (!id.node) continue;
  180. var keys = t.getBindingIdentifiers.keys[id.node.type];
  181. if (id.isIdentifier()) {
  182. if (duplicates) {
  183. var _ids = ids[id.node.name] = ids[id.node.name] || [];
  184. _ids.push(id);
  185. } else {
  186. ids[id.node.name] = id;
  187. }
  188. continue;
  189. }
  190. if (id.isExportDeclaration()) {
  191. var declaration = id.get("declaration");
  192. if (declaration.isDeclaration()) {
  193. search.push(declaration);
  194. }
  195. continue;
  196. }
  197. if (outerOnly) {
  198. if (id.isFunctionDeclaration()) {
  199. search.push(id.get("id"));
  200. continue;
  201. }
  202. if (id.isFunctionExpression()) {
  203. continue;
  204. }
  205. }
  206. if (keys) {
  207. for (var i = 0; i < keys.length; i++) {
  208. var key = keys[i];
  209. var child = id.get(key);
  210. if (Array.isArray(child) || child.node) {
  211. search = search.concat(child);
  212. }
  213. }
  214. }
  215. }
  216. return ids;
  217. }
  218. function getOuterBindingIdentifierPaths(duplicates) {
  219. return this.getBindingIdentifierPaths(duplicates, true);
  220. }