Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clone node for signature, fixes #1268 #1276

Merged
merged 1 commit into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 2 additions & 35 deletions src/fresh/babel.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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