-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Changes from 12 commits
b208b4f
e986333
e629fc9
da9f937
24fdef6
efd8a4f
d499fde
781bc65
e5e9e43
f6d56f3
0019f69
3ce4b24
e85d472
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7013,29 +7013,27 @@ namespace ts { | |
| } | ||
|
|
||
| function parseTemplateTag(atToken: AtToken, tagName: Identifier): JSDocTemplateTag | undefined { | ||
| if (some(tags, isJSDocTemplateTag)) { | ||
| parseErrorAt(tagName.pos, scanner.getTokenPos(), Diagnostics._0_tag_already_specified, tagName.escapedText); | ||
| // the template tag looks like '@template {Constraint} T,U,V' | ||
| let constraint: JSDocTypeExpression | undefined; | ||
| if (token() === SyntaxKind.OpenBraceToken) { | ||
| constraint = parseJSDocTypeExpression(); | ||
| skipWhitespace(); | ||
| } | ||
|
|
||
| // Type parameter list looks like '@template T,U,V' | ||
| const typeParameters = []; | ||
| const typeParametersPos = getNodePos(); | ||
|
|
||
| while (true) { | ||
| const typeParameter = <TypeParameterDeclaration>createNode(SyntaxKind.TypeParameter); | ||
| const name = parseJSDocIdentifierNameWithOptionalBraces(); | ||
| skipWhitespace(); | ||
| if (!name) { | ||
| parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected); | ||
| if (!tokenIsIdentifierOrKeyword(token())) { | ||
| parseErrorAtCurrentToken(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces); | ||
| return undefined; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was already this way, but it looks like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Separate is good. |
||
| } | ||
|
|
||
| typeParameter.name = name; | ||
| typeParameter.name = parseJSDocIdentifierName()!; | ||
| skipWhitespace(); | ||
| finishNode(typeParameter); | ||
|
|
||
| typeParameters.push(typeParameter); | ||
|
|
||
| if (token() === SyntaxKind.CommaToken) { | ||
| // need to look for more type parameters | ||
| nextJSDocToken(); | ||
| skipWhitespace(); | ||
| } | ||
|
|
@@ -7044,6 +7042,10 @@ namespace ts { | |
| } | ||
| } | ||
|
|
||
| if (constraint) { | ||
| first(typeParameters).constraint = constraint.type; | ||
| } | ||
|
|
||
| const result = <JSDocTemplateTag>createNode(SyntaxKind.JSDocTemplateTag, atToken.pos); | ||
| result.atToken = atToken; | ||
| result.tagName = tagName; | ||
|
|
@@ -7052,15 +7054,6 @@ namespace ts { | |
| return result; | ||
| } | ||
|
|
||
| function parseJSDocIdentifierNameWithOptionalBraces(): Identifier | undefined { | ||
| const parsedBrace = parseOptional(SyntaxKind.OpenBraceToken); | ||
| const res = parseJSDocIdentifierName(); | ||
| if (parsedBrace) { | ||
| parseExpected(SyntaxKind.CloseBraceToken); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| function nextJSDocToken(): JsDocSyntaxKind { | ||
| return currentToken = scanner.scanJSDocToken(); | ||
| } | ||
|
|
||
| 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) { } | ||
|
|
||
|
|
| 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)) | ||
|
|
||
|
|
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.
when is it not a NodeArray?
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.
getEffectiveTypeParameterDeclarations returns a ReadonlyArray now. Previously it returned
templateTags[0].typeParameters, which was a NodeArray. Now it returnsflatMap(templateTags, t => t.typeParameters), which is a ReadonlyArray.I don't think it's worth the trouble to create a NodeArray because the resulting span would stretch across all 3 template tags below, which is very ugly:
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 the past we stuck a
posand anendon the result to make it look like a NodeArray.. why can not we do the same here?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.
actually looking at the code, that is exactly what we do.. it looks like it will always be a NodeArray
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 getEffectiveTypeParameterDeclarations, the code path that calls getJSDocTypeParameterDeclarations ends up calling flatMap, which is what returns the non-NodeArray:
The jsdoc signature case returns emptyArray, which is also not a NodeArray.