babel-plugin-jest-hoist
Advanced tools
Comparing version 30.0.0-alpha.2 to 30.0.0-alpha.3
@@ -115,5 +115,4 @@ /*! | ||
} | ||
if (!isAllowedIdentifier) { | ||
throw id.buildCodeFrameError('The module factory of `jest.mock()` is not allowed to ' + 'reference any out-of-scope variables.\n' + `Invalid variable access: ${name}\n` + `Allowed objects: ${Array.from(ALLOWED_IDENTIFIERS).join(', ')}.\n` + 'Note: This is a precaution to guard against uninitialized mock ' + 'variables. If it is ensured that the mock is required lazily, ' + 'variable names prefixed with `mock` (case insensitive) are permitted.\n', ReferenceError); | ||
throw id.buildCodeFrameError('The module factory of `jest.mock()` is not allowed to ' + 'reference any out-of-scope variables.\n' + `Invalid variable access: ${name}\n` + `Allowed objects: ${[...ALLOWED_IDENTIFIERS].join(', ')}.\n` + 'Note: This is a precaution to guard against uninitialized mock ' + 'variables. If it is ensured that the mock is required lazily, ' + 'variable names prefixed with `mock` (case insensitive) are permitted.\n', ReferenceError); | ||
} | ||
@@ -190,2 +189,43 @@ } | ||
}; | ||
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])); | ||
} | ||
} | ||
} | ||
@@ -231,43 +271,2 @@ /* eslint-disable sort-keys */ | ||
}); | ||
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])); | ||
} | ||
} | ||
} | ||
} | ||
@@ -274,0 +273,0 @@ }; |
{ | ||
"name": "babel-plugin-jest-hoist", | ||
"version": "30.0.0-alpha.2", | ||
"version": "30.0.0-alpha.3", | ||
"repository": { | ||
@@ -44,3 +44,3 @@ "type": "git", | ||
}, | ||
"gitHead": "c04d13d7abd22e47b0997f6027886aed225c9ce4" | ||
"gitHead": "e267aff33d105399f2134bad7c8f82285104f3da" | ||
} |
14097