mirror of
https://github.com/hanzoai/babel.git
synced 2026-09-24 16:56:45 +00:00
fix: preserve trailing comma in optional call args (#17781)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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",
|
||||
)
|
||||
|
||||
+2
-1
@@ -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");
|
||||
Reference in New Issue
Block a user