Skip to content

Commit ed3e1d9

Browse files
committed
fix: clone node for signature, fixes #1268
1 parent 70353e5 commit ed3e1d9

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

Diff for: src/fresh/babel.js

+2-35
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export default function(babel) {
202202
key: fnHookCalls.map(call => call.name + '{' + call.key + '}').join('\n'),
203203
customHooks: fnHookCalls
204204
.filter(call => !isBuiltinHook(call.name))
205-
.map(call => call.callee),
205+
.map(call => t.clone(call.callee)),
206206
};
207207
}
208208

@@ -217,7 +217,6 @@ export default function(babel) {
217217

218218
let seenForRegistration = new WeakSet();
219219
let seenForSignature = new WeakSet();
220-
let seenForOutro = new WeakSet();
221220

222221
let hookCalls = new WeakMap();
223222
const HookCallsVisitor = {
@@ -291,7 +290,7 @@ export default function(babel) {
291290

292291
// Make sure we're not mutating the same tree twice.
293292
// This can happen if another Babel plugin replaces parents.
294-
if (seenForRegistration.has(node)) {
293+
if (seenForRegistration.has(node)) {
295294
return;
296295
}
297296
seenForRegistration.add(node);
@@ -557,38 +556,6 @@ export default function(babel) {
557556
// but it's the best we can do until we stop transpiling destructuring.
558557
path.traverse(HookCallsVisitor);
559558
},
560-
exit(path) {
561-
return;
562-
const registrations = registrationsByProgramPath.get(path);
563-
if (registrations === undefined) {
564-
return;
565-
}
566-
567-
// Make sure we're not mutating the same tree twice.
568-
// This can happen if another Babel plugin replaces parents.
569-
const node = path.node;
570-
if (seenForOutro.has(node)) {
571-
return;
572-
}
573-
seenForOutro.add(node);
574-
// Don't mutate the tree above this point.
575-
576-
registrationsByProgramPath.delete(path);
577-
const declarators = [];
578-
path.pushContainer('body', t.variableDeclaration('var', declarators));
579-
registrations.forEach(({handle, persistentID}) => {
580-
path.pushContainer(
581-
'body',
582-
t.expressionStatement(
583-
t.callExpression(t.identifier('__register__'), [
584-
handle,
585-
t.stringLiteral(persistentID),
586-
]),
587-
),
588-
);
589-
declarators.push(t.variableDeclarator(handle));
590-
});
591-
},
592559
},
593560
},
594561
};

Diff for: test/__babel_fixtures__/hooks.js

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const useCustomHook = () => {
3636
useExternalHook();
3737
};
3838

39+
const useCustomHookAgain = () => {
40+
useState(42);
41+
useEffectHook();
42+
useExternalHook();
43+
};
44+
3945
function useFunc () {
4046
useState(42);
4147
useEffectHook();

Diff for: test/__snapshots__/babel.test.js.snap

+20
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,16 @@ __signature__(useCustomHook, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternal
12031203
return [useEffectHook, _externalHook.useExternalHook];
12041204
});
12051205
1206+
var useCustomHookAgain = function useCustomHookAgain() {
1207+
(0, _react.useState)(42);
1208+
useEffectHook();
1209+
(0, _externalHook.useExternalHook)();
1210+
};
1211+
1212+
__signature__(useCustomHookAgain, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternalHook{}', function () {
1213+
return [useEffectHook, _externalHook.useExternalHook];
1214+
});
1215+
12061216
function useFunc() {
12071217
(0, _react.useState)(42);
12081218
useEffectHook();
@@ -1228,6 +1238,7 @@ __signature__(useFunc, 'useState{(42)}\\\\nuseEffectHook{}', function () {
12281238
reactHotLoader.register(useForwardRefHook, 'useForwardRefHook', __FILENAME__);
12291239
reactHotLoader.register(useForwardRefFunctionHook, 'useForwardRefFunctionHook', __FILENAME__);
12301240
reactHotLoader.register(useCustomHook, 'useCustomHook', __FILENAME__);
1241+
reactHotLoader.register(useCustomHookAgain, 'useCustomHookAgain', __FILENAME__);
12311242
reactHotLoader.register(useFunc, 'useFunc', __FILENAME__);
12321243
})();
12331244
@@ -2260,6 +2271,14 @@ const useCustomHook = () => {
22602271
22612272
__signature__(useCustomHook, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternalHook{}', () => [useEffectHook, _externalHook.useExternalHook]);
22622273
2274+
const useCustomHookAgain = () => {
2275+
(0, _react.useState)(42);
2276+
useEffectHook();
2277+
(0, _externalHook.useExternalHook)();
2278+
};
2279+
2280+
__signature__(useCustomHookAgain, 'useState{(42)}\\\\nuseEffectHook{}\\\\nuseExternalHook{}', () => [useEffectHook, _externalHook.useExternalHook]);
2281+
22632282
function useFunc() {
22642283
(0, _react.useState)(42);
22652284
useEffectHook();
@@ -2283,6 +2302,7 @@ __signature__(useFunc, 'useState{(42)}\\\\nuseEffectHook{}', () => [useEffectHoo
22832302
reactHotLoader.register(useForwardRefHook, 'useForwardRefHook', __FILENAME__);
22842303
reactHotLoader.register(useForwardRefFunctionHook, 'useForwardRefFunctionHook', __FILENAME__);
22852304
reactHotLoader.register(useCustomHook, 'useCustomHook', __FILENAME__);
2305+
reactHotLoader.register(useCustomHookAgain, 'useCustomHookAgain', __FILENAME__);
22862306
reactHotLoader.register(useFunc, 'useFunc', __FILENAME__);
22872307
})();
22882308

0 commit comments

Comments
 (0)