family.js 9.9 KB

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