family.js 9.9 KB

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