Skip to content

Commit

Permalink
FIX: Arrow functions obj destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
0xe committed Sep 2, 2024
1 parent 3847ee6 commit cb2858e
Show file tree
Hide file tree
Showing 3 changed files with 1,164 additions and 617 deletions.
14 changes: 9 additions & 5 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3588,6 +3588,7 @@ private ObjectLiteral objectLiteral() throws IOException {
setterNames = new HashSet<>();
}
Comment objJsdocNode = getAndResetJsDoc();
boolean processedDestructuringDefault = false;

commaLoop:
for (; ; ) {
Expand Down Expand Up @@ -3621,12 +3622,10 @@ private ObjectLiteral objectLiteral() throws IOException {
// many tokens.)
int peeked = peekToken();
if (peeked != Token.COMMA && peeked != Token.COLON && peeked != Token.RC) {
if (peeked == Token.ASSIGN
&& inDestructuringAssignment) { // we have an object literal with
// destructuring assignment and a
// default
if (peeked == Token.ASSIGN) { // we have an object literal with destructuring assignment
if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) {
elems.add(plainProperty(pname, tt));
processedDestructuringDefault = true;
if (matchToken(Token.COMMA, true)) {
continue;
} else {
Expand Down Expand Up @@ -3704,6 +3703,11 @@ private ObjectLiteral objectLiteral() throws IOException {
}

mustMatchToken(Token.RC, "msg.no.brace.prop", true);
if (processedDestructuringDefault && !inDestructuringAssignment) {
if (peekToken() == Token.RP) {
reportError("msg.syntax");
}
}
ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos);
if (objJsdocNode != null) {
pn.setJsDocNode(objJsdocNode);
Expand Down Expand Up @@ -3782,7 +3786,7 @@ private ObjectProperty plainProperty(AstNode property, int ptt) throws IOExcepti
pn.setIsShorthand(true);
pn.setLeftAndRight(property, nn);
return pn;
} else if (tt == Token.ASSIGN && inDestructuringAssignment) {
} else if (tt == Token.ASSIGN) {
/* we're in destructuring with defaults in a object literal; treat defaults as values */
ObjectProperty pn = new ObjectProperty();
consumeToken(); // consume the `=`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

import org.junit.Ignore;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EcmaError;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.*;

/*
Many of these are taken from examples at developer.mozilla.org
Expand Down Expand Up @@ -40,7 +37,6 @@ public void functionDefaultArgsArrayArrow() throws Exception {
}

@Test
@Ignore("wip")
public void functionDefaultArgsObjectArrow() throws Exception {
final String script = "(({x = 1} = {x: 2}) => {\n" + " return x;\n" + "})";

Expand All @@ -66,6 +62,24 @@ public void functionDefaultArgsUsage() throws Exception {
assertIntEvaluates(34, script + "\nfoo(32, 2)");
}

@Test
public void ObjIdInitSimpleStrictExpr() throws Exception {
final String script = "var e = (0, { eval = 0 } = {}); Object.keys(e).length == 0 && Object.values(e).length == 0";
assertEvaluates(true, script);
}

@Test
public void ObjIdInitSimpleStrictForOf() throws Exception {
final String script = "for ({ eval = 0 } of [{}]) ;";
assertEvaluates(Undefined.instance, script);
}

@Test
public void CoverInitName() throws Exception {
final String script = "({ a = 1 });";
assertThrows("syntax error", script);
}

@Test
@Ignore("wip") // TODO: generates bad tree
public void letExprDestructuring() throws Exception {
Expand Down Expand Up @@ -345,6 +359,9 @@ private static void assertThrowsWithLanguageLevel(
assertTrue(((EcmaError) e).getMessage().startsWith(expected));
return true;
}
} catch (EvaluatorException e) {
assertTrue(((EvaluatorException) e).getMessage().startsWith(expected));
return true;
}
fail("expected EcmaError but got " + rep);
return null;
Expand Down
Loading

0 comments on commit cb2858e

Please sign in to comment.