diff --git a/src/fresh/babel.js b/src/fresh/babel.js index 3e7e8047e..5333aa382 100644 --- a/src/fresh/babel.js +++ b/src/fresh/babel.js @@ -202,7 +202,7 @@ export default function(babel) { key: fnHookCalls.map(call => call.name + '{' + call.key + '}').join('\n'), customHooks: fnHookCalls .filter(call => !isBuiltinHook(call.name)) - .map(call => call.callee), + .map(call => t.clone(call.callee)), }; } @@ -217,7 +217,6 @@ export default function(babel) { let seenForRegistration = new WeakSet(); let seenForSignature = new WeakSet(); - let seenForOutro = new WeakSet(); let hookCalls = new WeakMap(); const HookCallsVisitor = { @@ -291,7 +290,7 @@ export default function(babel) { // Make sure we're not mutating the same tree twice. // This can happen if another Babel plugin replaces parents. - if (seenForRegistration.has(node)) { + if (seenForRegistration.has(node)) { return; } seenForRegistration.add(node); @@ -557,38 +556,6 @@ export default function(babel) { // but it's the best we can do until we stop transpiling destructuring. path.traverse(HookCallsVisitor); }, - exit(path) { - return; - const registrations = registrationsByProgramPath.get(path); - if (registrations === undefined) { - return; - } - - // Make sure we're not mutating the same tree twice. - // This can happen if another Babel plugin replaces parents. - const node = path.node; - if (seenForOutro.has(node)) { - return; - } - seenForOutro.add(node); - // Don't mutate the tree above this point. - - registrationsByProgramPath.delete(path); - const declarators = []; - path.pushContainer('body', t.variableDeclaration('var', declarators)); - registrations.forEach(({handle, persistentID}) => { - path.pushContainer( - 'body', - t.expressionStatement( - t.callExpression(t.identifier('__register__'), [ - handle, - t.stringLiteral(persistentID), - ]), - ), - ); - declarators.push(t.variableDeclarator(handle)); - }); - }, }, }, }; diff --git a/test/__babel_fixtures__/hooks.js b/test/__babel_fixtures__/hooks.js index 2e0860e27..14f33945c 100644 --- a/test/__babel_fixtures__/hooks.js +++ b/test/__babel_fixtures__/hooks.js @@ -36,6 +36,12 @@ const useCustomHook = () => { useExternalHook(); }; +const useCustomHookAgain = () => { + useState(42); + useEffectHook(); + useExternalHook(); +}; + function useFunc () { useState(42); useEffectHook(); diff --git a/test/__snapshots__/babel.test.js.snap b/test/__snapshots__/babel.test.js.snap index 0e7748970..50400b03d 100644 --- a/test/__snapshots__/babel.test.js.snap +++ b/test/__snapshots__/babel.test.js.snap @@ -1203,6 +1203,16 @@ __signature__(useCustomHook, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternal return [useEffectHook, _externalHook.useExternalHook]; }); +var useCustomHookAgain = function useCustomHookAgain() { + (0, _react.useState)(42); + useEffectHook(); + (0, _externalHook.useExternalHook)(); +}; + +__signature__(useCustomHookAgain, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternalHook{}', function () { + return [useEffectHook, _externalHook.useExternalHook]; +}); + function useFunc() { (0, _react.useState)(42); useEffectHook(); @@ -1228,6 +1238,7 @@ __signature__(useFunc, 'useState{(42)}\\\\nuseEffectHook{}', function () { reactHotLoader.register(useForwardRefHook, 'useForwardRefHook', __FILENAME__); reactHotLoader.register(useForwardRefFunctionHook, 'useForwardRefFunctionHook', __FILENAME__); reactHotLoader.register(useCustomHook, 'useCustomHook', __FILENAME__); + reactHotLoader.register(useCustomHookAgain, 'useCustomHookAgain', __FILENAME__); reactHotLoader.register(useFunc, 'useFunc', __FILENAME__); })(); @@ -2260,6 +2271,14 @@ const useCustomHook = () => { __signature__(useCustomHook, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternalHook{}', () => [useEffectHook, _externalHook.useExternalHook]); +const useCustomHookAgain = () => { + (0, _react.useState)(42); + useEffectHook(); + (0, _externalHook.useExternalHook)(); +}; + +__signature__(useCustomHookAgain, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternalHook{}', () => [useEffectHook, _externalHook.useExternalHook]); + function useFunc() { (0, _react.useState)(42); useEffectHook(); @@ -2283,6 +2302,7 @@ __signature__(useFunc, 'useState{(42)}\\\\nuseEffectHook{}', () => [useEffectHoo reactHotLoader.register(useForwardRefHook, 'useForwardRefHook', __FILENAME__); reactHotLoader.register(useForwardRefFunctionHook, 'useForwardRefFunctionHook', __FILENAME__); reactHotLoader.register(useCustomHook, 'useCustomHook', __FILENAME__); + reactHotLoader.register(useCustomHookAgain, 'useCustomHookAgain', __FILENAME__); reactHotLoader.register(useFunc, 'useFunc', __FILENAME__); })();