-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Brackets and postfix= in @param
add undefined
#22514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Previously they only added optionality. Note that, unlike Typescript, when a parameter initializer is specified in jsdoc, it does not remove undefined in the *body* of the function. That's because TS will generate initialisation code, but JS won't, so the author will have to manually write code to remove undefined from the type. ```js /** @param {number} [a=101] */ function f(a) { // a: number | undefined here if (!a) { a = 101 } // a: number here } ``` Note that we don't check that 1. the initializer value is actually assigned to the parameter. 2. the initializer's type matches the declared type of the parameter. Pretty much we just parse it and leave it alone.
This should be ported to 2.8.1 as well. |
src/compiler/checker.ts
Outdated
let isOptional = false; | ||
if (includeOptionality) { | ||
if (isInJavaScriptFile(declaration) && isParameterDeclaration(declaration)) { | ||
const parameterTags = getJSDocParameterTags(declaration as ParameterDeclaration); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cast not valid, isParameterDeclaration
returns true for non-parameters (maybe the name could use an improvement).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, isParameter
isn't obvious since it's one of the few types that don't match the name of their syntax kind.
src/compiler/checker.ts
Outdated
if (includeOptionality) { | ||
if (isInJavaScriptFile(declaration) && isParameterDeclaration(declaration)) { | ||
const parameterTags = getJSDocParameterTags(declaration as ParameterDeclaration); | ||
if (parameterTags && parameterTags.length > 0 && find(parameterTags, tag => tag.isBracketed)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simpler to write isOptional = ...
than if (...) isOptional = true;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess so. Doesn't match the next branch, so it makes the code a little less obvious to read.
* Brackets and postfix= in `@param` add undefined Previously they only added optionality. Note that, unlike Typescript, when a parameter initializer is specified in jsdoc, it does not remove undefined in the *body* of the function. That's because TS will generate initialisation code, but JS won't, so the author will have to manually write code to remove undefined from the type. ```js /** @param {number} [a=101] */ function f(a) { // a: number | undefined here if (!a) { a = 101 } // a: number here } ``` Note that we don't check that 1. the initializer value is actually assigned to the parameter. 2. the initializer's type matches the declared type of the parameter. Pretty much we just parse it and leave it alone. * Address PR comments
I cherry-picked it into 2.8 too. |
Previously they only added optionality.
Note that, unlike Typescript, when a parameter initializer is specified in jsdoc, it does not remove undefined in the body of the function. That's because TS will generate initialisation code, but JS won't, so the author will have to manually write code to remove undefined from the type.
Note that we don't check that
Pretty much we just parse it and leave it alone.
Fixes #22412