family.js 9.9 KB

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