hosseinmd / prettier-plugin-jsdoc

A Prettier plugin to format JSDoc comments.
MIT License
228 stars 29 forks source link

Consider keeping `@type` comments single-line if possible #135

Closed G-Rath closed 11 months ago

G-Rath commented 2 years ago

Currently with jsdocSingleLineComment set to false, this:

/** @type {import('eslint').Linter.Config} */
const config = {
  // ...
};

Becomes

/**
 * @type {import('eslint').Linter.Config}
 */
const config = {
  // ...
};

Personally, I like having multi-line jsdoc comments for "documentation" comments aka generally anytime it has more than a sole @ tag; I'm less fussed about things like @private as they're typically on methods which should be documented anyway, but @types is very common when working in javascript codebases that are statically checked with TypeScript.

This also leads to casts being multi-lined which doesn't look quite right (and might even break the cast):

module.exports = (/** @type {string} */ (config));

// becomes

module.exports = /**
 * @type {string}
 */ (config);

Would it be possible for this plugin to only force a jsdoc comment to be multiline if it had anything except a single @ tag?

cobaltt7 commented 2 years ago

jsdocSingleLineComment: true will make it multiline only if it has more than one tag, or if it exceeds the max line length.

hosseinmd commented 2 years ago

set jsdocSingleLineComment to true will solve your issue

G-Rath commented 2 years ago

@hosseinmd that won't solve my issue:

Personally, I like having multi-line jsdoc comments for "documentation" comments aka generally anytime it has more than a sole @ tag;

That's why I have it set to false, because of #93.

hosseinmd commented 2 years ago

93 closed becuase of this commit https://github.com/hosseinmd/prettier-plugin-jsdoc/commit/f755db1e72af75685fbb997d2ef1fe346e8092ee

Give me a solotion for your request.

G-Rath commented 2 years ago

Would it be possible for this plugin to only force a jsdoc comment to be multiline if it had anything except a single @ tag?

That's the high-level solution, maybe with an exclude for @description since that'd include what #93 was about.

So it'd look like:

// single-line because it's only got one @ tag
/** @internal */
export const myProperty1 = 1;

// multi-line because it's got multiple @ tags
/**
 * @type {number}
 * @internal
 */
export const myProperty2 = 1;

// multi-line because it exceeds the line length
/**
 * @type {import('@organisation/my-organisations-package').DeeplyNested.Type.Lives.here}
 */
export const myProperty3 = 1;

function receiveStructsSet(aSetObject){
  // single-line because it only has one @ tag
  const mySet = /** @type {goog.structs.Set} */ (aSetObject);
}

class MyClass {
  // multi-line because it doesn't have an @ tag
  /**
   * This describes my method, it is awesome
   */
  myMethod1() {
    // ...
  }

  // multi-line because it has a @description tag
  /**
   * @description This describes my method, it is awesome
   */
  myMethod2() {
    // ...
  }

  // single-line because it's just got one @ tag
  /** @private */
  myMethod3() {
    // ...
  }

  // single-line because it's just got one @ tag
  /** @private */
  myMethod4() {
    // ...
  }

  // multi-line because it's got multiple @ tag
  /**
   * @private
   * @internal
   */
  myMethod5() {
    // ...
  }

  // single-line because it's just got one @ tag
  /** @private */
  myMethod6() {
    // ...
  }

  // multi-line because it's got multiple @ tags
  /**
   * @private
   * @internal
   */
  myMethod7() {
    // ...
  }
}

I'm wondering if it might be worth supporting this as an option that takes an array that represents the tags to allow being single-line, with the default being @type 🤔

I'm happy to have a go at making a PR implementing this either as it's own new option, or as the default behaviour for jsdocSingleLineComment :)

hosseinmd commented 2 years ago

PR is welcome

tjx666 commented 1 year ago

I think jsdocSingleLineComment option should be a enum type, and provide:

Weather should be one line is diffrent in diffrent case. So there should be one option to keep they are

This is the only reasone forbid me to using this plugin

tjx666 commented 1 year ago

@G-Rath maybe this issue can be closed?

G-Rath commented 1 year ago

@tjx666 why do you say that?

tjx666 commented 1 year ago

Sorry for that message, lone time no see this issue, but see there is a option jsdocSingleLineComment...

G-Rath commented 1 year ago

jsdocSingleLineComment does not address this issue - please read the original body and my follow-on comments for why.

hosseinmd commented 1 year ago

I think jsdocSingleLineComment option should be a enum type, and provide:

  • singleLine
  • multipleLine
  • keep

Weather should be one line is diffrent in diffrent case. So there should be one option to keep they are

This is the only reasone forbid me to using this plugin

This is much better. But we should discuss options.

hosseinmd commented 11 months ago

implemented jsdocCommentLineStrategy ("singleLine","multiline","keep")

deprecated jsdocSingleLineComment has backward compatibility

v1.1.0