-
Notifications
You must be signed in to change notification settings - Fork 13.4k
JSDoc ?Type adds optionality to parameters
#22646
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
tests/baselines/reference/jsdocPostfixEqualsAddsOptionality.errors.txt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| tests/cases/conformance/jsdoc/a.js(4,5): error TS2322: Type 'null' is not assignable to type 'number | undefined'. | ||
| tests/cases/conformance/jsdoc/a.js(8,3): error TS2345: Argument of type 'null' is not assignable to parameter of type 'number | undefined'. | ||
|
|
||
|
|
||
| ==== tests/cases/conformance/jsdoc/a.js (2 errors) ==== | ||
| /** @param {number=} a */ | ||
| function f(a) { | ||
| a = 1 | ||
| a = null // should not be allowed | ||
| ~ | ||
| !!! error TS2322: Type 'null' is not assignable to type 'number | undefined'. | ||
| a = undefined | ||
| } | ||
| f() | ||
| f(null) // should not be allowed | ||
| ~~~~ | ||
| !!! error TS2345: Argument of type 'null' is not assignable to parameter of type 'number | undefined'. | ||
| f(undefined) | ||
| f(1) | ||
|
|
||
| /** @param {???!?number?=} a */ | ||
| function g(a) { | ||
| a = 1 | ||
| a = null | ||
| a = undefined | ||
| } | ||
| g() | ||
| g(null) | ||
| g(undefined) | ||
| g(1) | ||
|
|
57 changes: 57 additions & 0 deletions
57
tests/baselines/reference/jsdocPostfixEqualsAddsOptionality.symbols
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| === tests/cases/conformance/jsdoc/a.js === | ||
| /** @param {number=} a */ | ||
| function f(a) { | ||
| >f : Symbol(f, Decl(a.js, 0, 0)) | ||
| >a : Symbol(a, Decl(a.js, 1, 11)) | ||
|
|
||
| a = 1 | ||
| >a : Symbol(a, Decl(a.js, 1, 11)) | ||
|
|
||
| a = null // should not be allowed | ||
| >a : Symbol(a, Decl(a.js, 1, 11)) | ||
|
|
||
| a = undefined | ||
| >a : Symbol(a, Decl(a.js, 1, 11)) | ||
| >undefined : Symbol(undefined) | ||
| } | ||
| f() | ||
| >f : Symbol(f, Decl(a.js, 0, 0)) | ||
|
|
||
| f(null) // should not be allowed | ||
| >f : Symbol(f, Decl(a.js, 0, 0)) | ||
|
|
||
| f(undefined) | ||
| >f : Symbol(f, Decl(a.js, 0, 0)) | ||
| >undefined : Symbol(undefined) | ||
|
|
||
| f(1) | ||
| >f : Symbol(f, Decl(a.js, 0, 0)) | ||
|
|
||
| /** @param {???!?number?=} a */ | ||
| function g(a) { | ||
| >g : Symbol(g, Decl(a.js, 9, 4)) | ||
| >a : Symbol(a, Decl(a.js, 12, 11)) | ||
|
|
||
| a = 1 | ||
| >a : Symbol(a, Decl(a.js, 12, 11)) | ||
|
|
||
| a = null | ||
| >a : Symbol(a, Decl(a.js, 12, 11)) | ||
|
|
||
| a = undefined | ||
| >a : Symbol(a, Decl(a.js, 12, 11)) | ||
| >undefined : Symbol(undefined) | ||
| } | ||
| g() | ||
| >g : Symbol(g, Decl(a.js, 9, 4)) | ||
|
|
||
| g(null) | ||
| >g : Symbol(g, Decl(a.js, 9, 4)) | ||
|
|
||
| g(undefined) | ||
| >g : Symbol(g, Decl(a.js, 9, 4)) | ||
| >undefined : Symbol(undefined) | ||
|
|
||
| g(1) | ||
| >g : Symbol(g, Decl(a.js, 9, 4)) | ||
|
|
79 changes: 79 additions & 0 deletions
79
tests/baselines/reference/jsdocPostfixEqualsAddsOptionality.types
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| === tests/cases/conformance/jsdoc/a.js === | ||
| /** @param {number=} a */ | ||
| function f(a) { | ||
| >f : (a?: number | undefined) => void | ||
| >a : number | undefined | ||
|
|
||
| a = 1 | ||
| >a = 1 : 1 | ||
| >a : number | undefined | ||
| >1 : 1 | ||
|
|
||
| a = null // should not be allowed | ||
| >a = null : null | ||
| >a : number | undefined | ||
| >null : null | ||
|
|
||
| a = undefined | ||
| >a = undefined : undefined | ||
| >a : number | undefined | ||
| >undefined : undefined | ||
| } | ||
| f() | ||
| >f() : void | ||
| >f : (a?: number | undefined) => void | ||
|
|
||
| f(null) // should not be allowed | ||
| >f(null) : void | ||
| >f : (a?: number | undefined) => void | ||
| >null : null | ||
|
|
||
| f(undefined) | ||
| >f(undefined) : void | ||
| >f : (a?: number | undefined) => void | ||
| >undefined : undefined | ||
|
|
||
| f(1) | ||
| >f(1) : void | ||
| >f : (a?: number | undefined) => void | ||
| >1 : 1 | ||
|
|
||
| /** @param {???!?number?=} a */ | ||
| function g(a) { | ||
| >g : (a?: number | null | undefined) => void | ||
| >a : number | null | undefined | ||
|
|
||
| a = 1 | ||
| >a = 1 : 1 | ||
| >a : number | null | undefined | ||
| >1 : 1 | ||
|
|
||
| a = null | ||
| >a = null : null | ||
| >a : number | null | undefined | ||
| >null : null | ||
|
|
||
| a = undefined | ||
| >a = undefined : undefined | ||
| >a : number | null | undefined | ||
| >undefined : undefined | ||
| } | ||
| g() | ||
| >g() : void | ||
| >g : (a?: number | null | undefined) => void | ||
|
|
||
| g(null) | ||
| >g(null) : void | ||
| >g : (a?: number | null | undefined) => void | ||
| >null : null | ||
|
|
||
| g(undefined) | ||
| >g(undefined) : void | ||
| >g : (a?: number | null | undefined) => void | ||
| >undefined : undefined | ||
|
|
||
| g(1) | ||
| >g(1) : void | ||
| >g : (a?: number | null | undefined) => void | ||
| >1 : 1 | ||
|
|
Oops, something went wrong.
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.
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.
In
?!a=, maybe this should be parsed as(?!a)=and this wouldn't be necessary?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 considered that and will look at that next. The reason is that, for
?T=the parser doesparsePostfix at step (1) doesn't parse the trailing
=in this case like we would want, the one at step (6) does. This might be fixed by changing step (4) to be parseNonArrayType. I'll try that and see what breaks.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.
In fact, parseJSDocNodeWithType(SyntaxKind.JSDocNonNullable) does this already, so it's probably a mistake that parseJSDocNullableType doesn't.
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.
This breaks the existing parsing test in a way that I think is good (
...is no longer allowed in the middle of a type), but I want to test more and maybe revise the parsing of...too. I'll do that in a separate PR.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.
Hmm, it also changes how tightly
?Tbinds -- if it's tighter than=, then it's tighter than[]too. So?T[]is now(T | null)[]where before it wasT[] | null.