fix: preserve trailing comma in optional call args (#17781)

This commit is contained in:
Huáng Jùnliàng
2026-02-13 14:55:52 +01:00
committed by GitHub
parent 1068fe7cfa
commit f33a877ca1
8 changed files with 46 additions and 42 deletions
@@ -71,6 +71,24 @@ export function ConditionalExpression(
this.print(node.alternate);
}
function _printExpressionArguments(
this: Printer,
node: t.CallExpression | t.NewExpression | t.OptionalCallExpression,
) {
this.token("(");
const oldNoLineTerminatorAfterNode = this.enterDelimited();
this.printList(
node.arguments,
this.shouldPrintTrailingComma(")"),
undefined,
undefined,
undefined,
true,
);
this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
this.rightParens(node);
}
export function NewExpression(
this: Printer,
node: t.NewExpression,
@@ -99,18 +117,7 @@ export function NewExpression(
return;
}
this.token("(");
const oldNoLineTerminatorAfterNode = this.enterDelimited();
this.printList(
node.arguments,
this.shouldPrintTrailingComma(")"),
undefined,
undefined,
undefined,
true,
);
this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
this.rightParens(node);
_printExpressionArguments.call(this, node);
}
export function SequenceExpression(this: Printer, node: t.SequenceExpression) {
@@ -186,18 +193,7 @@ export function OptionalCallExpression(
this.print(node.typeArguments);
this.token("(");
const oldNoLineTerminatorAfterNode = this.enterDelimited();
this.printList(
node.arguments,
undefined,
undefined,
undefined,
undefined,
true,
);
this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
this.rightParens(node);
_printExpressionArguments.call(this, node);
}
export function CallExpression(this: Printer, node: t.CallExpression) {
@@ -205,18 +201,7 @@ export function CallExpression(this: Printer, node: t.CallExpression) {
this.print(node.typeArguments);
this.token("(");
const oldNoLineTerminatorAfterNode = this.enterDelimited();
this.printList(
node.arguments,
this.shouldPrintTrailingComma(")"),
undefined,
undefined,
undefined,
true,
);
this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
this.rightParens(node);
_printExpressionArguments.call(this, node);
}
export function Import(this: Printer) {
+5 -3
View File
@@ -398,13 +398,15 @@ class Printer {
/**
* Writes a simple token.
*
* @param {string} str The string to append.
* @param {boolean} [maybeNewline=false] Wether `str` might potentially
* @param str The string to append.
* @param [maybeNewline=false] Whether `str` might potentially
* contain a line terminator or not.
* @param {number} [occurrenceCount=0] The occurrence count of this token in
* @param [occurrenceCount=0] The occurrence count of this token in
* the current node. This is used when printing in `preserveFormat` mode,
* to know which token we should map to (for example, to disambiguate the
* commas in an array literal).
* @param [mayNeedSpace=false] Check whether a whitespace might be
* needed after this token, and if so, add a whitespace after it.
*/
token(
str: string,
@@ -3,3 +3,7 @@ foo("foo");
foo("foo", "bar");
foo(bar());
foo(bar("test"));
foo(
"foo",
"bar",
);
@@ -2,4 +2,5 @@ foo();
foo("foo");
foo("foo", "bar");
foo(bar());
foo(bar("test"));
foo(bar("test"));
foo("foo", "bar");
@@ -1,3 +1,7 @@
new Foo;
new Foo();
new Foo(bar);
new Foo(
bar,
baz,
)
@@ -1,3 +1,4 @@
new Foo();
new Foo();
new Foo(bar);
new Foo(bar);
new Foo(bar, baz);
@@ -41,3 +41,9 @@ a.foo?.(bar())?.baz;
a.foo?.(bar("test"))?.baz;
a.foo(bar?.())?.baz;
a.foo(bar?.("test"))?.baz;
a?.foo(
"foo",
"bar",
)(
"baz",
)
@@ -40,4 +40,5 @@ a.foo?.("foo", "bar")?.baz;
a.foo?.(bar())?.baz;
a.foo?.(bar("test"))?.baz;
a.foo(bar?.())?.baz;
a.foo(bar?.("test"))?.baz;
a.foo(bar?.("test"))?.baz;
a?.foo("foo", "bar")("baz");