Eslint-plugin-jsdoc: Disallow optional parameters notation with question mark

Created on 4 Feb 2020  路  4Comments  路  Source: gajus/eslint-plugin-jsdoc

With auto fix

I would like to disallow optional parameters notation with question mark character ?.

Disallow this:

/**
 * @param {string?} foo Optional parameter.
 */

and this:

/**
 * @param {?string} foo Optional parameter.
 */

In favor of this (auto fixed):

/**
 * @param {string} [foo] Optional parameter.
 */

Actually both {string?} and {?string} cannot be disallowed and leads to inconstancy when writing optional parameter notation.

Also I'm not sure about the {string} [foo] notation, is this really the "official" notation to use when writing optional parameters?

I think so 馃 https://jsdoc.app/tags-param.html#optional-parameters-and-default-values

An optional parameter (using JSDoc syntax)

/**
 * @param {string} [somebody] - Somebody's name.
 */
function sayHello(somebody) {
    if (!somebody) {
        somebody = 'John Doe';
    }
    alert('Hello ' + somebody);
}

Most helpful comment

I believe you are confusing _optional_ and _nullable_ types. ?string is nullable and essentially an equivalent of (string|null) both in vanilla and GCC.

@param {?string} foo is a perfectly valid tag which means function parameter foo is mandatory and could be either string or null.

Optional parameter would be either @param {string=} foo or @param {string} [foo] and roughly equivalent of @param {string|undefined} foo. ECMAScript Default Parameters are fine too.

All 4 comments

I believe you are confusing _optional_ and _nullable_ types. ?string is nullable and essentially an equivalent of (string|null) both in vanilla and GCC.

@param {?string} foo is a perfectly valid tag which means function parameter foo is mandatory and could be either string or null.

Optional parameter would be either @param {string=} foo or @param {string} [foo] and roughly equivalent of @param {string|undefined} foo. ECMAScript Default Parameters are fine too.

Oh yeah right, thank you!

And is there a way to disallow _nullable_ types? I'm pretty sure my team doesn't have any ?string nullable use-case 馃し鈥嶁檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ZacBrownBand picture ZacBrownBand  路  6Comments

michaelfarrell76 picture michaelfarrell76  路  4Comments

deraw picture deraw  路  3Comments

OmgImAlexis picture OmgImAlexis  路  5Comments

tdmalone picture tdmalone  路  5Comments