Skip to content

Commit 516b76b

Browse files
authored
[Fast Refresh] Support callthrough HOCs (#21104)
* [Fast Refresh] Support callthrough HOCs * Add a newly failing testing to demonstrate the flaw This shows why my initial approach doesn't make sense. * Attach signatures at every nesting level * Sign nested memo/forwardRef too * Add an IIFE test This is not a case that is important for Fast Refresh, but we shouldn't change the code semantics. This case shows the transform isn't quite correct. It's wrapping the call at the wrong place. * 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. * Be defensive against non-components being passed to setSignature * Fix lint
1 parent 0853aab commit 516b76b

File tree

5 files changed

+345
-49
lines changed

5 files changed

+345
-49
lines changed

packages/react-refresh/src/ReactFreshBabelPlugin.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,38 @@ export default function(babel, opts = {}) {
333333
return args;
334334
}
335335

336+
function findHOCCallPathsAbove(path) {
337+
const calls = [];
338+
while (true) {
339+
if (!path) {
340+
return calls;
341+
}
342+
const 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+
) {
351+
// Ignore registrations.
352+
path = parentPath;
353+
continue;
354+
}
355+
if (
356+
// hoc1(hoc2(...))
357+
parentPath.node.type === 'CallExpression' &&
358+
path.node !== parentPath.node.callee
359+
) {
360+
calls.push(parentPath);
361+
path = parentPath;
362+
continue;
363+
}
364+
return calls; // Stop at other types.
365+
}
366+
}
367+
336368
const seenForRegistration = new WeakSet();
337369
const seenForSignature = new WeakSet();
338370
const seenForOutro = new WeakSet();
@@ -630,13 +662,16 @@ export default function(babel, opts = {}) {
630662
// Result: let Foo = () => {}; __signature(Foo, ...);
631663
} else {
632664
// let Foo = hoc(() => {})
633-
path.replaceWith(
634-
t.callExpression(
635-
sigCallID,
636-
createArgumentsForSignature(node, signature, path.scope),
637-
),
638-
);
639-
// Result: let Foo = hoc(__signature(() => {}, ...))
665+
const paths = [path, ...findHOCCallPathsAbove(path)];
666+
paths.forEach(p => {
667+
p.replaceWith(
668+
t.callExpression(
669+
sigCallID,
670+
createArgumentsForSignature(p.node, signature, p.scope),
671+
),
672+
);
673+
});
674+
// Result: let Foo = __signature(hoc(__signature(() => {}, ...)), ...)
640675
}
641676
},
642677
},

packages/react-refresh/src/ReactFreshRuntime.js

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,25 @@ export function setSignature(
355355
getCustomHooks?: () => Array<Function>,
356356
): void {
357357
if (__DEV__) {
358-
allSignaturesByType.set(type, {
359-
forceReset,
360-
ownKey: key,
361-
fullKey: null,
362-
getCustomHooks: getCustomHooks || (() => []),
363-
});
358+
if (!allSignaturesByType.has(type)) {
359+
allSignaturesByType.set(type, {
360+
forceReset,
361+
ownKey: key,
362+
fullKey: null,
363+
getCustomHooks: getCustomHooks || (() => []),
364+
});
365+
}
366+
// Visit inner types because we might not have signed them.
367+
if (typeof type === 'object' && type !== null) {
368+
switch (getProperty(type, '$$typeof')) {
369+
case REACT_FORWARD_REF_TYPE:
370+
setSignature(type.render, key, forceReset, getCustomHooks);
371+
break;
372+
case REACT_MEMO_TYPE:
373+
setSignature(type.type, key, forceReset, getCustomHooks);
374+
break;
375+
}
376+
}
364377
} else {
365378
throw new Error(
366379
'Unexpected call to React Refresh in a production environment.',
@@ -609,57 +622,58 @@ export function _getMountedRootCount() {
609622
// function Hello() {
610623
// const [foo, setFoo] = useState(0);
611624
// const value = useCustomHook();
612-
// _s(); /* Second call triggers collecting the custom Hook list.
625+
// _s(); /* Call without arguments triggers collecting the custom Hook list.
613626
// * This doesn't happen during the module evaluation because we
614627
// * don't want to change the module order with inline requires.
615628
// * Next calls are noops. */
616629
// return <h1>Hi</h1>;
617630
// }
618631
//
619-
// /* First call specifies the signature: */
632+
// /* Call with arguments attaches the signature to the type: */
620633
// _s(
621634
// Hello,
622635
// 'useState{[foo, setFoo]}(0)',
623636
// () => [useCustomHook], /* Lazy to avoid triggering inline requires */
624637
// );
625-
type SignatureStatus = 'needsSignature' | 'needsCustomHooks' | 'resolved';
626638
export function createSignatureFunctionForTransform() {
627639
if (__DEV__) {
628-
// We'll fill in the signature in two steps.
629-
// First, we'll know the signature itself. This happens outside the component.
630-
// Then, we'll know the references to custom Hooks. This happens inside the component.
631-
// After that, the returned function will be a fast path no-op.
632-
let status: SignatureStatus = 'needsSignature';
633640
let savedType;
634641
let hasCustomHooks;
642+
let didCollectHooks = false;
635643
return function<T>(
636644
type: T,
637645
key: string,
638646
forceReset?: boolean,
639647
getCustomHooks?: () => Array<Function>,
640-
): T {
641-
switch (status) {
642-
case 'needsSignature':
643-
if (type !== undefined) {
644-
// If we received an argument, this is the initial registration call.
645-
savedType = type;
646-
hasCustomHooks = typeof getCustomHooks === 'function';
647-
setSignature(type, key, forceReset, getCustomHooks);
648-
// The next call we expect is from inside a function, to fill in the custom Hooks.
649-
status = 'needsCustomHooks';
650-
}
651-
break;
652-
case 'needsCustomHooks':
653-
if (hasCustomHooks) {
654-
collectCustomHooksForSignature(savedType);
655-
}
656-
status = 'resolved';
657-
break;
658-
case 'resolved':
659-
// Do nothing. Fast path for all future renders.
660-
break;
648+
): T | void {
649+
if (typeof key === 'string') {
650+
// We're in the initial phase that associates signatures
651+
// with the functions. Note this may be called multiple times
652+
// in HOC chains like _s(hoc1(_s(hoc2(_s(actualFunction))))).
653+
if (!savedType) {
654+
// We're in the innermost call, so this is the actual type.
655+
savedType = type;
656+
hasCustomHooks = typeof getCustomHooks === 'function';
657+
}
658+
// Set the signature for all types (even wrappers!) in case
659+
// they have no signatures of their own. This is to prevent
660+
// problems like https://github.com/facebook/react/issues/20417.
661+
if (
662+
type != null &&
663+
(typeof type === 'function' || typeof type === 'object')
664+
) {
665+
setSignature(type, key, forceReset, getCustomHooks);
666+
}
667+
return type;
668+
} else {
669+
// We're in the _s() call without arguments, which means
670+
// this is the time to collect custom Hook signatures.
671+
// Only do this once. This path is hot and runs *inside* every render!
672+
if (!didCollectHooks && hasCustomHooks) {
673+
didCollectHooks = true;
674+
collectCustomHooksForSignature(savedType);
675+
}
661676
}
662-
return type;
663677
};
664678
} else {
665679
throw new Error(

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,4 +524,16 @@ describe('ReactFreshBabelPlugin', () => {
524524
'". If you want to override this check, pass {skipEnvCheck: true} as plugin options.',
525525
);
526526
});
527+
528+
it('does not get tripped by IIFEs', () => {
529+
expect(
530+
transform(`
531+
while (item) {
532+
(item => {
533+
useFoo();
534+
})(item);
535+
}
536+
`),
537+
).toMatchSnapshot();
538+
});
527539
});

0 commit comments

Comments
 (0)