Skip to content

Commit 627f90f

Browse files
lauraharkerblickly
authored andcommitted
Treat NEW as escaping all variables in DeadPropertyAssignmentElimination
Fixes #2924 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=196892725
1 parent b1789cc commit 627f90f

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ public void visit(NodeTraversal t, Node n, Node parent) {
292292
visitAssignmentLhs(n.getFirstChild());
293293
}
294294

295-
// Assume that all properties may be read when leaving the function in a CALL or YIELD
296-
if (n.isCall() || n.isYield()) {
295+
// Assume that all properties may be read when control flow leaves the function
296+
if (NodeUtil.isInvocation(n) || n.isYield()) {
297297
if (ASSUME_CONSTRUCTORS_HAVENT_ESCAPED
298298
&& isConstructor
299299
&& !NodeUtil.referencesThis(n)

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

+7
Original file line numberDiff line numberDiff line change
@@ -4986,6 +4986,13 @@ static boolean isInvocationTarget(Node n) {
49864986
&& parent.getFirstChild() == n;
49874987
}
49884988

4989+
/**
4990+
* Returns whether this is a call (including tagged template lits) or new.
4991+
*/
4992+
static boolean isInvocation(Node n) {
4993+
return isCallOrNew(n) || n.isTaggedTemplateLit();
4994+
}
4995+
49894996
static boolean isCallOrNewArgument(Node n) {
49904997
Node parent = n.getParent();
49914998
return parent != null && isCallOrNew(parent) && parent.getFirstChild() != n;

test/com/google/javascript/jscomp/DeadPropertyAssignmentEliminationTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,28 @@ public void testYield() {
254254
"}"));
255255
}
256256

257+
public void testNew() {
258+
// Assume that properties may be read during a constructor call
259+
testSame(
260+
lines(
261+
"var foo = function() {",
262+
" a.b.c = 20;",
263+
" new C;",
264+
" a.b.c = 30;",
265+
"}"));
266+
}
267+
268+
public void testTaggedTemplateLit() {
269+
// Assume that properties may be read during a tagged template lit invocation
270+
testSame(
271+
lines(
272+
"var foo = function() {",
273+
" a.b.c = 20;",
274+
" doSomething`foo`;",
275+
" a.b.c = 30;",
276+
"}"));
277+
}
278+
257279
public void testUnknownLookup() {
258280
testSame(
259281
lines(

0 commit comments

Comments
 (0)