Brackets and postfix= in @param add undefined#22514
Merged
Merged
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.
Member
Author
|
This should be ported to 2.8.1 as well. |
ghost
reviewed
Mar 13, 2018
| let isOptional = false; | ||
| if (includeOptionality) { | ||
| if (isInJavaScriptFile(declaration) && isParameterDeclaration(declaration)) { | ||
| const parameterTags = getJSDocParameterTags(declaration as ParameterDeclaration); |
There was a problem hiding this comment.
Cast not valid, isParameterDeclaration returns true for non-parameters (maybe the name could use an improvement).
Member
Author
There was a problem hiding this comment.
Yeah, isParameter isn't obvious since it's one of the few types that don't match the name of their syntax kind.
| 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.
Simpler to write isOptional = ... than if (...) isOptional = true;
Member
Author
There was a problem hiding this comment.
I guess so. Doesn't match the next branch, so it makes the code a little less obvious to read.
ghost
approved these changes
Mar 13, 2018
sandersn
added a commit
that referenced
this pull request
Mar 13, 2018
* 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
Member
Author
|
I cherry-picked it into 2.8 too. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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