123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.getOpposite = getOpposite;
- exports.getCompletionRecords = getCompletionRecords;
- exports.getSibling = getSibling;
- exports.getPrevSibling = getPrevSibling;
- exports.getNextSibling = getNextSibling;
- exports.getAllNextSiblings = getAllNextSiblings;
- exports.getAllPrevSiblings = getAllPrevSiblings;
- exports.get = get;
- exports._getKey = _getKey;
- exports._getPattern = _getPattern;
- exports.getBindingIdentifiers = getBindingIdentifiers;
- exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
- exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
- exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
-
- var _index = require("./index");
-
- var t = require("@babel/types");
-
- const NORMAL_COMPLETION = 0;
- const BREAK_COMPLETION = 1;
-
- function NormalCompletion(path) {
- return {
- type: NORMAL_COMPLETION,
- path
- };
- }
-
- function BreakCompletion(path) {
- return {
- type: BREAK_COMPLETION,
- path
- };
- }
-
- function getOpposite() {
- if (this.key === "left") {
- return this.getSibling("right");
- } else if (this.key === "right") {
- return this.getSibling("left");
- }
-
- return null;
- }
-
- function addCompletionRecords(path, records, context) {
- if (path) return records.concat(_getCompletionRecords(path, context));
- return records;
- }
-
- function completionRecordForSwitch(cases, records, context) {
- let lastNormalCompletions = [];
-
- for (let i = 0; i < cases.length; i++) {
- const casePath = cases[i];
-
- const caseCompletions = _getCompletionRecords(casePath, context);
-
- const normalCompletions = [];
- const breakCompletions = [];
-
- for (const c of caseCompletions) {
- if (c.type === NORMAL_COMPLETION) {
- normalCompletions.push(c);
- }
-
- if (c.type === BREAK_COMPLETION) {
- breakCompletions.push(c);
- }
- }
-
- if (normalCompletions.length) {
- lastNormalCompletions = normalCompletions;
- }
-
- records = records.concat(breakCompletions);
- }
-
- records = records.concat(lastNormalCompletions);
- return records;
- }
-
- function normalCompletionToBreak(completions) {
- completions.forEach(c => {
- c.type = BREAK_COMPLETION;
- });
- }
-
- function replaceBreakStatementInBreakCompletion(completions, reachable) {
- completions.forEach(c => {
- if (c.path.isBreakStatement({
- label: null
- })) {
- if (reachable) {
- c.path.replaceWith(t.unaryExpression("void", t.numericLiteral(0)));
- } else {
- c.path.remove();
- }
- }
- });
- }
-
- function getStatementListCompletion(paths, context) {
- let completions = [];
-
- if (context.canHaveBreak) {
- let lastNormalCompletions = [];
-
- for (let i = 0; i < paths.length; i++) {
- const path = paths[i];
- const newContext = Object.assign({}, context, {
- inCaseClause: false
- });
-
- if (path.isBlockStatement() && (context.inCaseClause || context.shouldPopulateBreak)) {
- newContext.shouldPopulateBreak = true;
- } else {
- newContext.shouldPopulateBreak = false;
- }
-
- const statementCompletions = _getCompletionRecords(path, newContext);
-
- if (statementCompletions.length > 0 && statementCompletions.every(c => c.type === BREAK_COMPLETION)) {
- if (lastNormalCompletions.length > 0 && statementCompletions.every(c => c.path.isBreakStatement({
- label: null
- }))) {
- normalCompletionToBreak(lastNormalCompletions);
- completions = completions.concat(lastNormalCompletions);
-
- if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
- completions = completions.concat(statementCompletions);
- replaceBreakStatementInBreakCompletion(statementCompletions, true);
- }
-
- replaceBreakStatementInBreakCompletion(statementCompletions, false);
- } else {
- completions = completions.concat(statementCompletions);
-
- if (!context.shouldPopulateBreak) {
- replaceBreakStatementInBreakCompletion(statementCompletions, true);
- }
- }
-
- break;
- }
-
- if (i === paths.length - 1) {
- completions = completions.concat(statementCompletions);
- } else {
- completions = completions.concat(statementCompletions.filter(c => c.type === BREAK_COMPLETION));
- lastNormalCompletions = statementCompletions.filter(c => c.type === NORMAL_COMPLETION);
- }
- }
- } else if (paths.length) {
- completions = completions.concat(_getCompletionRecords(paths[paths.length - 1], context));
- }
-
- return completions;
- }
-
- function _getCompletionRecords(path, context) {
- let records = [];
-
- if (path.isIfStatement()) {
- records = addCompletionRecords(path.get("consequent"), records, context);
- records = addCompletionRecords(path.get("alternate"), records, context);
- } else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
- records = addCompletionRecords(path.get("body"), records, context);
- } else if (path.isProgram() || path.isBlockStatement()) {
- records = records.concat(getStatementListCompletion(path.get("body"), context));
- } else if (path.isFunction()) {
- return _getCompletionRecords(path.get("body"), context);
- } else if (path.isTryStatement()) {
- records = addCompletionRecords(path.get("block"), records, context);
- records = addCompletionRecords(path.get("handler"), records, context);
- } else if (path.isCatchClause()) {
- records = addCompletionRecords(path.get("body"), records, context);
- } else if (path.isSwitchStatement()) {
- records = completionRecordForSwitch(path.get("cases"), records, context);
- } else if (path.isSwitchCase()) {
- records = records.concat(getStatementListCompletion(path.get("consequent"), {
- canHaveBreak: true,
- shouldPopulateBreak: false,
- inCaseClause: true
- }));
- } else if (path.isBreakStatement()) {
- records.push(BreakCompletion(path));
- } else {
- records.push(NormalCompletion(path));
- }
-
- return records;
- }
-
- function getCompletionRecords() {
- const records = _getCompletionRecords(this, {
- canHaveBreak: false,
- shouldPopulateBreak: false,
- inCaseClause: false
- });
-
- return records.map(r => r.path);
- }
-
- function getSibling(key) {
- return _index.default.get({
- parentPath: this.parentPath,
- parent: this.parent,
- container: this.container,
- listKey: this.listKey,
- key: key
- }).setContext(this.context);
- }
-
- function getPrevSibling() {
- return this.getSibling(this.key - 1);
- }
-
- function getNextSibling() {
- return this.getSibling(this.key + 1);
- }
-
- function getAllNextSiblings() {
- let _key = this.key;
- let sibling = this.getSibling(++_key);
- const siblings = [];
-
- while (sibling.node) {
- siblings.push(sibling);
- sibling = this.getSibling(++_key);
- }
-
- return siblings;
- }
-
- function getAllPrevSiblings() {
- let _key = this.key;
- let sibling = this.getSibling(--_key);
- const siblings = [];
-
- while (sibling.node) {
- siblings.push(sibling);
- sibling = this.getSibling(--_key);
- }
-
- return siblings;
- }
-
- function get(key, context = true) {
- if (context === true) context = this.context;
- const parts = key.split(".");
-
- if (parts.length === 1) {
- return this._getKey(key, context);
- } else {
- return this._getPattern(parts, context);
- }
- }
-
- function _getKey(key, context) {
- const node = this.node;
- const container = node[key];
-
- if (Array.isArray(container)) {
- return container.map((_, i) => {
- return _index.default.get({
- listKey: key,
- parentPath: this,
- parent: node,
- container: container,
- key: i
- }).setContext(context);
- });
- } else {
- return _index.default.get({
- parentPath: this,
- parent: node,
- container: node,
- key: key
- }).setContext(context);
- }
- }
-
- function _getPattern(parts, context) {
- let path = this;
-
- for (const part of parts) {
- if (part === ".") {
- path = path.parentPath;
- } else {
- if (Array.isArray(path)) {
- path = path[part];
- } else {
- path = path.get(part, context);
- }
- }
- }
-
- return path;
- }
-
- function getBindingIdentifiers(duplicates) {
- return t.getBindingIdentifiers(this.node, duplicates);
- }
-
- function getOuterBindingIdentifiers(duplicates) {
- return t.getOuterBindingIdentifiers(this.node, duplicates);
- }
-
- function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
- const path = this;
- let search = [].concat(path);
- const ids = Object.create(null);
-
- while (search.length) {
- const id = search.shift();
- if (!id) continue;
- if (!id.node) continue;
- const keys = t.getBindingIdentifiers.keys[id.node.type];
-
- if (id.isIdentifier()) {
- if (duplicates) {
- const _ids = ids[id.node.name] = ids[id.node.name] || [];
-
- _ids.push(id);
- } else {
- ids[id.node.name] = id;
- }
-
- continue;
- }
-
- if (id.isExportDeclaration()) {
- const declaration = id.get("declaration");
-
- if (declaration.isDeclaration()) {
- search.push(declaration);
- }
-
- continue;
- }
-
- if (outerOnly) {
- if (id.isFunctionDeclaration()) {
- search.push(id.get("id"));
- continue;
- }
-
- if (id.isFunctionExpression()) {
- continue;
- }
- }
-
- if (keys) {
- for (let i = 0; i < keys.length; i++) {
- const key = keys[i];
- const child = id.get(key);
-
- if (Array.isArray(child) || child.node) {
- search = search.concat(child);
- }
- }
- }
- }
-
- return ids;
- }
-
- function getOuterBindingIdentifierPaths(duplicates) {
- return this.getBindingIdentifierPaths(duplicates, true);
- }
|