Skip to content

Commit acc6e94

Browse files
lauraharkercopybara-github
authored andcommitted
Remove unnecessary PropagateConstantPropertyOverVars in property collapsing
Requires setting Node.IS_CONSTANT_NAME explicitly in a few more places after flattening a property into a variable. PiperOrigin-RevId: 565176828
1 parent 31018cb commit acc6e94

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

src/com/google/javascript/jscomp/InlineAndCollapseProperties.java

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.google.javascript.jscomp.GlobalNamespace.Ref;
3535
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
3636
import com.google.javascript.jscomp.NodeTraversal.ExternsSkippingCallback;
37-
import com.google.javascript.jscomp.Normalize.PropagateConstantPropertyOverVars;
3837
import com.google.javascript.jscomp.base.format.SimpleFormat;
3938
import com.google.javascript.jscomp.deps.ModuleLoader.ResolutionMode;
4039
import com.google.javascript.jscomp.diagnostic.LogFile;
@@ -236,25 +235,13 @@ private void performMinimalInliningAndModuleExportCollapsing(Node externs, Node
236235
// and reuse that one instead.
237236
namespace = new GlobalNamespace(decisionsLog, compiler, root);
238237
new CollapseProperties().process(externs, root);
239-
240-
namespace = null; // free up memory before PropagateConstantPropertyOverVars
241-
// This shouldn't be necessary, this pass should already be setting new constants as
242-
// constant.
243-
// TODO(b/64256754): Investigate.
244-
new PropagateConstantPropertyOverVars(compiler, false).process(externs, root);
245238
}
246239

247240
private void performAggressiveInliningAndCollapsing(Node externs, Node root) {
248241
new ConcretizeStaticInheritanceForInlining(compiler).process(externs, root);
249242
new AggressiveInlineAliases().process(externs, root);
250243

251244
new CollapseProperties().process(externs, root);
252-
253-
namespace = null; // free up memory before PropagateConstantPropertyOverVars
254-
// This shouldn't be necessary, this pass should already be setting new constants as
255-
// constant.
256-
// TODO(b/64256754): Investigate.
257-
new PropagateConstantPropertyOverVars(compiler, false).process(externs, root);
258245
}
259246

260247
private void performAggressiveInliningForTest(Node externs, Node root) {
@@ -1082,6 +1069,7 @@ private Node expandObjectPattern(
10821069
checkState(keyChild.isObjectPattern());
10831070
String uniqueId = t.getCompiler().getUniqueIdSupplier().getUniqueId(t.getInput());
10841071
nameNode = IR.name("destructuring$" + uniqueId).srcref(keyChild);
1072+
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
10851073
}
10861074
Node newRhs = IR.getprop(rhs.cloneTree(), key.getString()).srcref(keyChild);
10871075
Node newConstNode = IR.constNode(nameNode, newRhs).srcref(objectPattern);
@@ -1543,6 +1531,22 @@ private void flattenSimpleStubDeclaration(Name name, String alias) {
15431531
compiler.reportChangeToEnclosingScope(varNode);
15441532
}
15451533

1534+
/**
1535+
* Returns whether a NAME node with the given {@code collapsedName}, for a name originally
1536+
* declared at {@code declarationNode}, should be annotated with {@link Node#IS_CONSTANT_NAME}
1537+
*
1538+
* <p>Validity checks in unit tests will otherwise throw an exception.
1539+
*/
1540+
private boolean shouldAddConstantName(Node declarationNode, String collapsedName) {
1541+
if (declarationNode == null) {
1542+
return false;
1543+
}
1544+
JSDocInfo info = NodeUtil.getBestJSDocInfo(declarationNode.getParent());
1545+
return (info != null && info.isConstant())
1546+
|| declarationNode.getBooleanProp(Node.IS_CONSTANT_NAME)
1547+
|| compiler.getCodingConvention().isConstant(collapsedName);
1548+
}
1549+
15461550
/**
15471551
* Flattens all references to a collapsible property of a global name except its initial
15481552
* definition.
@@ -1552,6 +1556,9 @@ private void flattenSimpleStubDeclaration(Name name, String alias) {
15521556
*/
15531557
private void flattenReferencesTo(Name n, String alias) {
15541558
String originalName = n.getFullName();
1559+
boolean isConstantName =
1560+
shouldAddConstantName(
1561+
n.getDeclaration() != null ? n.getDeclaration().getNode() : null, alias);
15551562
for (Ref r : n.getRefs()) {
15561563
if (r == n.getDeclaration()) {
15571564
// Declarations are handled separately.
@@ -1561,7 +1568,7 @@ private void flattenReferencesTo(Name n, String alias) {
15611568
// We shouldn't flatten a reference that's an object literal key, because duplicate keys
15621569
// show up as refs.
15631570
if (!NodeUtil.mayBeObjectLitKey(r.getNode())) {
1564-
flattenNameRef(alias, r.getNode(), rParent, originalName);
1571+
flattenNameRef(alias, r.getNode(), rParent, originalName, isConstantName);
15651572
} else if (r.getNode().isStringKey() && r.getNode().getParent().isObjectPattern()) {
15661573
Node newNode = IR.name(alias).srcref(r.getNode());
15671574
NodeUtil.copyNameAnnotations(r.getNode(), newNode);
@@ -1575,7 +1582,7 @@ private void flattenReferencesTo(Name n, String alias) {
15751582
// replaced with "a$b" in all occurrences of "a.b.c", "a.b.c.d", etc.
15761583
if (n.props != null) {
15771584
for (Name p : n.props) {
1578-
flattenPrefixes(alias, originalName + p.getBaseName(), p, 1);
1585+
flattenPrefixes(alias, originalName + p.getBaseName(), p, 1, isConstantName);
15791586
}
15801587
}
15811588
}
@@ -1590,12 +1597,13 @@ private void flattenReferencesTo(Name n, String alias) {
15901597
* to n.getFullName(), but pre-computed to save on intermediate string allocation
15911598
* @param depth The difference in depth between the property name and the prefix name (e.g. 2)
15921599
*/
1593-
private void flattenPrefixes(String alias, String originalName, Name n, int depth) {
1600+
private void flattenPrefixes(
1601+
String alias, String originalName, Name n, int depth, boolean isConstantName) {
15941602
// Only flatten the prefix of a name declaration if the name being
15951603
// initialized is fully qualified (i.e. not an object literal key).
15961604
Ref decl = n.getDeclaration();
15971605
if (decl != null && decl.getNode() != null && decl.getNode().isGetProp()) {
1598-
flattenNameRefAtDepth(alias, decl.getNode(), depth, originalName);
1606+
flattenNameRefAtDepth(alias, decl.getNode(), depth, originalName, isConstantName);
15991607
}
16001608

16011609
for (Ref r : n.getRefs()) {
@@ -1604,12 +1612,12 @@ private void flattenPrefixes(String alias, String originalName, Name n, int dept
16041612
continue;
16051613
}
16061614

1607-
flattenNameRefAtDepth(alias, r.getNode(), depth, originalName);
1615+
flattenNameRefAtDepth(alias, r.getNode(), depth, originalName, isConstantName);
16081616
}
16091617

16101618
if (n.props != null) {
16111619
for (Name p : n.props) {
1612-
flattenPrefixes(alias, originalName + p.getBaseName(), p, depth + 1);
1620+
flattenPrefixes(alias, originalName + p.getBaseName(), p, depth + 1, isConstantName);
16131621
}
16141622
}
16151623
}
@@ -1622,7 +1630,8 @@ private void flattenPrefixes(String alias, String originalName, Name n, int dept
16221630
* @param depth The difference in depth between the property name and the prefix name (e.g. 2)
16231631
* @param originalName String version of the property name.
16241632
*/
1625-
private void flattenNameRefAtDepth(String alias, Node n, int depth, String originalName) {
1633+
private void flattenNameRefAtDepth(
1634+
String alias, Node n, int depth, String originalName, boolean isConstantName) {
16261635
// This method has to work for both GETPROP chains and, in rare cases,
16271636
// OBJLIT keys, possibly nested. That's why we check for children before
16281637
// proceeding. In the OBJLIT case, we don't need to do anything.
@@ -1635,7 +1644,7 @@ private void flattenNameRefAtDepth(String alias, Node n, int depth, String origi
16351644
n = n.getFirstChild();
16361645
}
16371646
if (n.isGetProp() && n.getFirstChild().isGetProp()) {
1638-
flattenNameRef(alias, n.getFirstChild(), n, originalName);
1647+
flattenNameRef(alias, n.getFirstChild(), n, originalName, isConstantName);
16391648
}
16401649
}
16411650
}
@@ -1647,8 +1656,10 @@ private void flattenNameRefAtDepth(String alias, Node n, int depth, String origi
16471656
* @param n The GETPROP node corresponding to the original name (e.g. "a.b")
16481657
* @param parent {@code n}'s parent
16491658
* @param originalName String version of the property name.
1659+
* @param isConstantName whether to annotate with {@link Node#IS_CONSTANT_NAME}
16501660
*/
1651-
private void flattenNameRef(String alias, Node n, Node parent, String originalName) {
1661+
private void flattenNameRef(
1662+
String alias, Node n, Node parent, String originalName, boolean isConstantName) {
16521663
Preconditions.checkArgument(
16531664
n.isGetProp(), "Expected GETPROP, found %s. Node: %s", n.getToken(), n);
16541665

@@ -1662,6 +1673,9 @@ private void flattenNameRef(String alias, Node n, Node parent, String originalNa
16621673
// name a$b$c
16631674
Node ref = NodeUtil.newName(compiler, alias, n, originalName).copyTypeFrom(n);
16641675
NodeUtil.copyNameAnnotations(n, ref);
1676+
if (isConstantName) {
1677+
ref.putBooleanProp(Node.IS_CONSTANT_NAME, isConstantName);
1678+
}
16651679
if (NodeUtil.isNormalOrOptChainCall(parent) && n.isFirstChildOf(parent)) {
16661680
// The node was a call target. We are deliberately flattening these as
16671681
// the "this" isn't provided by the namespace. Mark it as such:
@@ -1869,13 +1883,9 @@ private void updateGlobalNameDeclarationAtAssignNode(
18691883
Node nameNode =
18701884
NodeUtil.newName(compiler, alias, ref.getNode().getAncestor(2), n.getFullName());
18711885

1872-
Node constPropNode = ref.getNode();
1873-
JSDocInfo info = NodeUtil.getBestJSDocInfo(ref.getNode().getParent());
1874-
nameNode.putBooleanProp(
1875-
Node.IS_CONSTANT_NAME,
1876-
(info != null && info.hasConstAnnotation())
1877-
|| constPropNode.getBooleanProp(Node.IS_CONSTANT_NAME));
1886+
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, shouldAddConstantName(ref.getNode(), alias));
18781887

1888+
JSDocInfo info = NodeUtil.getBestJSDocInfo(ref.getNode().getParent());
18791889
if (info != null) {
18801890
varNode.setJSDocInfo(info);
18811891
}
@@ -2097,14 +2107,15 @@ private void declareVariablesForObjLitValues(
20972107

20982108
String propAlias = appendPropForAlias(alias, propName);
20992109
Node refNode = null;
2110+
boolean isConstantName = shouldAddConstantName(key, propAlias);
21002111
if (discardKeys) {
21012112
key.detach();
21022113
value.detach();
21032114
// Don't report a change here because the objlit has already been removed from the tree.
21042115
} else {
21052116
// Substitute a reference for the value.
21062117
refNode = IR.name(propAlias);
2107-
if (key.getBooleanProp(Node.IS_CONSTANT_NAME)) {
2118+
if (isConstantName) {
21082119
refNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
21092120
}
21102121

@@ -2115,7 +2126,7 @@ private void declareVariablesForObjLitValues(
21152126
// Declare the collapsed name as a variable with the original value.
21162127
Node nameNode = IR.name(propAlias);
21172128
nameNode.addChildToFront(value);
2118-
if (key.getBooleanProp(Node.IS_CONSTANT_NAME)) {
2129+
if (isConstantName) {
21192130
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
21202131
}
21212132
Node newVar = IR.var(nameNode).srcrefTreeIfMissing(key);
@@ -2174,8 +2185,14 @@ private void addStubsForUndeclaredProperties(Name n, String alias, Node parent,
21742185
// Determine if this is a constant var by checking the first
21752186
// reference to it. Don't check the declaration, as it might be null.
21762187
Node constPropNode = p.getFirstRef().getNode();
2177-
nameNode.putBooleanProp(
2178-
Node.IS_CONSTANT_NAME, constPropNode.getBooleanProp(Node.IS_CONSTANT_NAME));
2188+
boolean isConstantName =
2189+
// Don't call shouldAddConstantName because it expects to see a declaration node for
2190+
// a name, and this name is, by definition, undeclared.
2191+
constPropNode.getBooleanProp(Node.IS_CONSTANT_NAME)
2192+
|| compiler.getCodingConvention().isConstant(propAlias);
2193+
if (isConstantName) {
2194+
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
2195+
}
21792196

21802197
compiler.reportChangeToEnclosingScope(newVar);
21812198
addAfter = newVar;

0 commit comments

Comments
 (0)