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