Skip to content

Commit

Permalink
Fix ConcretizeStaticInheritanceForInlining bug adding semi-random new…
Browse files Browse the repository at this point in the history
… static props

In theory, these properties were always dead-code eliminated and this won't affect the optimized code.

There's a small chance some code accidentally referenced the added properties & will break. Such references should have caused type errors, though.

PiperOrigin-RevId: 554551433
  • Loading branch information
lauraharker authored and copybara-github committed Aug 7, 2023
1 parent 86750a3 commit 7bb038f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.IR;
Expand Down Expand Up @@ -325,6 +326,9 @@ private void visitFunctionClassDef(Node n) {
JSDocInfo classInfo = NodeUtil.getBestJSDocInfo(n);
if (classInfo != null && classInfo.isConstructor()) {
String name = NodeUtil.getName(n);
if (name == null) {
return;
}
if (classByAlias.containsKey(name)) {
duplicateClassNames.add(name);
} else {
Expand All @@ -334,6 +338,8 @@ private void visitFunctionClassDef(Node n) {
}

private void setAlias(String original, String alias) {
Preconditions.checkNotNull(original, "original is null");
Preconditions.checkNotNull(alias, "alias is null");
checkArgument(classByAlias.containsKey(original));
classByAlias.put(alias, classByAlias.get(original));
}
Expand Down Expand Up @@ -369,7 +375,7 @@ private void visitVariableDeclaration(Node n) {
return;
}
String maybeOriginalName = child.getFirstChild().getQualifiedName();
if (classByAlias.containsKey(maybeOriginalName)) {
if (maybeOriginalName != null && classByAlias.containsKey(maybeOriginalName)) {
String maybeAlias = child.getQualifiedName();
if (maybeAlias != null) {
setAlias(maybeOriginalName, maybeAlias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,20 +628,9 @@ public void testAddSingletonGetter() {

@Test
public void testNonQnameConstructor_doesntPolluteListOfAssignments() {
// Reproduce a pretty bad bug caused by accidentally reading/writing 'null' keys from a map.
test(
lines(
"const ns = {};",
"/** @constructor */",
"ns['NOT_A_NAME'] = function() {};",
"ns['NOT_A_NAME'].staticMethod = function() { alert(1); }",
"",
"/** @constructor */",
"const Example = function() {}",
"",
"/** @constructor @extends {Example} */",
"function Subclass() {}",
"$jscomp.inherits(Subclass, Example);"),
// Reproduce a bug that once created a nonsensical assignment:
// Subclass.staticMethod = Example.staticMethod;
testSame(
lines(
"const ns = {};",
"/** @constructor */",
Expand All @@ -653,9 +642,6 @@ public void testNonQnameConstructor_doesntPolluteListOfAssignments() {
"",
"/** @constructor @extends {Example} */",
"function Subclass() {}",
"$jscomp.inherits(Subclass, Example);",
// TODO(b/293320792) - stop producing this assignment, there's no
// actual Example.staticMethod.
"Subclass.staticMethod = Example.staticMethod;"));
"$jscomp.inherits(Subclass, Example);"));
}
}

0 comments on commit 7bb038f

Please sign in to comment.