babel-plugin-jest-hoist
Advanced tools
Comparing version 30.0.0-alpha.6 to 30.0.0-alpha.7
@@ -12,3 +12,3 @@ /*! | ||
var __webpack_exports__ = {}; | ||
// This entry need to be wrapped in an IIFE because it uses a non-standard name for the exports (exports). | ||
// This entry needs to be wrapped in an IIFE because it uses a non-standard name for the exports (exports). | ||
(() => { | ||
@@ -189,43 +189,2 @@ var exports = __webpack_exports__; | ||
}; | ||
function visitBlock(block) { | ||
// use a temporary empty statement instead of the real first statement, which may itself be hoisted | ||
const [varsHoistPoint, callsHoistPoint] = block.unshiftContainer('body', [(0, _types().emptyStatement)(), (0, _types().emptyStatement)()]); | ||
block.traverse({ | ||
CallExpression: visitCallExpr, | ||
VariableDeclarator: visitVariableDeclarator, | ||
// do not traverse into nested blocks, or we'll hoist calls in there out to this block | ||
denylist: ['BlockStatement'] | ||
}); | ||
callsHoistPoint.remove(); | ||
varsHoistPoint.remove(); | ||
function visitCallExpr(callExpr) { | ||
if (hoistedJestGetters.has(callExpr.node)) { | ||
const mockStmt = callExpr.getStatementParent(); | ||
if (mockStmt) { | ||
const mockStmtParent = mockStmt.parentPath; | ||
if (mockStmtParent.isBlock()) { | ||
const mockStmtNode = mockStmt.node; | ||
mockStmt.remove(); | ||
callsHoistPoint.insertBefore(mockStmtNode); | ||
} | ||
} | ||
} | ||
} | ||
function visitVariableDeclarator(varDecl) { | ||
if (hoistedVariables.has(varDecl.node)) { | ||
// should be assert function, but it's not. So let's cast below | ||
varDecl.parentPath.assertVariableDeclaration(); | ||
const { | ||
kind, | ||
declarations | ||
} = varDecl.parent; | ||
if (declarations.length === 1) { | ||
varDecl.parentPath.remove(); | ||
} else { | ||
varDecl.remove(); | ||
} | ||
varsHoistPoint.insertBefore((0, _types().variableDeclaration)(kind, [varDecl.node])); | ||
} | ||
} | ||
} | ||
@@ -267,6 +226,47 @@ /* eslint-disable sort-keys */ | ||
}) { | ||
visitBlock(program); | ||
const stack = [{ | ||
calls: [], | ||
vars: [] | ||
}]; | ||
program.traverse({ | ||
BlockStatement: visitBlock | ||
BlockStatement: { | ||
enter() { | ||
stack.push({ | ||
calls: [], | ||
vars: [] | ||
}); | ||
}, | ||
exit(path) { | ||
const item = stack.pop(); | ||
path.node.body.unshift(...item.vars, ...item.calls); | ||
} | ||
}, | ||
CallExpression(callExpr) { | ||
if (hoistedJestGetters.has(callExpr.node)) { | ||
const mockStmt = callExpr.getStatementParent(); | ||
if (mockStmt && mockStmt.parentPath.isBlock()) { | ||
stack.at(-1).calls.push(mockStmt.node); | ||
mockStmt.remove(); | ||
} | ||
} | ||
}, | ||
VariableDeclarator(varDecl) { | ||
if (hoistedVariables.has(varDecl.node)) { | ||
// should be assert function, but it's not. So let's cast below | ||
varDecl.parentPath.assertVariableDeclaration(); | ||
const { | ||
kind, | ||
declarations | ||
} = varDecl.parent; | ||
if (declarations.length === 1) { | ||
varDecl.parentPath.remove(); | ||
} else { | ||
varDecl.remove(); | ||
} | ||
stack.at(-1).vars.push((0, _types().variableDeclaration)(kind, [varDecl.node])); | ||
} | ||
} | ||
}); | ||
const item = stack.pop(); | ||
program.node.body.unshift(...item.vars, ...item.calls); | ||
} | ||
@@ -273,0 +273,0 @@ }; |
{ | ||
"name": "babel-plugin-jest-hoist", | ||
"version": "30.0.0-alpha.6", | ||
"version": "30.0.0-alpha.7", | ||
"repository": { | ||
@@ -43,3 +43,3 @@ "type": "git", | ||
}, | ||
"gitHead": "ba74b7de1b9cca88daf33f9d1b46bfe2b7f485a5" | ||
"gitHead": "bacb7de30d053cd87181294b0c8a8576632a8b02" | ||
} |
13945