babel-plugin-jest-hoist
Advanced tools
+1
-2
@@ -10,6 +10,5 @@ /** | ||
| import { Identifier } from '@babel/types'; | ||
| declare const _default: () => PluginObj<{ | ||
| export default function jestHoist(): PluginObj<{ | ||
| declareJestObjGetterIdentifier: () => Identifier; | ||
| jestObjGetterIdentifier?: Identifier; | ||
| }>; | ||
| export default _default; |
+92
-92
@@ -6,3 +6,3 @@ 'use strict'; | ||
| }); | ||
| exports.default = void 0; | ||
| exports.default = jestHoist; | ||
@@ -279,113 +279,113 @@ function _template() { | ||
| var _default = () => ({ | ||
| pre({path: program}) { | ||
| this.declareJestObjGetterIdentifier = () => { | ||
| if (this.jestObjGetterIdentifier) { | ||
| function jestHoist() { | ||
| return { | ||
| pre({path: program}) { | ||
| this.declareJestObjGetterIdentifier = () => { | ||
| if (this.jestObjGetterIdentifier) { | ||
| return this.jestObjGetterIdentifier; | ||
| } | ||
| this.jestObjGetterIdentifier = | ||
| program.scope.generateUidIdentifier('getJestObj'); | ||
| program.unshiftContainer('body', [ | ||
| createJestObjectGetter({ | ||
| GETTER_NAME: this.jestObjGetterIdentifier.name, | ||
| JEST_GLOBALS_MODULE_JEST_EXPORT_NAME, | ||
| JEST_GLOBALS_MODULE_NAME | ||
| }) | ||
| ]); | ||
| return this.jestObjGetterIdentifier; | ||
| } | ||
| }; | ||
| }, | ||
| this.jestObjGetterIdentifier = | ||
| program.scope.generateUidIdentifier('getJestObj'); | ||
| program.unshiftContainer('body', [ | ||
| createJestObjectGetter({ | ||
| GETTER_NAME: this.jestObjGetterIdentifier.name, | ||
| JEST_GLOBALS_MODULE_JEST_EXPORT_NAME, | ||
| JEST_GLOBALS_MODULE_NAME | ||
| }) | ||
| ]); | ||
| return this.jestObjGetterIdentifier; | ||
| }; | ||
| }, | ||
| visitor: { | ||
| ExpressionStatement(exprStmt) { | ||
| const jestObjExpr = extractJestObjExprIfHoistable( | ||
| exprStmt.get('expression') | ||
| ); | ||
| visitor: { | ||
| ExpressionStatement(exprStmt) { | ||
| const jestObjExpr = extractJestObjExprIfHoistable( | ||
| exprStmt.get('expression') | ||
| ); | ||
| if (jestObjExpr) { | ||
| jestObjExpr.replaceWith( | ||
| (0, _types().callExpression)( | ||
| this.declareJestObjGetterIdentifier(), | ||
| [] | ||
| ) | ||
| ); | ||
| if (jestObjExpr) { | ||
| jestObjExpr.replaceWith( | ||
| (0, _types().callExpression)( | ||
| this.declareJestObjGetterIdentifier(), | ||
| [] | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| }, | ||
| // in `post` to make sure we come after an import transform and can unshift above the `require`s | ||
| post({path: program}) { | ||
| const self = this; | ||
| visitBlock(program); | ||
| program.traverse({ | ||
| BlockStatement: visitBlock | ||
| }); | ||
| 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 | ||
| blacklist: ['BlockStatement'] | ||
| // in `post` to make sure we come after an import transform and can unshift above the `require`s | ||
| post({path: program}) { | ||
| const self = this; | ||
| visitBlock(program); | ||
| program.traverse({ | ||
| BlockStatement: visitBlock | ||
| }); | ||
| callsHoistPoint.remove(); | ||
| varsHoistPoint.remove(); | ||
| function visitCallExpr(callExpr) { | ||
| var _self$jestObjGetterId; | ||
| 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 | ||
| blacklist: ['BlockStatement'] | ||
| }); | ||
| callsHoistPoint.remove(); | ||
| varsHoistPoint.remove(); | ||
| const { | ||
| node: {callee} | ||
| } = callExpr; | ||
| function visitCallExpr(callExpr) { | ||
| var _self$jestObjGetterId; | ||
| if ( | ||
| (0, _types().isIdentifier)(callee) && | ||
| callee.name === | ||
| ((_self$jestObjGetterId = self.jestObjGetterIdentifier) === null || | ||
| _self$jestObjGetterId === void 0 | ||
| ? void 0 | ||
| : _self$jestObjGetterId.name) | ||
| ) { | ||
| const mockStmt = callExpr.getStatementParent(); | ||
| const { | ||
| node: {callee} | ||
| } = callExpr; | ||
| if (mockStmt) { | ||
| const mockStmtParent = mockStmt.parentPath; | ||
| if ( | ||
| (0, _types().isIdentifier)(callee) && | ||
| callee.name === | ||
| ((_self$jestObjGetterId = self.jestObjGetterIdentifier) === | ||
| null || _self$jestObjGetterId === void 0 | ||
| ? void 0 | ||
| : _self$jestObjGetterId.name) | ||
| ) { | ||
| const mockStmt = callExpr.getStatementParent(); | ||
| if (mockStmtParent.isBlock()) { | ||
| const mockStmtNode = mockStmt.node; | ||
| mockStmt.remove(); | ||
| callsHoistPoint.insertBefore(mockStmtNode); | ||
| 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; | ||
| 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(); | ||
| if (declarations.length === 1) { | ||
| varDecl.parentPath.remove(); | ||
| } else { | ||
| varDecl.remove(); | ||
| } | ||
| varsHoistPoint.insertBefore( | ||
| (0, _types().variableDeclaration)(kind, [varDecl.node]) | ||
| ); | ||
| } | ||
| varsHoistPoint.insertBefore( | ||
| (0, _types().variableDeclaration)(kind, [varDecl.node]) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
| /* eslint-enable */ | ||
| exports.default = _default; |
+2
-2
| { | ||
| "name": "babel-plugin-jest-hoist", | ||
| "version": "27.5.0", | ||
| "version": "27.5.1", | ||
| "repository": { | ||
@@ -40,3 +40,3 @@ "type": "git", | ||
| }, | ||
| "gitHead": "247cbe6026a590deaf0d23edecc7b2779a4aace9" | ||
| "gitHead": "67c1aa20c5fec31366d733e901fee2b981cb1850" | ||
| } |
13673
1.18%