From 9e088f1a5791bba2523dda2e002ee234f72a40fe Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 1 Nov 2020 02:03:29 +0100 Subject: [PATCH] replace variable reference with constant value --- .../src/__tests__/__snapshots__/hoistPlugin.test.ts.snap | 2 +- packages/babel-plugin-jest-hoist/src/index.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap b/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap index bb0deabba40c..227860db32e3 100644 --- a/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap +++ b/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap @@ -15,7 +15,7 @@ _getJestObj().mock("./App", () => () => void 0, false, { - fileName: _jsxFileName, + fileName: "/root/project/src/file.js", lineNumber: 1, columnNumber: 32 }, diff --git a/packages/babel-plugin-jest-hoist/src/index.ts b/packages/babel-plugin-jest-hoist/src/index.ts index 6d02aefefefa..f952746674e6 100644 --- a/packages/babel-plugin-jest-hoist/src/index.ts +++ b/packages/babel-plugin-jest-hoist/src/index.ts @@ -146,7 +146,9 @@ FUNCTIONS.mock = args => { const initNode = binding.path.node.init; if (initNode && binding.constant && scope.isPure(initNode, true)) { - // how to hoist??? + // replace the reference with its constant value + id.replaceWith(initNode); + isAllowedIdentifier = true; } } @@ -286,7 +288,7 @@ export default (): PluginObj<{ visitor: { ExpressionStatement(exprStmt) { const jestObjExpr = extractJestObjExprIfHoistable( - exprStmt.get<'expression'>('expression'), + exprStmt.get('expression'), ); if (jestObjExpr) { jestObjExpr.replaceWith(