Skip to content

Commit ec51a58

Browse files
rishipalcopybara-github
authored andcommitted
Mark the newly created $jscomp$destructuring$var declarations in Es6RewriteDestructuring pass with IS_CONSTANT_NAME prop
Also special case and mark those names as constants even in ES6RewriteRestAndSpread as that pass transitions to running post-normalization. PiperOrigin-RevId: 618368483
1 parent 890db2c commit ec51a58

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,13 @@ private Node replacePatternParamWithTempVar(
321321
* (the compiler only assigns $jscomp$destructuring$var[num] once)
322322
*/
323323
private Node createTempVarNameNode(String name, AstFactory.Type type) {
324-
return astFactory.createConstantName(name, type);
324+
// NOTE: This does not really create a constant node as this pass runs before normalization. See
325+
// b/322009741.
326+
Node ret = astFactory.createConstantName(name, type);
327+
// TODO(b/197349249): When this pass moves post normalization, stop explictly marking these
328+
// names as const, as createConstantName will automatically do that.
329+
ret.putBooleanProp(Node.IS_CONSTANT_NAME, true);
330+
return ret;
325331
}
326332

327333
/** Creates a new unique name to use for a pattern we need to rewrite. */

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

+12
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ private void visitRestParam(NodeTraversal t, Node restParam, Node paramList) {
105105
astFactory.createNumber(restIndex),
106106
astFactory.createArgumentsReference()))
107107
.srcrefTreeIfMissing(functionBody);
108+
if (paramName.startsWith("$jscomp$destructuring$var")) {
109+
// Currently this pass is before normalization. Hence, the createSingleLetNameDeclaration
110+
// creates a non-const names. But the earlier Es6RewriteDesctructuring pass creates these
111+
// names as const names.
112+
// TODO(b/197349249): When this pass moves post normalization, stop explictly marking these
113+
// names as const in this if-block.
114+
checkState(
115+
nameNode.getBooleanProp(Node.IS_CONSTANT_NAME),
116+
"The $jscomp$destructuring$var[num] name must've been marked as const by the"
117+
+ " Es6RewriteDesctructuring pass");
118+
let.getFirstChild().putBooleanProp(Node.IS_CONSTANT_NAME, true);
119+
}
108120
functionBody.addChildToFront(let);
109121
NodeUtil.addFeatureToScript(t.getCurrentScript(), Feature.LET_DECLARATIONS, compiler);
110122
compiler.ensureLibraryInjected("es6/util/restarguments", /* force= */ false);

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

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.javascript.jscomp;
1717

18+
import static com.google.common.base.Preconditions.checkState;
1819
import static com.google.common.truth.Truth.assertThat;
1920
import static com.google.javascript.rhino.testing.NodeSubject.assertNode;
2021

@@ -556,6 +557,30 @@ public void testArrayDestructuring_inVanillaForInitializer_let() {
556557
"true; c++) {}"));
557558
}
558559

560+
@Test
561+
public void testSimpleDestructuring_constAnnotationAdded() {
562+
563+
test(
564+
"function foo(...{length}) {}",
565+
// pull destructuring out of params first.
566+
lines(
567+
"function foo(...$jscomp$destructuring$var0) {",
568+
" var $jscomp$destructuring$var1 = $jscomp$destructuring$var0;",
569+
" var length = $jscomp$destructuring$var1.length;",
570+
"}"));
571+
Node script = getLastCompiler().getJsRoot().getFirstChild();
572+
checkState(script.isScript());
573+
Node func = script.getFirstChild();
574+
checkState(func.isFunction());
575+
Node block = func.getLastChild();
576+
checkState(block.isBlock());
577+
// `var $jscomp$destructuring$var1 = ...`
578+
Node var1 = block.getFirstFirstChild();
579+
checkState(var1.isName());
580+
assertThat(var1.getString()).isEqualTo("$jscomp$destructuring$var1");
581+
assertThat(var1.getBooleanProp(Node.IS_CONSTANT_NAME)).isTrue();
582+
}
583+
559584
@Test
560585
public void testArrayDestructuringRest() {
561586
test(

0 commit comments

Comments
 (0)