diff --git a/src/printer.js b/src/printer.js index a0a53d9c..d1e6b976 100644 --- a/src/printer.js +++ b/src/printer.js @@ -556,6 +556,9 @@ function genericPrintNoParens(path, options, print, args) { (args && args.expandLastArg) || path.getParentNode().type === "JSXExpressionContainer"; + const printTrailingComma = + args && args.expandLastArg && shouldPrintComma(options, "all"); + // In order to avoid confusion between // a => a ? a : a // a <= a ? a : a @@ -580,10 +583,7 @@ function genericPrintNoParens(path, options, print, args) { ]) ), shouldAddSoftLine - ? concat([ - ifBreak(shouldPrintComma(options, "all") ? "," : ""), - softline - ]) + ? concat([ifBreak(printTrailingComma ? "," : ""), softline]) : "" ]) ) diff --git a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap b/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap index 73d6d260..8649f223 100644 --- a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap +++ b/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap @@ -210,6 +210,63 @@ a( `; +exports[`jsx.js 1`] = ` +
+ doSomething({ + foo: bar + }) + } +/>; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
+ doSomething({ + foo: bar + }) + } +/>; + +`; + +exports[`jsx.js 2`] = ` +
+ doSomething({ + foo: bar + }) + } +/>; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
+ doSomething({ + foo: bar, + }) + } +/>; + +`; + +exports[`jsx.js 3`] = ` +
+ doSomething({ + foo: bar + }) + } +/>; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
+ doSomething({ + foo: bar, + }) + } +/>; + +`; + exports[`object.js 1`] = ` const a = { b: true, diff --git a/tests/trailing_comma/jsx.js b/tests/trailing_comma/jsx.js new file mode 100644 index 00000000..bef0b483 --- /dev/null +++ b/tests/trailing_comma/jsx.js @@ -0,0 +1,7 @@ +
+ doSomething({ + foo: bar + }) + } +/>;