Skip to content

Commit 070262b

Browse files
committed
Find HOCs above more precisely
This fixes a false positive that was causing an IIFE to be wrapped in the wrong place, which made the wrapping unsafe.
1 parent d2a2045 commit 070262b

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

packages/react-refresh/src/ReactFreshBabelPlugin.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,26 @@ export default function(babel, opts = {}) {
339339
if (!path) {
340340
return calls;
341341
}
342-
if (path.node.type === 'AssignmentExpression') {
342+
let parentPath = path.parentPath;
343+
if (!parentPath) {
344+
return calls;
345+
}
346+
if (
347+
// hoc(_c = function() { })
348+
parentPath.node.type === 'AssignmentExpression' &&
349+
path.node === parentPath.node.right
350+
) {
343351
// Ignore registrations.
344-
path = path.parentPath;
352+
path = parentPath;
345353
continue;
346354
}
347-
if (path.node.type === 'CallExpression') {
348-
calls.push(path);
349-
path = path.parentPath;
355+
if (
356+
// hoc1(hoc2(...))
357+
parentPath.node.type === 'CallExpression' &&
358+
path.node !== parentPath.node.callee
359+
) {
360+
calls.push(parentPath);
361+
path = parentPath;
350362
continue;
351363
}
352364
return calls; // Stop at other types.
@@ -650,7 +662,7 @@ export default function(babel, opts = {}) {
650662
// Result: let Foo = () => {}; __signature(Foo, ...);
651663
} else {
652664
// let Foo = hoc(() => {})
653-
const paths = [path, ...findHOCCallPathsAbove(path.parentPath)];
665+
const paths = [path, ...findHOCCallPathsAbove(path)];
654666
paths.forEach(p => {
655667
p.replaceWith(
656668
t.callExpression(

packages/react-refresh/src/__tests__/__snapshots__/ReactFreshBabelPlugin-test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ exports[`ReactFreshBabelPlugin does not get tripped by IIFEs 1`] = `
9090
while (item) {
9191
var _s = $RefreshSig$();
9292
93-
_s(_s(item => {
93+
_s(item => {
9494
_s();
9595
9696
useFoo();
97-
}, "useFoo{}", true)(item), "useFoo{}", true);
97+
}, "useFoo{}", true)(item);
9898
}
9999
`;
100100

0 commit comments

Comments
 (0)