diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 2c81b2054..e301bd9fa 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -5508,7 +5508,21 @@ function printComment(commentPath, options) { case "CommentBlock": case "Block": { if (isJsDocComment(comment)) { - return printJsDocComment(comment); + const printed = printJsDocComment(comment); + // We need to prevent an edge case of a previous trailing comment + // printed as a `lineSuffix` which causes the comments to be + // interleaved. See https://github.com/prettier/prettier/issues/4412 + if ( + comment.trailing && + !privateUtil.hasNewline( + options.originalText, + options.locStart(comment), + { backwards: true } + ) + ) { + return concat([hardline, printed]); + } + return printed; } const isInsideFlowComment = diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index ccf9db933..0bf73efb5 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -1637,6 +1637,60 @@ exports[`trailing_space.js 1`] = ` `; +exports[`trailing-jsdocs.js 1`] = ` +const CONNECTION_STATUS = exports.CONNECTION_STATUS = { + CLOSED: Object.freeze({ kind: 'CLOSED' }), + CONNECTED: Object.freeze({ kind: 'CONNECTED' }), + CONNECTING: Object.freeze({ kind: 'CONNECTING' }), + NOT_CONNECTED: Object.freeze({ kind: 'NOT_CONNECTED' }) }; + +/* A comment */ /** +* A type that can be written to a buffer. +*/ /** +* Describes the connection status of a ReactiveSocket/DuplexConnection. +* - NOT_CONNECTED: no connection established or pending. +* - CONNECTING: when \`connect()\` has been called but a connection is not yet +* established. +* - CONNECTED: when a connection is established. +* - CLOSED: when the connection has been explicitly closed via \`close()\`. +* - ERROR: when the connection has been closed for any other reason. +*/ /** +* A contract providing different interaction models per the [ReactiveSocket protocol] +* (https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md). +*/ /** +* A single unit of data exchanged between the peers of a \`ReactiveSocket\`. +*/ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const CONNECTION_STATUS = (exports.CONNECTION_STATUS = { + CLOSED: Object.freeze({ kind: "CLOSED" }), + CONNECTED: Object.freeze({ kind: "CONNECTED" }), + CONNECTING: Object.freeze({ kind: "CONNECTING" }), + NOT_CONNECTED: Object.freeze({ kind: "NOT_CONNECTED" }) +}); + +/* A comment */ +/** + * A type that can be written to a buffer. + */ +/** + * Describes the connection status of a ReactiveSocket/DuplexConnection. + * - NOT_CONNECTED: no connection established or pending. + * - CONNECTING: when \`connect()\` has been called but a connection is not yet + * established. + * - CONNECTED: when a connection is established. + * - CLOSED: when the connection has been explicitly closed via \`close()\`. + * - ERROR: when the connection has been closed for any other reason. + */ +/** + * A contract providing different interaction models per the [ReactiveSocket protocol] + * (https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md). + */ +/** + * A single unit of data exchanged between the peers of a \`ReactiveSocket\`. + */ + +`; + exports[`try.js 1`] = ` // comment 1 try { diff --git a/tests/comments/trailing-jsdocs.js b/tests/comments/trailing-jsdocs.js new file mode 100644 index 000000000..9c562d2d2 --- /dev/null +++ b/tests/comments/trailing-jsdocs.js @@ -0,0 +1,22 @@ +const CONNECTION_STATUS = exports.CONNECTION_STATUS = { + CLOSED: Object.freeze({ kind: 'CLOSED' }), + CONNECTED: Object.freeze({ kind: 'CONNECTED' }), + CONNECTING: Object.freeze({ kind: 'CONNECTING' }), + NOT_CONNECTED: Object.freeze({ kind: 'NOT_CONNECTED' }) }; + +/* A comment */ /** +* A type that can be written to a buffer. +*/ /** +* Describes the connection status of a ReactiveSocket/DuplexConnection. +* - NOT_CONNECTED: no connection established or pending. +* - CONNECTING: when `connect()` has been called but a connection is not yet +* established. +* - CONNECTED: when a connection is established. +* - CLOSED: when the connection has been explicitly closed via `close()`. +* - ERROR: when the connection has been closed for any other reason. +*/ /** +* A contract providing different interaction models per the [ReactiveSocket protocol] +* (https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md). +*/ /** +* A single unit of data exchanged between the peers of a `ReactiveSocket`. +*/