Skip to content

Commit 9b91b62

Browse files
committed
add VariableDeclaration visitor
1 parent 8e1319b commit 9b91b62

File tree

1 file changed

+30
-0
lines changed
  • packages/babel-plugin-jest-hoist/src

1 file changed

+30
-0
lines changed

packages/babel-plugin-jest-hoist/src/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,36 @@ export default (): {visitor: Visitor} => {
189189
path.node._blockHoist = Infinity;
190190
}
191191
},
192+
VariableDeclaration(path) {
193+
const declarations = path.get('declarations');
194+
195+
if (declarations.length === 1) {
196+
const declarationInit = declarations[0].get('init');
197+
198+
if (declarationInit.isCallExpression()) {
199+
const callee = declarationInit.get('callee') as NodePath;
200+
const callArguments = declarationInit.get('arguments') as Array<
201+
NodePath
202+
>;
203+
204+
if (
205+
callee.isIdentifier() &&
206+
callee.node.name === 'require' &&
207+
callArguments.length === 1
208+
) {
209+
const [argument] = callArguments;
210+
211+
if (
212+
argument.isStringLiteral() &&
213+
argument.node.value === '@jest/globals'
214+
) {
215+
// @ts-ignore: private, magical property
216+
path.node._blockHoist = Infinity;
217+
}
218+
}
219+
}
220+
}
221+
},
192222
};
193223

194224
return {visitor};

0 commit comments

Comments
 (0)