A Prettier plugin that reflows /** ... */ doc comment blocks in JavaScript and
TypeScript files to respect printWidth. All other formatting is delegated to
Prettier's built-in parsers and printers.
pnpm add -D prettier prettier-js-commentsAdd the plugin to your Prettier config:
{
"plugins": ["prettier-js-comments"],
"printWidth": 80
}Then format files as usual:
pnpm prettier --write "**/*.{js,jsx,ts,tsx}"Given a file like this with printWidth: 40:
/**
* This is a long doc comment that is too wide for the configured print width and will be reflowed.
*/
function foo() {}The plugin rewrites the comment to:
/**
* This is a long doc comment that is
* too wide for the configured print
* width and will be reflowed.
*/
function foo() {}A single-line doc comment that exceeds printWidth is expanded to multiline:
/** This is a long doc comment that exceeds the print width. */Becomes:
/**
* This is a long doc comment that
* exceeds the print width.
*/- Only
/** ... */doc comment blocks are touched. Regular block comments (/* */) and line comments (//) are left alone. - The
/**must be the only content on its line. Inline doc comments after code are not modified. - If any JSDoc tag (
@param,@returns, etc.) is present in the block, the entire block is skipped. - Consecutive text lines inside a doc comment are joined and reflowed together as one paragraph.
- The indent of the opening
/**line is used for the reflowed output. printWidthis measured from column 0, so a deeper indent yields a narrower reflowed width.- A bare
*line inside a doc comment is a paragraph break. - Separators (
* ----), bullets (* - item), and indented content (code examples) are emitted verbatim and act as paragraph boundaries. - Words are never broken. A word longer than the available width overflows its line.
MIT