Skip to content

Commit

Permalink
Include trailing commas in function params in AST.toSource
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bakker committed Nov 6, 2023
1 parent c17868e commit 7072014
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,10 @@ private void parseFunctionParams(FunctionNode fnNode) throws IOException {
Set<String> paramNames = new HashSet<>();
do {
int tt = peekToken();
if (tt == Token.RP) {
fnNode.putIntProp(Node.TRAILING_COMMA, 1);
break;
}
if (tt == Token.LB || tt == Token.LC) {
AstNode expr = destructuringPrimaryExpr();
markDestructuring(expr);
Expand Down Expand Up @@ -821,7 +825,7 @@ private void parseFunctionParams(FunctionNode fnNode) throws IOException {
fnNode.addParam(makeErrorNode());
}
}
} while (matchToken(Token.COMMA, true) && peekToken() != Token.RP);
} while (matchToken(Token.COMMA, true));

if (destructuring != null) {
Node destructuringNode = new Node(Token.COMMA);
Expand Down Expand Up @@ -969,6 +973,9 @@ private AstNode arrowFunction(AstNode params) throws IOException {
try {
if (params instanceof ParenthesizedExpression) {
fnNode.setParens(0, params.getLength());
if (params.getIntProp(Node.TRAILING_COMMA, 0) == 1) {
fnNode.putIntProp(Node.TRAILING_COMMA, 1);
}
AstNode p = ((ParenthesizedExpression) params).getExpression();
if (!(p instanceof EmptyExpression)) {
arrowFunctionParams(fnNode, p, destructuring, paramNames);
Expand Down Expand Up @@ -3170,15 +3177,12 @@ private AstNode parenExpr() throws IOException {
}
mustMatchToken(Token.RP, "msg.no.paren", true);

if (e.getIntProp(Node.TRAILING_COMMA, 0) == 1 && peekToken() != Token.ARROW) {
boolean hasTrailingComma = e.getIntProp(Node.TRAILING_COMMA, 0) == 1;
if (peekToken() != Token.ARROW && (hasTrailingComma || e.getType() == Token.EMPTY)) {
reportError("msg.syntax");
return makeErrorNode();
}

if (e.getType() == Token.EMPTY && peekToken() != Token.ARROW) {
reportError("msg.syntax");
return makeErrorNode();
}
int length = ts.tokenEnd - begin;
ParenthesizedExpression pn = new ParenthesizedExpression(begin, length, e);
pn.setLineno(lineno);
Expand All @@ -3188,6 +3192,9 @@ private AstNode parenExpr() throws IOException {
if (jsdocNode != null) {
pn.setJsDocNode(jsdocNode);
}
if (hasTrailingComma) {
pn.putIntProp(Node.TRAILING_COMMA, 1);
}
return pn;
} finally {
inForInit = wasInForInit;
Expand Down
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/ast/FunctionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ public String toSource(int depth) {
} else {
sb.append("(");
printList(params, sb);
if (getIntProp(TRAILING_COMMA, 0) == 1) {
sb.append(", ");
}
sb.append(") ");
}
if (isArrow) {
Expand Down

0 comments on commit 7072014

Please sign in to comment.