family.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getOpposite = getOpposite;
  6. exports.getCompletionRecords = getCompletionRecords;
  7. exports.getSibling = getSibling;
  8. exports.getPrevSibling = getPrevSibling;
  9. exports.getNextSibling = getNextSibling;
  10. exports.getAllNextSiblings = getAllNextSiblings;
  11. exports.getAllPrevSiblings = getAllPrevSiblings;
  12. exports.get = get;
  13. exports._getKey = _getKey;
  14. exports._getPattern = _getPattern;
  15. exports.getBindingIdentifiers = getBindingIdentifiers;
  16. exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
  17. exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
  18. exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
  19. var _index = _interopRequireDefault(require("./index"));
  20. function t() {
  21. const data = _interopRequireWildcard(require("@babel/types"));
  22. t = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. 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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. function getOpposite() {
  30. if (this.key === "left") {
  31. return this.getSibling("right");
  32. } else if (this.key === "right") {
  33. return this.getSibling("left");
  34. }
  35. }
  36. function addCompletionRecords(path, paths) {
  37. if (path) return paths.concat(path.getCompletionRecords());
  38. return paths;
  39. }
  40. function getCompletionRecords() {
  41. let paths = [];
  42. if (this.isIfStatement()) {
  43. paths = addCompletionRecords(this.get("consequent"), paths);
  44. paths = addCompletionRecords(this.get("alternate"), paths);
  45. } else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
  46. paths = addCompletionRecords(this.get("body"), paths);
  47. } else if (this.isProgram() || this.isBlockStatement()) {
  48. paths = addCompletionRecords(this.get("body").pop(), paths);
  49. } else if (this.isFunction()) {
  50. return this.get("body").getCompletionRecords();
  51. } else if (this.isTryStatement()) {
  52. paths = addCompletionRecords(this.get("block"), paths);
  53. paths = addCompletionRecords(this.get("handler"), paths);
  54. paths = addCompletionRecords(this.get("finalizer"), paths);
  55. } else if (this.isCatchClause()) {
  56. paths = addCompletionRecords(this.get("body"), paths);
  57. } else {
  58. paths.push(this);
  59. }
  60. return paths;
  61. }
  62. function getSibling(key) {
  63. return _index.default.get({
  64. parentPath: this.parentPath,
  65. parent: this.parent,
  66. container: this.container,
  67. listKey: this.listKey,
  68. key: key
  69. });
  70. }
  71. function getPrevSibling() {
  72. return this.getSibling(this.key - 1);
  73. }
  74. function getNextSibling() {
  75. return this.getSibling(this.key + 1);
  76. }
  77. function getAllNextSiblings() {
  78. let _key = this.key;
  79. let sibling = this.getSibling(++_key);
  80. const siblings = [];
  81. while (sibling.node) {
  82. siblings.push(sibling);
  83. sibling = this.getSibling(++_key);
  84. }
  85. return siblings;
  86. }
  87. function getAllPrevSiblings() {
  88. let _key = this.key;
  89. let sibling = this.getSibling(--_key);
  90. const siblings = [];
  91. while (sibling.node) {
  92. siblings.push(sibling);
  93. sibling = this.getSibling(--_key);
  94. }
  95. return siblings;
  96. }
  97. function get(key, context) {
  98. if (context === true) context = this.context;
  99. const parts = key.split(".");
  100. if (parts.length === 1) {
  101. return this._getKey(key, context);
  102. } else {
  103. return this._getPattern(parts, context);
  104. }
  105. }
  106. function _getKey(key, context) {
  107. const node = this.node;
  108. const container = node[key];
  109. if (Array.isArray(container)) {
  110. return container.map((_, i) => {
  111. return _index.default.get({
  112. listKey: key,
  113. parentPath: this,
  114. parent: node,
  115. container: container,
  116. key: i
  117. }).setContext(context);
  118. });
  119. } else {
  120. return _index.default.get({
  121. parentPath: this,
  122. parent: node,
  123. container: node,
  124. key: key
  125. }).setContext(context);
  126. }
  127. }
  128. function _getPattern(parts, context) {
  129. let path = this;
  130. for (const part of parts) {
  131. if (part === ".") {
  132. path = path.parentPath;
  133. } else {
  134. if (Array.isArray(path)) {
  135. path = path[part];
  136. } else {
  137. path = path.get(part, context);
  138. }
  139. }
  140. }
  141. return path;
  142. }
  143. function getBindingIdentifiers(duplicates) {
  144. return t().getBindingIdentifiers(this.node, duplicates);
  145. }
  146. function getOuterBindingIdentifiers(duplicates) {
  147. return t().getOuterBindingIdentifiers(this.node, duplicates);
  148. }
  149. function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
  150. const path = this;
  151. let search = [].concat(path);
  152. const ids = Object.create(null);
  153. while (search.length) {
  154. const id = search.shift();
  155. if (!id) continue;
  156. if (!id.node) continue;
  157. const keys = t().getBindingIdentifiers.keys[id.node.type];
  158. if (id.isIdentifier()) {
  159. if (duplicates) {
  160. const _ids = ids[id.node.name] = ids[id.node.name] || [];
  161. _ids.push(id);
  162. } else {
  163. ids[id.node.name] = id;
  164. }
  165. continue;
  166. }
  167. if (id.isExportDeclaration()) {
  168. const declaration = id.get("declaration");
  169. if (declaration.isDeclaration()) {
  170. search.push(declaration);
  171. }
  172. continue;
  173. }
  174. if (outerOnly) {
  175. if (id.isFunctionDeclaration()) {
  176. search.push(id.get("id"));
  177. continue;
  178. }
  179. if (id.isFunctionExpression()) {
  180. continue;
  181. }
  182. }
  183. if (keys) {
  184. for (let i = 0; i < keys.length; i++) {
  185. const key = keys[i];
  186. const child = id.get(key);
  187. if (Array.isArray(child) || child.node) {
  188. search = search.concat(child);
  189. }
  190. }
  191. }
  192. }
  193. return ids;
  194. }
  195. function getOuterBindingIdentifierPaths(duplicates) {
  196. return this.getBindingIdentifierPaths(duplicates, true);
  197. }