-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Template tag allows specification of constraints #24600
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
13 commits
Select commit
Hold shift + click to select a range
b208b4f
Parse (and mostly support) template tag constraints
sandersn e986333
Messy version is finished. Need to add a few tests
sandersn e629fc9
Refactor to be smaller
sandersn da9f937
Small refactor + Add one test
sandersn 24fdef6
Another test
sandersn efd8a4f
Minor cleanup
sandersn d499fde
Fix error reporting on type parameters on ctors
sandersn 781bc65
Simplify syntax of `@template` tag
sandersn e5e9e43
Better error message for template tag
sandersn f6d56f3
Fix fourslash baselines
sandersn 0019f69
Another fourslash update
sandersn 3ce4b24
Address PR comments
sandersn e85d472
Simplify getEffectiveTypeParameterDeclarations
sandersn 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
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
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
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
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
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
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
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
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/jsdocTemplateConstructorFunction.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
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/jsdocTemplateConstructorFunction.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
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
2 changes: 1 addition & 1 deletion
2
tests/baselines/reference/jsdocTemplateConstructorFunction2.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
2 changes: 1 addition & 1 deletion
2
tests/baselines/reference/jsdocTemplateConstructorFunction2.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
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,47 @@ | ||
tests/cases/conformance/jsdoc/a.js(14,29): error TS2339: Property 'a' does not exist on type 'U'. | ||
tests/cases/conformance/jsdoc/a.js(14,35): error TS2339: Property 'b' does not exist on type 'U'. | ||
tests/cases/conformance/jsdoc/a.js(21,3): error TS2345: Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: string; }'. | ||
Property 'b' is missing in type '{ a: number; }'. | ||
tests/cases/conformance/jsdoc/a.js(25,2): error TS1069: Unexpected token. A type parameter name was expected without curly braces. | ||
|
||
|
||
==== tests/cases/conformance/jsdoc/a.js (4 errors) ==== | ||
/** | ||
* @template {{ a: number, b: string }} T,U A Comment | ||
* @template {{ c: boolean }} V uh ... are comments even supported?? | ||
* @template W | ||
* @template X That last one had no comment | ||
* @param {T} t | ||
* @param {U} u | ||
* @param {V} v | ||
* @param {W} w | ||
* @param {X} x | ||
* @return {W | X} | ||
*/ | ||
function f(t, u, v, w, x) { | ||
if(t.a + t.b.length > u.a - u.b.length && v.c) { | ||
~ | ||
!!! error TS2339: Property 'a' does not exist on type 'U'. | ||
~ | ||
!!! error TS2339: Property 'b' does not exist on type 'U'. | ||
return w; | ||
} | ||
return x; | ||
} | ||
|
||
f({ a: 12, b: 'hi', c: null }, undefined, { c: false, d: 12, b: undefined }, 101, 'nope'); | ||
f({ a: 12 }, undefined, undefined, 101, 'nope'); | ||
~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: string; }'. | ||
!!! error TS2345: Property 'b' is missing in type '{ a: number; }'. | ||
|
||
/** | ||
* @template {NoLongerAllowed} | ||
* @template T preceding line's syntax is no longer allowed | ||
~ | ||
!!! error TS1069: Unexpected token. A type parameter name was expected without curly braces. | ||
* @param {T} x | ||
*/ | ||
function g(x) { } | ||
|
||
|
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,70 @@ | ||
=== tests/cases/conformance/jsdoc/a.js === | ||
/** | ||
* @template {{ a: number, b: string }} T,U A Comment | ||
* @template {{ c: boolean }} V uh ... are comments even supported?? | ||
* @template W | ||
* @template X That last one had no comment | ||
* @param {T} t | ||
* @param {U} u | ||
* @param {V} v | ||
* @param {W} w | ||
* @param {X} x | ||
* @return {W | X} | ||
*/ | ||
function f(t, u, v, w, x) { | ||
>f : Symbol(f, Decl(a.js, 0, 0)) | ||
>t : Symbol(t, Decl(a.js, 12, 11)) | ||
>u : Symbol(u, Decl(a.js, 12, 13)) | ||
>v : Symbol(v, Decl(a.js, 12, 16)) | ||
>w : Symbol(w, Decl(a.js, 12, 19)) | ||
>x : Symbol(x, Decl(a.js, 12, 22)) | ||
|
||
if(t.a + t.b.length > u.a - u.b.length && v.c) { | ||
>t.a : Symbol(a, Decl(a.js, 1, 15)) | ||
>t : Symbol(t, Decl(a.js, 12, 11)) | ||
>a : Symbol(a, Decl(a.js, 1, 15)) | ||
>t.b.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>t.b : Symbol(b, Decl(a.js, 1, 26)) | ||
>t : Symbol(t, Decl(a.js, 12, 11)) | ||
>b : Symbol(b, Decl(a.js, 1, 26)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>u : Symbol(u, Decl(a.js, 12, 13)) | ||
>u : Symbol(u, Decl(a.js, 12, 13)) | ||
>v.c : Symbol(c, Decl(a.js, 2, 15)) | ||
>v : Symbol(v, Decl(a.js, 12, 16)) | ||
>c : Symbol(c, Decl(a.js, 2, 15)) | ||
|
||
return w; | ||
>w : Symbol(w, Decl(a.js, 12, 19)) | ||
} | ||
return x; | ||
>x : Symbol(x, Decl(a.js, 12, 22)) | ||
} | ||
|
||
f({ a: 12, b: 'hi', c: null }, undefined, { c: false, d: 12, b: undefined }, 101, 'nope'); | ||
>f : Symbol(f, Decl(a.js, 0, 0)) | ||
>a : Symbol(a, Decl(a.js, 19, 3)) | ||
>b : Symbol(b, Decl(a.js, 19, 10)) | ||
>c : Symbol(c, Decl(a.js, 19, 19)) | ||
>undefined : Symbol(undefined) | ||
>c : Symbol(c, Decl(a.js, 19, 43)) | ||
>d : Symbol(d, Decl(a.js, 19, 53)) | ||
>b : Symbol(b, Decl(a.js, 19, 60)) | ||
>undefined : Symbol(undefined) | ||
|
||
f({ a: 12 }, undefined, undefined, 101, 'nope'); | ||
>f : Symbol(f, Decl(a.js, 0, 0)) | ||
>a : Symbol(a, Decl(a.js, 20, 3)) | ||
>undefined : Symbol(undefined) | ||
>undefined : Symbol(undefined) | ||
|
||
/** | ||
* @template {NoLongerAllowed} | ||
* @template T preceding line's syntax is no longer allowed | ||
* @param {T} x | ||
*/ | ||
function g(x) { } | ||
>g : Symbol(g, Decl(a.js, 20, 49)) | ||
>x : Symbol(x, Decl(a.js, 27, 11)) | ||
|
||
|
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.
Was already this way, but it looks like
parseTemplateTag
is the only tag-parsing method out of 9 that can return undefined. Might be better to callparseJSDocIdentifierName
withcreateIfMissing
set, and a new parameter to allow a custom diagnostic. ThanparseTag
could returnJSDocTag
(without| undefined
).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.
parseTag returns undefined near the beginning if it can't parseJSDocIdentifierName, too. As-is the change is not worth it in my opinion. It might be worthwhile to have parseJSDocIdentifierName always return a missing node. I tried that and it looks a bit better, but there's a good bit of churn. Would you like to review it as part of this PR or would you like to see it in a separate one?
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.
Separate is good.