[d565449] | 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 | 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 _getKey.call(this, key, context);
|
---|
| 238 | } else {
|
---|
| 239 | return _getPattern.call(this, 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 getAssignmentIdentifiers() {
|
---|
| 280 | return _getAssignmentIdentifiers(this.node);
|
---|
| 281 | }
|
---|
| 282 | function getBindingIdentifiers(duplicates) {
|
---|
| 283 | return _getBindingIdentifiers(this.node, duplicates);
|
---|
| 284 | }
|
---|
| 285 | function getOuterBindingIdentifiers(duplicates) {
|
---|
| 286 | return _getOuterBindingIdentifiers(this.node, duplicates);
|
---|
| 287 | }
|
---|
| 288 | function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
---|
| 289 | const path = this;
|
---|
| 290 | const search = [path];
|
---|
| 291 | const ids = Object.create(null);
|
---|
| 292 | while (search.length) {
|
---|
| 293 | const id = search.shift();
|
---|
| 294 | if (!id) continue;
|
---|
| 295 | if (!id.node) continue;
|
---|
| 296 | const keys = _getBindingIdentifiers.keys[id.node.type];
|
---|
| 297 | if (id.isIdentifier()) {
|
---|
| 298 | if (duplicates) {
|
---|
| 299 | const _ids = ids[id.node.name] = ids[id.node.name] || [];
|
---|
| 300 | _ids.push(id);
|
---|
| 301 | } else {
|
---|
| 302 | ids[id.node.name] = id;
|
---|
| 303 | }
|
---|
| 304 | continue;
|
---|
| 305 | }
|
---|
| 306 | if (id.isExportDeclaration()) {
|
---|
| 307 | const declaration = id.get("declaration");
|
---|
| 308 | if (declaration.isDeclaration()) {
|
---|
| 309 | search.push(declaration);
|
---|
| 310 | }
|
---|
| 311 | continue;
|
---|
| 312 | }
|
---|
| 313 | if (outerOnly) {
|
---|
| 314 | if (id.isFunctionDeclaration()) {
|
---|
| 315 | search.push(id.get("id"));
|
---|
| 316 | continue;
|
---|
| 317 | }
|
---|
| 318 | if (id.isFunctionExpression()) {
|
---|
| 319 | continue;
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | if (keys) {
|
---|
| 323 | for (let i = 0; i < keys.length; i++) {
|
---|
| 324 | const key = keys[i];
|
---|
| 325 | const child = id.get(key);
|
---|
| 326 | if (Array.isArray(child)) {
|
---|
| 327 | search.push(...child);
|
---|
| 328 | } else if (child.node) {
|
---|
| 329 | search.push(child);
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 | return ids;
|
---|
| 335 | }
|
---|
| 336 | function getOuterBindingIdentifierPaths(duplicates = false) {
|
---|
| 337 | return this.getBindingIdentifierPaths(duplicates, true);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | //# sourceMappingURL=family.js.map
|
---|