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

Do not hoist jsx referencing a mutable binding #10529

Merged
merged 1 commit into from Oct 8, 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
@@ -0,0 +1,10 @@
let foo = 'hello';

const mutate = () => {
foo = 'goodbye';
}

export const Component = () => {
if (Math.random() > 0.5) mutate();
return <span>{foo}</span>;
};
@@ -0,0 +1,10 @@
let foo = 'hello';

const mutate = () => {
foo = 'goodbye';
};

export const Component = () => {
if (Math.random() > 0.5) mutate();
return <span>{foo}</span>;
};
@@ -0,0 +1,6 @@
let foo = 'hello';

export const Component = () => {
foo = 'goodbye';
return <span>{foo}</span>;
};
@@ -0,0 +1,5 @@
let foo = 'hello';
export const Component = () => {
foo = 'goodbye';
return <span>{foo}</span>;
};
14 changes: 14 additions & 0 deletions packages/babel-traverse/src/path/lib/hoister.js
Expand Up @@ -32,6 +32,15 @@ const referenceVisitor = {
const binding = path.scope.getBinding(path.node.name);
if (!binding) return;

// we can handle reassignments only if they happen in the same scope as the declaration
for (const violation of binding.constantViolations) {
if (violation.scope !== binding.path.scope) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we still hoist to the level of the violation?

let foo = 'hello';
export const Component = () => {
  foo = 'goodbye';
  return <span>{array.map(() => <inner>{foo}</inner>}</span>;
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we can't. Consider this example:

let foo = 0;
export const Component = () => {
  foo++;
  return Promise.resolve().then(() => <span>{foo}</span>);
};

Component();
Component();

Here both the components should use 2, but if we hoist it the first one will use 1.

state.mutableBinding = true;
path.stop();
return;
}
}

// this binding isn't accessible from the parent scope so we can safely ignore it
// eg. it's in a closure etc
if (binding !== state.scope.getBinding(path.node.name)) return;
Expand All @@ -46,6 +55,9 @@ export default class PathHoister {
this.breakOnScopePaths = [];
// Storage for bindings that may affect what path we can hoist to.
this.bindings = {};
// "true" if the current path contains a reference to a binding whose
// value can change and thus can't be safely hoisted.
this.mutableBinding = false;
// Storage for eligible scopes.
this.scopes = [];
// Our original scope and path.
Expand Down Expand Up @@ -195,6 +207,8 @@ export default class PathHoister {
run() {
this.path.traverse(referenceVisitor, this);

if (this.mutableBinding) return;

this.getCompatibleScopes();

const attachTo = this.getAttachmentPath();
Expand Down