Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 42 additions & 53 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
import {statement} from '@babel/template';
import type {NodePath} from '@babel/traverse';
import {
type BlockStatement,
type CallExpression,
type EmptyStatement,
type Expression,
type Identifier,
type ImportDeclaration,
type MemberExpression,
type Node,
type Program,
type Statement,
type Super,
type VariableDeclaration,
type VariableDeclarator,
callExpression,
emptyStatement,
isIdentifier,
variableDeclaration,
} from '@babel/types';
Expand Down Expand Up @@ -312,53 +309,6 @@
return null;
};

function visitBlock(block: NodePath<BlockStatement> | NodePath<Program>) {
// use a temporary empty statement instead of the real first statement, which may itself be hoisted
const [varsHoistPoint, callsHoistPoint] = block.unshiftContainer<
BlockStatement | Program,
'body',
[EmptyStatement, EmptyStatement]
>('body', [emptyStatement(), 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: NodePath<CallExpression>) {
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: NodePath<VariableDeclarator>) {
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 as VariableDeclaration;
if (declarations.length === 1) {
varDecl.parentPath.remove();
} else {
varDecl.remove();
}
varsHoistPoint.insertBefore(variableDeclaration(kind, [varDecl.node]));
}
}
}

/* eslint-disable sort-keys */
export default function jestHoist(): PluginObj<{
declareJestObjGetterIdentifier: () => Identifier;
Expand Down Expand Up @@ -404,8 +354,47 @@
},
// in `post` to make sure we come after an import transform and can unshift above the `require`s
post({path: program}) {
visitBlock(program);
program.traverse({BlockStatement: visitBlock});
type Item = {calls: Array<Statement>; vars: Array<Statement>};

const stack: Array<Item> = [{calls: [], vars: []}];
program.traverse({
BlockStatement: {
enter() {
stack.push({calls: [], vars: []});
},
exit(path) {
const item = stack.pop()!;
path.node.body.unshift(...item.vars, ...item.calls);
},
},
CallExpression(callExpr: NodePath<CallExpression>) {
if (hoistedJestGetters.has(callExpr.node)) {
const mockStmt = callExpr.getStatementParent();

// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions

Check failure on line 374 in packages/babel-plugin-jest-hoist/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

'@typescript-eslint/strict-boolean-expressions' rule is disabled but never reported
if (mockStmt?.parentPath.isBlock()) {
stack.at(-1)!.calls.push(mockStmt.node);
mockStmt.remove();
}
}
},
VariableDeclarator(varDecl: NodePath<VariableDeclarator>) {
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 as VariableDeclaration;
if (declarations.length === 1) {
varDecl.parentPath.remove();
} else {
varDecl.remove();
}
stack.at(-1)!.vars.push(variableDeclaration(kind, [varDecl.node]));
}
},
});
const item = stack.pop()!;
program.node.body.unshift(...item.vars, ...item.calls);
},
};
}
Expand Down
Loading