family.js 10 KB

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