babel-plugin-jest-hoist
Advanced tools
Comparing version 29.4.3 to 29.5.0
@@ -33,2 +33,3 @@ 'use strict'; | ||
const hoistedVariables = new WeakSet(); | ||
const hoistedJestGetters = new WeakSet(); | ||
const hoistedJestExpressions = new WeakSet(); | ||
@@ -247,3 +248,3 @@ | ||
: // The Jest object could be returned from another call since the functions are all chainable. | ||
extractJestObjExprIfHoistable(object); | ||
extractJestObjExprIfHoistable(object)?.path; | ||
if (!jestObjExpr) { | ||
@@ -256,9 +257,10 @@ return null; | ||
// which should only happen if we're already sure it's a call on the Jest object. | ||
let functionLooksHoistableOrInHoistable = FUNCTIONS[propertyName]?.(args); | ||
const functionIsHoistable = FUNCTIONS[propertyName]?.(args) ?? false; | ||
let functionHasHoistableScope = functionIsHoistable; | ||
for ( | ||
let path = expr; | ||
path && !functionLooksHoistableOrInHoistable; | ||
path && !functionHasHoistableScope; | ||
path = path.parentPath | ||
) { | ||
functionLooksHoistableOrInHoistable = hoistedJestExpressions.has( | ||
functionHasHoistableScope = hoistedJestExpressions.has( | ||
// @ts-expect-error: it's ok if path.node is not an Expression, .has will | ||
@@ -269,5 +271,8 @@ // just return false. | ||
} | ||
if (functionLooksHoistableOrInHoistable) { | ||
if (functionHasHoistableScope) { | ||
hoistedJestExpressions.add(expr.node); | ||
return jestObjExpr; | ||
return { | ||
hoist: functionIsHoistable, | ||
path: jestObjExpr | ||
}; | ||
} | ||
@@ -299,12 +304,14 @@ return null; | ||
ExpressionStatement(exprStmt) { | ||
const jestObjExpr = extractJestObjExprIfHoistable( | ||
const jestObjInfo = extractJestObjExprIfHoistable( | ||
exprStmt.get('expression') | ||
); | ||
if (jestObjExpr) { | ||
jestObjExpr.replaceWith( | ||
(0, _types().callExpression)( | ||
this.declareJestObjGetterIdentifier(), | ||
[] | ||
) | ||
if (jestObjInfo) { | ||
const jestCallExpr = (0, _types().callExpression)( | ||
this.declareJestObjGetterIdentifier(), | ||
[] | ||
); | ||
jestObjInfo.path.replaceWith(jestCallExpr); | ||
if (jestObjInfo.hoist) { | ||
hoistedJestGetters.add(jestCallExpr); | ||
} | ||
} | ||
@@ -315,4 +322,2 @@ } | ||
post({path: program}) { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const self = this; | ||
visitBlock(program); | ||
@@ -337,9 +342,3 @@ program.traverse({ | ||
function visitCallExpr(callExpr) { | ||
const { | ||
node: {callee} | ||
} = callExpr; | ||
if ( | ||
(0, _types().isIdentifier)(callee) && | ||
callee.name === self.jestObjGetterIdentifier?.name | ||
) { | ||
if (hoistedJestGetters.has(callExpr.node)) { | ||
const mockStmt = callExpr.getStatementParent(); | ||
@@ -346,0 +345,0 @@ if (mockStmt) { |
{ | ||
"name": "babel-plugin-jest-hoist", | ||
"version": "29.4.3", | ||
"version": "29.5.0", | ||
"repository": { | ||
@@ -41,3 +41,3 @@ "type": "git", | ||
}, | ||
"gitHead": "a49c88610e49a3242576160740a32a2fe11161e1" | ||
"gitHead": "39f3beda6b396665bebffab94e8d7c45be30454c" | ||
} |
14335
375