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

- old
+ new

@@ -16,118 +16,236 @@ exports.getBindingIdentifiers = getBindingIdentifiers; exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers; exports.getBindingIdentifierPaths = getBindingIdentifierPaths; exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths; -var _index = _interopRequireDefault(require("./index")); +var _index = require("./index"); -var t = _interopRequireWildcard(require("@babel/types")); +var _t = require("@babel/types"); -function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } +const { + getBindingIdentifiers: _getBindingIdentifiers, + getOuterBindingIdentifiers: _getOuterBindingIdentifiers, + isDeclaration, + numericLiteral, + unaryExpression +} = _t; +const NORMAL_COMPLETION = 0; +const BREAK_COMPLETION = 1; -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 NormalCompletion(path) { + return { + type: NORMAL_COMPLETION, + path + }; +} -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } +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, paths) { - if (path) return paths.concat(path.getCompletionRecords()); - return paths; +function addCompletionRecords(path, records, context) { + if (path) { + records.push(..._getCompletionRecords(path, context)); + } + + return records; } -function completionRecordForSwitch(cases, paths) { - let isLastCaseWithConsequent = true; +function completionRecordForSwitch(cases, records, context) { + let lastNormalCompletions = []; - for (let i = cases.length - 1; i >= 0; i--) { - const switchCase = cases[i]; - const consequent = switchCase.get("consequent"); - let breakStatement; + for (let i = 0; i < cases.length; i++) { + const casePath = cases[i]; - 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; + 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 (breakStatement) { - while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) { - breakStatement = breakStatement.parentPath; + 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(); } + } + }); +} - const prevSibling = breakStatement.getPrevSibling(); +function getStatementListCompletion(paths, context) { + const completions = []; - if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) { - paths = addCompletionRecords(prevSibling, paths); - breakStatement.remove(); + 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 { - breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode()); - paths = addCompletionRecords(breakStatement, paths); + newContext.shouldPopulateBreak = false; } - } else if (isLastCaseWithConsequent) { - const statementFinder = statement => !statement.isBlockStatement() || statement.get("body").some(statementFinder); - const hasConsequent = consequent.some(statementFinder); + const statementCompletions = _getCompletionRecords(path, newContext); - if (hasConsequent) { - paths = addCompletionRecords(consequent[consequent.length - 1], paths); - isLastCaseWithConsequent = false; + 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 (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 paths; + return completions; } -function getCompletionRecords() { - let paths = []; +function _getCompletionRecords(path, context) { + let records = []; - 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); + 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)); } else { - paths.push(this); + records.push(NormalCompletion(path)); } - return paths; + 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); } @@ -160,11 +278,11 @@ } return siblings; } -function get(key, context) { +function get(key, context = true) { if (context === true) context = this.context; const parts = key.split("."); if (parts.length === 1) { return this._getKey(key, context); @@ -214,27 +332,27 @@ return path; } function getBindingIdentifiers(duplicates) { - return t.getBindingIdentifiers(this.node, duplicates); + return _getBindingIdentifiers(this.node, duplicates); } function getOuterBindingIdentifiers(duplicates) { - return t.getOuterBindingIdentifiers(this.node, duplicates); + return _getOuterBindingIdentifiers(this.node, duplicates); } function getBindingIdentifierPaths(duplicates = false, outerOnly = false) { const path = this; - let search = [].concat(path); + const search = [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]; + const keys = _getBindingIdentifiers.keys[id.node.type]; if (id.isIdentifier()) { if (duplicates) { const _ids = ids[id.node.name] = ids[id.node.name] || []; @@ -247,11 +365,11 @@ } if (id.isExportDeclaration()) { const declaration = id.get("declaration"); - if (declaration.isDeclaration()) { + if (isDeclaration(declaration)) { search.push(declaration); } continue; } @@ -270,11 +388,13 @@ 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); + if (Array.isArray(child)) { + search.push(...child); + } else if (child.node) { + search.push(child); } } } } \ No newline at end of file