@babel/traverse
Advanced tools
Comparing version 7.13.13 to 7.13.15
@@ -31,2 +31,19 @@ "use strict"; | ||
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() { | ||
@@ -42,100 +59,161 @@ if (this.key === "left") { | ||
function addCompletionRecords(path, paths) { | ||
if (path) return paths.concat(path.getCompletionRecords()); | ||
return paths; | ||
function addCompletionRecords(path, records, context) { | ||
if (path) return records.concat(_getCompletionRecords(path, context)); | ||
return records; | ||
} | ||
function findBreak(statements) { | ||
let breakStatement; | ||
function completionRecordForSwitch(cases, records, context) { | ||
let lastNormalCompletions = []; | ||
if (!Array.isArray(statements)) { | ||
statements = [statements]; | ||
} | ||
for (let i = 0; i < cases.length; i++) { | ||
const casePath = cases[i]; | ||
for (const statement of statements) { | ||
if (statement.isDoExpression() || statement.isProgram() || statement.isBlockStatement() || statement.isCatchClause() || statement.isLabeledStatement()) { | ||
breakStatement = findBreak(statement.get("body")); | ||
} else if (statement.isIfStatement()) { | ||
var _findBreak; | ||
const caseCompletions = _getCompletionRecords(casePath, context); | ||
breakStatement = (_findBreak = findBreak(statement.get("consequent"))) != null ? _findBreak : findBreak(statement.get("alternate")); | ||
} else if (statement.isTryStatement()) { | ||
var _findBreak2; | ||
const normalCompletions = []; | ||
const breakCompletions = []; | ||
breakStatement = (_findBreak2 = findBreak(statement.get("block"))) != null ? _findBreak2 : findBreak(statement.get("handler")); | ||
} else if (statement.isBreakStatement()) { | ||
breakStatement = statement; | ||
for (const c of caseCompletions) { | ||
if (c.type === NORMAL_COMPLETION) { | ||
normalCompletions.push(c); | ||
} | ||
if (c.type === BREAK_COMPLETION) { | ||
breakCompletions.push(c); | ||
} | ||
} | ||
if (breakStatement) { | ||
return breakStatement; | ||
if (normalCompletions.length) { | ||
lastNormalCompletions = normalCompletions; | ||
} | ||
records = records.concat(breakCompletions); | ||
} | ||
return null; | ||
records = records.concat(lastNormalCompletions); | ||
return records; | ||
} | ||
function completionRecordForSwitch(cases, paths) { | ||
let isLastCaseWithConsequent = true; | ||
function normalCompletionToBreak(completions) { | ||
completions.forEach(c => { | ||
c.type = BREAK_COMPLETION; | ||
}); | ||
} | ||
for (let i = cases.length - 1; i >= 0; i--) { | ||
const switchCase = cases[i]; | ||
const consequent = switchCase.get("consequent"); | ||
let breakStatement = findBreak(consequent); | ||
if (breakStatement) { | ||
while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) { | ||
breakStatement = breakStatement.parentPath; | ||
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(); | ||
} | ||
} | ||
}); | ||
} | ||
const prevSibling = breakStatement.getPrevSibling(); | ||
function getStatementListCompletion(paths, context) { | ||
let completions = []; | ||
if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) { | ||
paths = addCompletionRecords(prevSibling, paths); | ||
breakStatement.remove(); | ||
} else { | ||
breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode()); | ||
paths = addCompletionRecords(breakStatement, paths); | ||
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; | ||
} | ||
} 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 = 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 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()) { | ||
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 { | ||
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) { | ||
@@ -142,0 +220,0 @@ return _index.default.get({ |
@@ -197,2 +197,3 @@ "use strict"; | ||
const isParentAsync = functionParent == null ? void 0 : functionParent.is("async"); | ||
const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator"); | ||
const container = t.arrowFunctionExpression([], t.blockStatement(nodes)); | ||
@@ -228,7 +229,19 @@ this.replaceWith(t.callExpression(container, [])); | ||
if (isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES)) { | ||
const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES); | ||
const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", t.FUNCTION_TYPES); | ||
if (needToAwaitFunction) { | ||
callee.set("async", true); | ||
this.replaceWith(t.awaitExpression(this.node)); | ||
if (!needToYieldFunction) { | ||
this.replaceWith(t.awaitExpression(this.node)); | ||
} | ||
} | ||
if (needToYieldFunction) { | ||
callee.set("generator", true); | ||
this.replaceWith(t.yieldExpression(this.node, true)); | ||
} | ||
return callee.get("body.body"); | ||
@@ -235,0 +248,0 @@ } |
{ | ||
"name": "@babel/traverse", | ||
"version": "7.13.13", | ||
"version": "7.13.15", | ||
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes", | ||
@@ -23,4 +23,4 @@ "author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
"@babel/helper-split-export-declaration": "^7.12.13", | ||
"@babel/parser": "^7.13.13", | ||
"@babel/types": "^7.13.13", | ||
"@babel/parser": "^7.13.15", | ||
"@babel/types": "^7.13.14", | ||
"debug": "^4.1.0", | ||
@@ -27,0 +27,0 @@ "globals": "^11.1.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
170254
4631
Updated@babel/parser@^7.13.15
Updated@babel/types@^7.13.14