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 3be3add
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
24 changes: 16 additions & 8 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 @@ -2295,7 +2302,7 @@ private AstNode expr(boolean allowTrailingComma) throws IOException {
if (compilerEnv.isStrictMode() && !pn.hasSideEffects())
addStrictWarning("msg.no.side.effects", "", pos, nodeEnd(pn) - pos);
if (peekToken() == Token.YIELD) reportError("msg.yield.parenthesized");
if (peekToken() == Token.RP && allowTrailingComma) {
if (allowTrailingComma && peekToken() == Token.RP) {
pn.putIntProp(Node.TRAILING_COMMA, 1);
return pn;
}
Expand Down Expand Up @@ -3170,16 +3177,14 @@ private AstNode parenExpr() throws IOException {
}
mustMatchToken(Token.RP, "msg.no.paren", true);

if (e.getIntProp(Node.TRAILING_COMMA, 0) == 1 && peekToken() != Token.ARROW) {
reportError("msg.syntax");
return makeErrorNode();
}
int length = ts.tokenEnd - begin;

if (e.getType() == Token.EMPTY && peekToken() != Token.ARROW) {
boolean hasTrailingComma = e.getIntProp(Node.TRAILING_COMMA, 0) == 1;
if ((hasTrailingComma || 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);
if (jsdocNode == null) {
Expand All @@ -3188,6 +3193,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 3be3add

Please sign in to comment.