项目原始demo,不改动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

254 line
6.2 KiB

  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. var data = _interopRequireWildcard(require("@babel/types"));
  22. t = function t() {
  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. var 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. var _key = this.key;
  79. var sibling = this.getSibling(++_key);
  80. var siblings = [];
  81. while (sibling.node) {
  82. siblings.push(sibling);
  83. sibling = this.getSibling(++_key);
  84. }
  85. return siblings;
  86. }
  87. function getAllPrevSiblings() {
  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 get(key, context) {
  98. if (context === true) context = this.context;
  99. var 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. var _this = this;
  108. var node = this.node;
  109. var container = node[key];
  110. if (Array.isArray(container)) {
  111. return container.map(function (_, i) {
  112. return _index.default.get({
  113. listKey: key,
  114. parentPath: _this,
  115. parent: node,
  116. container: container,
  117. key: i
  118. }).setContext(context);
  119. });
  120. } else {
  121. return _index.default.get({
  122. parentPath: this,
  123. parent: node,
  124. container: node,
  125. key: key
  126. }).setContext(context);
  127. }
  128. }
  129. function _getPattern(parts, context) {
  130. var path = this;
  131. var _arr = parts;
  132. for (var _i = 0; _i < _arr.length; _i++) {
  133. var part = _arr[_i];
  134. if (part === ".") {
  135. path = path.parentPath;
  136. } else {
  137. if (Array.isArray(path)) {
  138. path = path[part];
  139. } else {
  140. path = path.get(part, context);
  141. }
  142. }
  143. }
  144. return path;
  145. }
  146. function getBindingIdentifiers(duplicates) {
  147. return t().getBindingIdentifiers(this.node, duplicates);
  148. }
  149. function getOuterBindingIdentifiers(duplicates) {
  150. return t().getOuterBindingIdentifiers(this.node, duplicates);
  151. }
  152. function getBindingIdentifierPaths(duplicates, outerOnly) {
  153. if (duplicates === void 0) {
  154. duplicates = false;
  155. }
  156. if (outerOnly === void 0) {
  157. outerOnly = false;
  158. }
  159. var path = this;
  160. var search = [].concat(path);
  161. var ids = Object.create(null);
  162. while (search.length) {
  163. var id = search.shift();
  164. if (!id) continue;
  165. if (!id.node) continue;
  166. var keys = t().getBindingIdentifiers.keys[id.node.type];
  167. if (id.isIdentifier()) {
  168. if (duplicates) {
  169. var _ids = ids[id.node.name] = ids[id.node.name] || [];
  170. _ids.push(id);
  171. } else {
  172. ids[id.node.name] = id;
  173. }
  174. continue;
  175. }
  176. if (id.isExportDeclaration()) {
  177. var declaration = id.get("declaration");
  178. if (declaration.isDeclaration()) {
  179. search.push(declaration);
  180. }
  181. continue;
  182. }
  183. if (outerOnly) {
  184. if (id.isFunctionDeclaration()) {
  185. search.push(id.get("id"));
  186. continue;
  187. }
  188. if (id.isFunctionExpression()) {
  189. continue;
  190. }
  191. }
  192. if (keys) {
  193. for (var i = 0; i < keys.length; i++) {
  194. var key = keys[i];
  195. var child = id.get(key);
  196. if (Array.isArray(child) || child.node) {
  197. search = search.concat(child);
  198. }
  199. }
  200. }
  201. }
  202. return ids;
  203. }
  204. function getOuterBindingIdentifierPaths(duplicates) {
  205. return this.getBindingIdentifierPaths(duplicates, true);
  206. }