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: constant variables only enable constant react elements #13054

Merged
merged 1 commit into from Mar 25, 2021
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
Expand Up @@ -64,13 +64,17 @@ export default declare((api, options) => {
// Ignore identifiers & JSX expressions.
if (
path.isJSXIdentifier() ||
path.isIdentifier() ||
path.isJSXMemberExpression() ||
path.isJSXNamespacedName()
) {
return;
}

if (path.isIdentifier()) {
const binding = path.scope.getBinding(path.node.name);
if (binding && binding.constant) return;
}

if (!path.isImmutable()) {
// If it's not immutable, it may still be a pure expression, such as string concatenation.
// It is still safe to hoist that, so long as its result is immutable.
Expand Down
@@ -1,5 +1,3 @@
var _span;

let foo = 'hello';

const mutate = () => {
Expand All @@ -8,5 +6,5 @@ const mutate = () => {

export const Component = () => {
if (Math.random() > 0.5) mutate();
return _span || (_span = <span>{foo}</span>);
return <span>{foo}</span>;
};
@@ -1,7 +1,5 @@
var _span;

let foo = 'hello';
export const Component = () => {
foo = 'goodbye';
return _span || (_span = <span>{foo}</span>);
return <span>{foo}</span>;
};
@@ -0,0 +1,10 @@
function render() {
const nodes = [];

for(let i = 0; i < 5; i++) {
nodes.push(<div>{i}</div>)
}

return nodes;
}

@@ -0,0 +1,10 @@
function render() {
const nodes = [];

for (let i = 0; i < 5; i++) {
nodes.push(<div>{i}</div>);
}

return nodes;
}

@@ -1,7 +1,5 @@
var _div;

var Foo = React.createClass({
render: function render() {
return _div || (_div = <div foo={notDeclared}></div>);
return <div foo={notDeclared}></div>;
}
});