family.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. var t = _interopRequireWildcard(require("@babel/types"));
  21. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  22. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. const NORMAL_COMPLETION = 0;
  25. const BREAK_COMPLETION = 1;
  26. function NormalCompletion(path) {
  27. return {
  28. type: NORMAL_COMPLETION,
  29. path
  30. };
  31. }
  32. function BreakCompletion(path) {
  33. return {
  34. type: BREAK_COMPLETION,
  35. path
  36. };
  37. }
  38. function getOpposite() {
  39. if (this.key === "left") {
  40. return this.getSibling("right");
  41. } else if (this.key === "right") {
  42. return this.getSibling("left");
  43. }
  44. return null;
  45. }
  46. function addCompletionRecords(path, records, context) {
  47. if (path) return records.concat(_getCompletionRecords(path, context));
  48. return records;
  49. }
  50. function completionRecordForSwitch(cases, records, context) {
  51. let lastNormalCompletions = [];
  52. for (let i = 0; i < cases.length; i++) {
  53. const casePath = cases[i];
  54. const caseCompletions = _getCompletionRecords(casePath, context);
  55. const normalCompletions = [];
  56. const breakCompletions = [];
  57. for (const c of caseCompletions) {
  58. if (c.type === NORMAL_COMPLETION) {
  59. normalCompletions.push(c);
  60. }
  61. if (c.type === BREAK_COMPLETION) {
  62. breakCompletions.push(c);
  63. }
  64. }
  65. if (normalCompletions.length) {
  66. lastNormalCompletions = normalCompletions;
  67. }
  68. records = records.concat(breakCompletions);
  69. }
  70. records = records.concat(lastNormalCompletions);
  71. return records;
  72. }
  73. function normalCompletionToBreak(completions) {
  74. completions.forEach(c => {
  75. c.type = BREAK_COMPLETION;
  76. });
  77. }
  78. function replaceBreakStatementInBreakCompletion(completions, reachable) {
  79. completions.forEach(c => {
  80. if (c.path.isBreakStatement({
  81. label: null
  82. })) {
  83. if (reachable) {
  84. c.path.replaceWith(t.unaryExpression("void", t.numericLiteral(0)));
  85. } else {
  86. c.path.remove();
  87. }
  88. }
  89. });
  90. }
  91. function getStatementListCompletion(paths, context) {
  92. let completions = [];
  93. if (context.canHaveBreak) {
  94. let lastNormalCompletions = [];
  95. for (let i = 0; i < paths.length; i++) {
  96. const path = paths[i];
  97. const newContext = Object.assign({}, context, {
  98. inCaseClause: false
  99. });
  100. if (path.isBlockStatement() && (context.inCaseClause || context.shouldPopulateBreak)) {
  101. newContext.shouldPopulateBreak = true;
  102. } else {
  103. newContext.shouldPopulateBreak = false;
  104. }
  105. const statementCompletions = _getCompletionRecords(path, newContext);
  106. if (statementCompletions.length > 0 && statementCompletions.every(c => c.type === BREAK_COMPLETION)) {
  107. if (lastNormalCompletions.length > 0 && statementCompletions.every(c => c.path.isBreakStatement({
  108. label: null
  109. }))) {
  110. normalCompletionToBreak(lastNormalCompletions);
  111. completions = completions.concat(lastNormalCompletions);
  112. if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
  113. completions = completions.concat(statementCompletions);
  114. replaceBreakStatementInBreakCompletion(statementCompletions, true);
  115. }
  116. replaceBreakStatementInBreakCompletion(statementCompletions, false);
  117. } else {
  118. completions = completions.concat(statementCompletions);
  119. if (!context.shouldPopulateBreak) {
  120. replaceBreakStatementInBreakCompletion(statementCompletions, true);
  121. }
  122. }
  123. break;
  124. }
  125. if (i === paths.length - 1) {
  126. completions = completions.concat(statementCompletions);
  127. } else {
  128. completions = completions.concat(statementCompletions.filter(c => c.type === BREAK_COMPLETION));
  129. lastNormalCompletions = statementCompletions.filter(c => c.type === NORMAL_COMPLETION);
  130. }
  131. }
  132. } else if (paths.length) {
  133. completions = completions.concat(_getCompletionRecords(paths[paths.length - 1], context));
  134. }
  135. return completions;
  136. }
  137. function _getCompletionRecords(path, context) {
  138. let records = [];
  139. if (path.isIfStatement()) {
  140. records = addCompletionRecords(path.get("consequent"), records, context);
  141. records = addCompletionRecords(path.get("alternate"), records, context);
  142. } else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
  143. records = addCompletionRecords(path.get("body"), records, context);
  144. } else if (path.isProgram() || path.isBlockStatement()) {
  145. records = records.concat(getStatementListCompletion(path.get("body"), context));
  146. } else if (path.isFunction()) {
  147. return _getCompletionRecords(path.get("body"), context);
  148. } else if (path.isTryStatement()) {
  149. records = addCompletionRecords(path.get("block"), records, context);
  150. records = addCompletionRecords(path.get("handler"), records, context);
  151. } else if (path.isCatchClause()) {
  152. records = addCompletionRecords(path.get("body"), records, context);
  153. } else if (path.isSwitchStatement()) {
  154. records = completionRecordForSwitch(path.get("cases"), records, context);
  155. } else if (path.isSwitchCase()) {
  156. records = records.concat(getStatementListCompletion(path.get("consequent"), {
  157. canHaveBreak: true,
  158. shouldPopulateBreak: false,
  159. inCaseClause: true
  160. }));
  161. } else if (path.isBreakStatement()) {
  162. records.push(BreakCompletion(path));
  163. } else {
  164. records.push(NormalCompletion(path));
  165. }
  166. return records;
  167. }
  168. function getCompletionRecords() {
  169. const records = _getCompletionRecords(this, {
  170. canHaveBreak: false,
  171. shouldPopulateBreak: false,
  172. inCaseClause: false
  173. });
  174. return records.map(r => r.path);
  175. }
  176. function getSibling(key) {
  177. return _index.default.get({
  178. parentPath: this.parentPath,
  179. parent: this.parent,
  180. container: this.container,
  181. listKey: this.listKey,
  182. key: key
  183. }).setContext(this.context);
  184. }
  185. function getPrevSibling() {
  186. return this.getSibling(this.key - 1);
  187. }
  188. function getNextSibling() {
  189. return this.getSibling(this.key + 1);
  190. }
  191. function getAllNextSiblings() {
  192. let _key = this.key;
  193. let sibling = this.getSibling(++_key);
  194. const siblings = [];
  195. while (sibling.node) {
  196. siblings.push(sibling);
  197. sibling = this.getSibling(++_key);
  198. }
  199. return siblings;
  200. }
  201. function getAllPrevSiblings() {
  202. let _key = this.key;
  203. let sibling = this.getSibling(--_key);
  204. const siblings = [];
  205. while (sibling.node) {
  206. siblings.push(sibling);
  207. sibling = this.getSibling(--_key);
  208. }
  209. return siblings;
  210. }
  211. function get(key, context = true) {
  212. if (context === true) context = this.context;
  213. const parts = key.split(".");
  214. if (parts.length === 1) {
  215. return this._getKey(key, context);
  216. } else {
  217. return this._getPattern(parts, context);
  218. }
  219. }
  220. function _getKey(key, context) {
  221. const node = this.node;
  222. const container = node[key];
  223. if (Array.isArray(container)) {
  224. return container.map((_, i) => {
  225. return _index.default.get({
  226. listKey: key,
  227. parentPath: this,
  228. parent: node,
  229. container: container,
  230. key: i
  231. }).setContext(context);
  232. });
  233. } else {
  234. return _index.default.get({
  235. parentPath: this,
  236. parent: node,
  237. container: node,
  238. key: key
  239. }).setContext(context);
  240. }
  241. }
  242. function _getPattern(parts, context) {
  243. let path = this;
  244. for (const part of parts) {
  245. if (part === ".") {
  246. path = path.parentPath;
  247. } else {
  248. if (Array.isArray(path)) {
  249. path = path[part];
  250. } else {
  251. path = path.get(part, context);
  252. }
  253. }
  254. }
  255. return path;
  256. }
  257. function getBindingIdentifiers(duplicates) {
  258. return t.getBindingIdentifiers(this.node, duplicates);
  259. }
  260. function getOuterBindingIdentifiers(duplicates) {
  261. return t.getOuterBindingIdentifiers(this.node, duplicates);
  262. }
  263. function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
  264. const path = this;
  265. let search = [].concat(path);
  266. const ids = Object.create(null);
  267. while (search.length) {
  268. const id = search.shift();
  269. if (!id) continue;
  270. if (!id.node) continue;
  271. const keys = t.getBindingIdentifiers.keys[id.node.type];
  272. if (id.isIdentifier()) {
  273. if (duplicates) {
  274. const _ids = ids[id.node.name] = ids[id.node.name] || [];
  275. _ids.push(id);
  276. } else {
  277. ids[id.node.name] = id;
  278. }
  279. continue;
  280. }
  281. if (id.isExportDeclaration()) {
  282. const declaration = id.get("declaration");
  283. if (declaration.isDeclaration()) {
  284. search.push(declaration);
  285. }
  286. continue;
  287. }
  288. if (outerOnly) {
  289. if (id.isFunctionDeclaration()) {
  290. search.push(id.get("id"));
  291. continue;
  292. }
  293. if (id.isFunctionExpression()) {
  294. continue;
  295. }
  296. }
  297. if (keys) {
  298. for (let i = 0; i < keys.length; i++) {
  299. const key = keys[i];
  300. const child = id.get(key);
  301. if (Array.isArray(child) || child.node) {
  302. search = search.concat(child);
  303. }
  304. }
  305. }
  306. }
  307. return ids;
  308. }
  309. function getOuterBindingIdentifierPaths(duplicates) {
  310. return this.getBindingIdentifierPaths(duplicates, true);
  311. }