homer0 / prettier-plugin-jsdoc

A Prettier plugin to format JSDoc blocks.
MIT License
6 stars 2 forks source link

Inline comments without tags are not very readable #16

Open mech-tools opened 1 week ago

mech-tools commented 1 week ago

Hi,

When both jsdocUseInlineCommentForASingleTagBlock and jsdocExperimentalFormatCommentsWithoutTags are true Comments without tags are collapsed to a single line if able.

That's ok and more readable for quick type def like /** @type {string} */

But for a simple description, we get something like this: image

This feels (to me) much more readable: image

Another example with class: image image

Do you think this is smt that can be changed or added as a setting?

homer0 commented 4 hours ago

Hey @mech-tools 👋

I'll try and take a look today and see if there's an easy fix, but I won't promise you anything, the jsdocExperimentalFormatCommentsWithoutTags is not something I plan to get out of experimental; it was a request that was easy enough to add, but not a use case I plan to support nor expand on it.

mech-tools commented 4 hours ago

Hi @homer0

Thanks! Here is my workaround for the time being:

import { get, override } from "@homer0/prettier-plugin-jsdoc/src/fns/app.js";
import { loadFns } from "@homer0/prettier-plugin-jsdoc/src/loader.js";
import { getPlugin } from "@homer0/prettier-plugin-jsdoc/src/fns/getPlugin.js";
import { getRenderer } from "@homer0/prettier-plugin-jsdoc/src/fns/getParsers.js";
import { render } from "@homer0/prettier-plugin-jsdoc/src/fns/render.js";

loadFns();

const customGetRenderer = (options) => {
  const renderer = get(render)(options);
  return (column, block) => {
    const padding = " ".repeat(column + 1);
    const prefix = `${padding}* `;
    const lines = renderer(column, block);

    if (
      lines.length === 1 &&
      block.tags.length > 0 && //<------ Added this line
      options.jsdocUseInlineCommentForASingleTagBlock
    ) {
      return `* ${lines[0]} `;
    }

    const useLines = lines.map((line) => `${prefix}${line}`).join("\n");

    return `*\n${useLines}\n${padding}`;
  };
};

override(getRenderer, customGetRenderer);

export default get(getPlugin)();