Skip to content

Commit

Permalink
Clone reused nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
eoin committed May 10, 2023
1 parent 358d5bb commit 8c44bb7
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/babel-plugin-relay/compileGraphQLTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ function createNode(

const id = topScope.generateUidIdentifier(definitionName);

const expHash = t.MemberExpression(id, t.Identifier('hash'));
const expHash = t.MemberExpression(t.cloneNode(id), t.Identifier('hash'));
const expWarn = warnNeedsRebuild(t, definitionName, options.buildCommand);
const expWarnIfOutdated = t.LogicalExpression(
'&&',
expHash,
t.cloneNode(expHash),
t.LogicalExpression(
'&&',
t.BinaryExpression('!==', expHash, t.StringLiteral(hash)),
Expand All @@ -145,34 +145,41 @@ function createNode(
const program = path.findParent(parent => parent.isProgram());
program.unshiftContainer('body', importDeclaration);

const expAssignAndCheck = t.SequenceExpression([expWarnIfOutdated, id]);
const expAssignAndCheck = t.SequenceExpression([
expWarnIfOutdated,
t.cloneNode(id),
]);

let expAssign;
if (options.isDevVariable != null) {
expAssign = t.ConditionalExpression(
t.Identifier(options.isDevVariable),
expAssignAndCheck,
id,
t.cloneNode(id),
);
} else if (options.isDevelopment) {
expAssign = expAssignAndCheck;
} else {
expAssign = id;
expAssign = t.cloneNode(id);
}

path.replaceWith(expAssign);
} else {
topScope.push({id});
topScope.push({id: t.cloneNode(id)});

const requireGraphQLModule = t.CallExpression(t.Identifier('require'), [
t.StringLiteral(requiredPath),
]);

const expAssignProd = t.AssignmentExpression('=', id, requireGraphQLModule);
const expAssignProd = t.AssignmentExpression(
'=',
t.cloneNode(id),
requireGraphQLModule,
);
const expAssignAndCheck = t.SequenceExpression([
expAssignProd,
expWarnIfOutdated,
id,
t.cloneNode(id),
]);

let expAssign;
Expand All @@ -191,9 +198,9 @@ function createNode(
const expVoid0 = t.UnaryExpression('void', t.NumericLiteral(0));
path.replaceWith(
t.ConditionalExpression(
t.BinaryExpression('!==', id, expVoid0),
id,
expAssign,
t.BinaryExpression('!==', t.cloneNode(id), expVoid0),
t.cloneNode(id),
t.cloneNode(expAssign),
),
);
}
Expand Down

0 comments on commit 8c44bb7

Please sign in to comment.