Skip to content

Commit

Permalink
fix: clone node for signature, fixes #1268
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jun 29, 2019
1 parent 70353e5 commit ed3e1d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/fresh/babel.js
Expand Up @@ -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)),
};
}

Expand All @@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});
},
},
},
};
Expand Down
6 changes: 6 additions & 0 deletions test/__babel_fixtures__/hooks.js
Expand Up @@ -36,6 +36,12 @@ const useCustomHook = () => {
useExternalHook();
};

const useCustomHookAgain = () => {
useState(42);
useEffectHook();
useExternalHook();
};

function useFunc () {
useState(42);
useEffectHook();
Expand Down
20 changes: 20 additions & 0 deletions test/__snapshots__/babel.test.js.snap
Expand Up @@ -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();
Expand All @@ -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__);
})();
Expand Down Expand Up @@ -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();
Expand All @@ -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__);
})();
Expand Down

0 comments on commit ed3e1d9

Please sign in to comment.