Skip to content

Commit 745b880

Browse files
committed
Fix crash when arguments[] occurs within getter/setter
1 parent 8379354 commit 745b880

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ 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+
// Nothing to do for getters and setters:
154+
// - Getters cannot have any params.
155+
// - Setters can only have one param, and it is required to be present, so we cannot synthesize
156+
// any new params.
157+
if (scopeRootParent.isGetterDef() || scopeRootParent.isSetterDef()) {
158+
return;
159+
}
160+
152161
// Find the number of parameters that can be accessed without using `arguments`.
153162
Node parametersList = NodeUtil.getFunctionParameters(scopeRoot);
154163
checkState(parametersList.isParamList(), parametersList);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,18 @@ public void testGlobalArgumentsReferences() {
352352
" console.log(arguments);",
353353
"}"));
354354
}
355+
356+
@Test
357+
public void testGettersCannotHaveAnyParams() {
358+
// Getters cannot have any parameters; synthesizing one would be an error.
359+
testSame("class Foo { get prop() { arguments[0] } }");
360+
testSame("const a = { get prop() { arguments[0] } }");
361+
}
362+
363+
@Test
364+
public void testSettersCanOnlyHaveOneParam() {
365+
// Setters can only have one parameter; synthesizing any more would be an error.
366+
testSame("class Foo { set prop(x) { arguments[1] } }");
367+
testSame("const a = { set prop(x) { arguments[1] } }");
368+
}
355369
}

0 commit comments

Comments
 (0)