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 57d0043
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 144 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

0 comments on commit 57d0043

Please sign in to comment.