test/dummy/node_modules/@babel/traverse/lib/path/family.js in disco_app-0.18.1 vs test/dummy/node_modules/@babel/traverse/lib/path/family.js in disco_app-0.18.2

- old
+ new

@@ -16,236 +16,118 @@ exports.getBindingIdentifiers = getBindingIdentifiers; exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers; exports.getBindingIdentifierPaths = getBindingIdentifierPaths; exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths; -var _index = require("./index"); +var _index = _interopRequireDefault(require("./index")); -var _t = require("@babel/types"); +var t = _interopRequireWildcard(require("@babel/types")); -const { - getBindingIdentifiers: _getBindingIdentifiers, - getOuterBindingIdentifiers: _getOuterBindingIdentifiers, - isDeclaration, - numericLiteral, - unaryExpression -} = _t; -const NORMAL_COMPLETION = 0; -const BREAK_COMPLETION = 1; +function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } -function NormalCompletion(path) { - return { - type: NORMAL_COMPLETION, - path - }; -} +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } -function BreakCompletion(path) { - return { - type: BREAK_COMPLETION, - path - }; -} +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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) { - records.push(..._getCompletionRecords(path, context)); - } - - return records; +function addCompletionRecords(path, paths) { + if (path) return paths.concat(path.getCompletionRecords()); + return paths; } -function completionRecordForSwitch(cases, records, context) { - let lastNormalCompletions = []; +function completionRecordForSwitch(cases, paths) { + let isLastCaseWithConsequent = true; - for (let i = 0; i < cases.length; i++) { - const casePath = cases[i]; + for (let i = cases.length - 1; i >= 0; i--) { + const switchCase = cases[i]; + const consequent = switchCase.get("consequent"); + let breakStatement; - const caseCompletions = _getCompletionRecords(casePath, context); - - const normalCompletions = []; - const breakCompletions = []; - - for (const c of caseCompletions) { - if (c.type === NORMAL_COMPLETION) { - normalCompletions.push(c); + findBreak: for (const statement of consequent) { + if (statement.isBlockStatement()) { + for (const statementInBlock of statement.get("body")) { + if (statementInBlock.isBreakStatement()) { + breakStatement = statementInBlock; + break findBreak; + } + } + } else if (statement.isBreakStatement()) { + breakStatement = statement; + break; } - - if (c.type === BREAK_COMPLETION) { - breakCompletions.push(c); - } } - if (normalCompletions.length) { - lastNormalCompletions = normalCompletions; - } - - records.push(...breakCompletions); - } - - records.push(...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(unaryExpression("void", numericLiteral(0))); - } else { - c.path.remove(); + if (breakStatement) { + while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) { + breakStatement = breakStatement.parentPath; } - } - }); -} -function getStatementListCompletion(paths, context) { - const completions = []; + const prevSibling = breakStatement.getPrevSibling(); - 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; + if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) { + paths = addCompletionRecords(prevSibling, paths); + breakStatement.remove(); } else { - newContext.shouldPopulateBreak = false; + breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode()); + paths = addCompletionRecords(breakStatement, paths); } + } else if (isLastCaseWithConsequent) { + const statementFinder = statement => !statement.isBlockStatement() || statement.get("body").some(statementFinder); - const statementCompletions = _getCompletionRecords(path, newContext); + const hasConsequent = consequent.some(statementFinder); - 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.push(...lastNormalCompletions); - - if (lastNormalCompletions.some(c => c.path.isDeclaration())) { - completions.push(...statementCompletions); - replaceBreakStatementInBreakCompletion(statementCompletions, true); - } - - replaceBreakStatementInBreakCompletion(statementCompletions, false); - } else { - completions.push(...statementCompletions); - - if (!context.shouldPopulateBreak) { - replaceBreakStatementInBreakCompletion(statementCompletions, true); - } - } - - break; + if (hasConsequent) { + paths = addCompletionRecords(consequent[consequent.length - 1], paths); + isLastCaseWithConsequent = false; } - - if (i === paths.length - 1) { - completions.push(...statementCompletions); - } else { - lastNormalCompletions = []; - - for (let i = 0; i < statementCompletions.length; i++) { - const c = statementCompletions[i]; - - if (c.type === BREAK_COMPLETION) { - completions.push(c); - } - - if (c.type === NORMAL_COMPLETION) { - lastNormalCompletions.push(c); - } - } - } } - } else if (paths.length) { - for (let i = paths.length - 1; i >= 0; i--) { - const pathCompletions = _getCompletionRecords(paths[i], context); - - if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) { - completions.push(...pathCompletions); - break; - } - } } - return completions; + return paths; } -function _getCompletionRecords(path, context) { - let records = []; +function getCompletionRecords() { + let paths = []; - 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()) { - return addCompletionRecords(path.get("body"), records, context); - } else if (path.isProgram() || path.isBlockStatement()) { - return 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()) { - return addCompletionRecords(path.get("body"), records, context); - } else if (path.isSwitchStatement()) { - return completionRecordForSwitch(path.get("cases"), records, context); - } else if (path.isSwitchCase()) { - return getStatementListCompletion(path.get("consequent"), { - canHaveBreak: true, - shouldPopulateBreak: false, - inCaseClause: true - }); - } else if (path.isBreakStatement()) { - records.push(BreakCompletion(path)); + if (this.isIfStatement()) { + paths = addCompletionRecords(this.get("consequent"), paths); + paths = addCompletionRecords(this.get("alternate"), paths); + } else if (this.isDoExpression() || this.isFor() || this.isWhile()) { + paths = addCompletionRecords(this.get("body"), paths); + } else if (this.isProgram() || this.isBlockStatement()) { + paths = addCompletionRecords(this.get("body").pop(), paths); + } else if (this.isFunction()) { + return this.get("body").getCompletionRecords(); + } else if (this.isTryStatement()) { + paths = addCompletionRecords(this.get("block"), paths); + paths = addCompletionRecords(this.get("handler"), paths); + } else if (this.isCatchClause()) { + paths = addCompletionRecords(this.get("body"), paths); + } else if (this.isSwitchStatement()) { + paths = completionRecordForSwitch(this.get("cases"), paths); } else { - records.push(NormalCompletion(path)); + paths.push(this); } - return records; + return paths; } -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); } @@ -278,11 +160,11 @@ } return siblings; } -function get(key, context = true) { +function get(key, context) { if (context === true) context = this.context; const parts = key.split("."); if (parts.length === 1) { return this._getKey(key, context); @@ -332,27 +214,27 @@ return path; } function getBindingIdentifiers(duplicates) { - return _getBindingIdentifiers(this.node, duplicates); + return t.getBindingIdentifiers(this.node, duplicates); } function getOuterBindingIdentifiers(duplicates) { - return _getOuterBindingIdentifiers(this.node, duplicates); + return t.getOuterBindingIdentifiers(this.node, duplicates); } function getBindingIdentifierPaths(duplicates = false, outerOnly = false) { const path = this; - const search = [path]; + 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 = _getBindingIdentifiers.keys[id.node.type]; + const keys = t.getBindingIdentifiers.keys[id.node.type]; if (id.isIdentifier()) { if (duplicates) { const _ids = ids[id.node.name] = ids[id.node.name] || []; @@ -365,11 +247,11 @@ } if (id.isExportDeclaration()) { const declaration = id.get("declaration"); - if (isDeclaration(declaration)) { + if (declaration.isDeclaration()) { search.push(declaration); } continue; } @@ -388,13 +270,11 @@ if (keys) { for (let i = 0; i < keys.length; i++) { const key = keys[i]; const child = id.get(key); - if (Array.isArray(child)) { - search.push(...child); - } else if (child.node) { - search.push(child); + if (Array.isArray(child) || child.node) { + search = search.concat(child); } } } } \ No newline at end of file