Skip to content

Commit ce52aa6

Browse files
Merge pull request #4059 from CamWass:fix-args
PiperOrigin-RevId: 509388769
2 parents 7ee713c + a27cc06 commit ce52aa6

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
149149
* @param scope scope of the function
150150
*/
151151
private void tryReplaceArguments(Node scopeRoot) {
152+
Node scopeRootParent = scopeRoot.getParent();
153+
// Getters cannot have any params, so there are none to replace or synthesize.
154+
if (scopeRootParent.isGetterDef()) {
155+
return;
156+
}
157+
152158
// Find the number of parameters that can be accessed without using `arguments`.
153159
Node parametersList = NodeUtil.getFunctionParameters(scopeRoot);
154160
checkState(parametersList.isParamList(), parametersList);
@@ -161,8 +167,14 @@ private void tryReplaceArguments(Node scopeRoot) {
161167
return;
162168
}
163169

164-
ImmutableSortedMap<Integer, String> argNames =
165-
assembleParamNames(parametersList, highestIndex + 1);
170+
int maxCount = highestIndex + 1;
171+
// Setters can only have one param and it is required to be present, so we cannot synthesize any
172+
// new params, but we can still replace references to the first param.
173+
if (scopeRootParent.isSetterDef()) {
174+
maxCount = 1;
175+
}
176+
177+
ImmutableSortedMap<Integer, String> argNames = assembleParamNames(parametersList, maxCount);
166178
changeMethodSignature(argNames, parametersList);
167179
changeBody(argNames);
168180
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,33 @@ public void testGlobalArgumentsReferences() {
352352
" console.log(arguments);",
353353
"}"));
354354
}
355+
356+
@Test
357+
public void testGettersCannotHaveAnyParams() {
358+
// Getters cannot have any parameters; synthesizing them would be an error.
359+
testSame("class Foo { get prop() { arguments[0] } }");
360+
testSame("const a = { get prop() { arguments[0] } }");
361+
362+
// Ensure references in nested functions are still eligible.
363+
test(
364+
"class Foo { get prop() { function f( ) { arguments[0] } } }", //
365+
"class Foo { get prop() { function f(p0) { p0 } } }");
366+
}
367+
368+
@Test
369+
public void testSettersCanOnlyHaveOneParam() {
370+
// Setters can only have one parameter; synthesizing any more would be an error.
371+
testSame("class Foo { set prop(x) { arguments[1] } }");
372+
testSame("const a = { set prop(x) { arguments[1] } }");
373+
374+
// We can still replace references to the first param.
375+
test(
376+
"class Foo { set prop(x) { arguments[0]; arguments[1] } }", //
377+
"class Foo { set prop(x) { x; arguments[1] } }");
378+
379+
// Ensure references in nested functions are still eligible.
380+
test(
381+
"class Foo { set prop(x) { function f( ) { arguments[0] } } }", //
382+
"class Foo { set prop(x) { function f(p0) { p0 } } }");
383+
}
355384
}

0 commit comments

Comments
 (0)