family.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. replaceBreakStatementInBreakCompletion(statementCompletions, true);
  122. }
  123. replaceBreakStatementInBreakCompletion(statementCompletions, false);
  124. } else {
  125. completions.push(...statementCompletions);
  126. if (!context.shouldPopulateBreak) {
  127. replaceBreakStatementInBreakCompletion(statementCompletions, true);
  128. }
  129. }
  130. break;
  131. }
  132. if (i === paths.length - 1) {
  133. completions.push(...statementCompletions);
  134. } else {
  135. lastNormalCompletions = [];
  136. for (let i = 0; i < statementCompletions.length; i++) {
  137. const c = statementCompletions[i];
  138. if (c.type === BREAK_COMPLETION) {
  139. completions.push(c);
  140. }
  141. if (c.type === NORMAL_COMPLETION) {
  142. lastNormalCompletions.push(c);
  143. }
  144. }
  145. }
  146. }
  147. } else if (paths.length) {
  148. for (let i = paths.length - 1; i >= 0; i--) {
  149. const pathCompletions = _getCompletionRecords(paths[i], context);
  150. if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) {
  151. completions.push(...pathCompletions);
  152. break;
  153. }
  154. }
  155. }
  156. return completions;
  157. }
  158. function _getCompletionRecords(path, context) {
  159. let records = [];
  160. if (path.isIfStatement()) {
  161. records = addCompletionRecords(path.get("consequent"), records, context);
  162. records = addCompletionRecords(path.get("alternate"), records, context);
  163. } else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
  164. return addCompletionRecords(path.get("body"), records, context);
  165. } else if (path.isProgram() || path.isBlockStatement()) {
  166. return getStatementListCompletion(path.get("body"), context);
  167. } else if (path.isFunction()) {
  168. return _getCompletionRecords(path.get("body"), context);
  169. } else if (path.isTryStatement()) {
  170. records = addCompletionRecords(path.get("block"), records, context);
  171. records = addCompletionRecords(path.get("handler"), records, context);
  172. } else if (path.isCatchClause()) {
  173. return addCompletionRecords(path.get("body"), records, context);
  174. } else if (path.isSwitchStatement()) {
  175. return completionRecordForSwitch(path.get("cases"), records, context);
  176. } else if (path.isSwitchCase()) {
  177. return getStatementListCompletion(path.get("consequent"), {
  178. canHaveBreak: true,
  179. shouldPopulateBreak: false,
  180. inCaseClause: true
  181. });
  182. } else if (path.isBreakStatement()) {
  183. records.push(BreakCompletion(path));
  184. } else {
  185. records.push(NormalCompletion(path));
  186. }
  187. return records;
  188. }
  189. function getCompletionRecords() {
  190. const records = _getCompletionRecords(this, {
  191. canHaveBreak: false,
  192. shouldPopulateBreak: false,
  193. inCaseClause: false
  194. });
  195. return records.map(r => r.path);
  196. }
  197. function getSibling(key) {
  198. return _index.default.get({
  199. parentPath: this.parentPath,
  200. parent: this.parent,
  201. container: this.container,
  202. listKey: this.listKey,
  203. key: key
  204. }).setContext(this.context);
  205. }
  206. function getPrevSibling() {
  207. return this.getSibling(this.key - 1);
  208. }
  209. function getNextSibling() {
  210. return this.getSibling(this.key + 1);
  211. }
  212. function getAllNextSiblings() {
  213. let _key = this.key;
  214. let sibling = this.getSibling(++_key);
  215. const siblings = [];
  216. while (sibling.node) {
  217. siblings.push(sibling);
  218. sibling = this.getSibling(++_key);
  219. }
  220. return siblings;
  221. }
  222. function getAllPrevSiblings() {
  223. let _key = this.key;
  224. let sibling = this.getSibling(--_key);
  225. const siblings = [];
  226. while (sibling.node) {
  227. siblings.push(sibling);
  228. sibling = this.getSibling(--_key);
  229. }
  230. return siblings;
  231. }
  232. function get(key, context = true) {
  233. if (context === true) context = this.context;
  234. const parts = key.split(".");
  235. if (parts.length === 1) {
  236. return _getKey.call(this, key, context);
  237. } else {
  238. return _getPattern.call(this, parts, context);
  239. }
  240. }
  241. function _getKey(key, context) {
  242. const node = this.node;
  243. const container = node[key];
  244. if (Array.isArray(container)) {
  245. return container.map((_, i) => {
  246. return _index.default.get({
  247. listKey: key,
  248. parentPath: this,
  249. parent: node,
  250. container: container,
  251. key: i
  252. }).setContext(context);
  253. });
  254. } else {
  255. return _index.default.get({
  256. parentPath: this,
  257. parent: node,
  258. container: node,
  259. key: key
  260. }).setContext(context);
  261. }
  262. }
  263. function _getPattern(parts, context) {
  264. let path = this;
  265. for (const part of parts) {
  266. if (part === ".") {
  267. path = path.parentPath;
  268. } else {
  269. if (Array.isArray(path)) {
  270. path = path[part];
  271. } else {
  272. path = path.get(part, context);
  273. }
  274. }
  275. }
  276. return path;
  277. }
  278. function getAssignmentIdentifiers() {
  279. return _getAssignmentIdentifiers(this.node);
  280. }
  281. function getBindingIdentifiers(duplicates) {
  282. return _getBindingIdentifiers(this.node, duplicates);
  283. }
  284. function getOuterBindingIdentifiers(duplicates) {
  285. return _getOuterBindingIdentifiers(this.node, duplicates);
  286. }
  287. function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
  288. const path = this;
  289. const search = [path];
  290. const ids = Object.create(null);
  291. while (search.length) {
  292. const id = search.shift();
  293. if (!id) continue;
  294. if (!id.node) continue;
  295. const keys = _getBindingIdentifiers.keys[id.node.type];
  296. if (id.isIdentifier()) {
  297. if (duplicates) {
  298. const _ids = ids[id.node.name] = ids[id.node.name] || [];
  299. _ids.push(id);
  300. } else {
  301. ids[id.node.name] = id;
  302. }
  303. continue;
  304. }
  305. if (id.isExportDeclaration()) {
  306. const declaration = id.get("declaration");
  307. if (declaration.isDeclaration()) {
  308. search.push(declaration);
  309. }
  310. continue;
  311. }
  312. if (outerOnly) {
  313. if (id.isFunctionDeclaration()) {
  314. search.push(id.get("id"));
  315. continue;
  316. }
  317. if (id.isFunctionExpression()) {
  318. continue;
  319. }
  320. }
  321. if (keys) {
  322. for (let i = 0; i < keys.length; i++) {
  323. const key = keys[i];
  324. const child = id.get(key);
  325. if (Array.isArray(child)) {
  326. search.push(...child);
  327. } else if (child.node) {
  328. search.push(child);
  329. }
  330. }
  331. }
  332. }
  333. return ids;
  334. }
  335. function getOuterBindingIdentifierPaths(duplicates = false) {
  336. return this.getBindingIdentifierPaths(duplicates, true);
  337. }
  338. //# sourceMappingURL=family.js.map