babel-plugin-jest-hoist
Advanced tools
Comparing version 29.0.0 to 29.0.2
@@ -38,3 +38,4 @@ 'use strict'; | ||
const JEST_GLOBALS_MODULE_JEST_EXPORT_NAME = 'jest'; | ||
const hoistedVariables = new WeakSet(); // We allow `jest`, `expect`, `require`, all default Node.js globals and all | ||
const hoistedVariables = new WeakSet(); | ||
const hoistedJestExpressions = new WeakSet(); // We allow `jest`, `expect`, `require`, all default Node.js globals and all | ||
// ES2015 built-ins to be used inside of a `jest.mock` factory. | ||
@@ -164,2 +165,15 @@ // We also allow variables prefixed with `mock` as an escape-hatch. | ||
} | ||
} else if (binding?.path.isImportSpecifier()) { | ||
const importDecl = binding.path.parentPath; | ||
const imported = binding.path.node.imported; | ||
if ( | ||
importDecl.node.source.value === JEST_GLOBALS_MODULE_NAME && | ||
((0, _types().isIdentifier)(imported) | ||
? imported.name | ||
: imported.value) === JEST_GLOBALS_MODULE_JEST_EXPORT_NAME | ||
) { | ||
isAllowedIdentifier = true; // Imports are already hoisted, so we don't need to add it | ||
// to hoistedVariables. | ||
} | ||
} | ||
@@ -263,4 +277,22 @@ } | ||
const functionLooksHoistable = FUNCTIONS[propertyName]?.(args); | ||
return functionLooksHoistable ? jestObjExpr : null; | ||
let functionLooksHoistableOrInHoistable = FUNCTIONS[propertyName]?.(args); | ||
for ( | ||
let path = expr; | ||
path && !functionLooksHoistableOrInHoistable; | ||
path = path.parentPath | ||
) { | ||
functionLooksHoistableOrInHoistable = hoistedJestExpressions.has( | ||
// @ts-expect-error: it's ok if path.node is not an Expression, .has will | ||
// just return false. | ||
path.node | ||
); | ||
} | ||
if (functionLooksHoistableOrInHoistable) { | ||
hoistedJestExpressions.add(expr.node); | ||
return jestObjExpr; | ||
} | ||
return null; | ||
}; | ||
@@ -267,0 +299,0 @@ /* eslint-disable sort-keys */ |
{ | ||
"name": "babel-plugin-jest-hoist", | ||
"version": "29.0.0", | ||
"version": "29.0.2", | ||
"repository": { | ||
@@ -40,3 +40,3 @@ "type": "git", | ||
}, | ||
"gitHead": "75006e46c76f6fda14bbc0548f86edb2ba087cd2" | ||
"gitHead": "616fcf56bb8481d29ba29cc34be32a92b1cf85e5" | ||
} |
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
14304
363