Fixed braceless type tags with types starting with an open parenthesis (#57167)

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
Mateusz Burzyński 2024-04-06 00:38:53 +02:00 committed by GitHub
parent bdd1f947f6
commit 9ba0800646
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 167 additions and 1 deletions

View File

@ -2656,6 +2656,10 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
return token = SyntaxKind.OpenBracketToken;
case CharacterCodes.closeBracket:
return token = SyntaxKind.CloseBracketToken;
case CharacterCodes.openParen:
return token = SyntaxKind.OpenParenToken;
case CharacterCodes.closeParen:
return token = SyntaxKind.CloseParenToken;
case CharacterCodes.lessThan:
return token = SyntaxKind.LessThanToken;
case CharacterCodes.greaterThan:

View File

@ -764,6 +764,8 @@ export type JSDocSyntaxKind =
| SyntaxKind.GreaterThanToken
| SyntaxKind.OpenBracketToken
| SyntaxKind.CloseBracketToken
| SyntaxKind.OpenParenToken
| SyntaxKind.CloseParenToken
| SyntaxKind.EqualsToken
| SyntaxKind.CommaToken
| SyntaxKind.DotToken

View File

@ -4134,7 +4134,7 @@ declare namespace ts {
type KeywordTypeSyntaxKind = SyntaxKind.AnyKeyword | SyntaxKind.BigIntKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.IntrinsicKeyword | SyntaxKind.NeverKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.VoidKeyword;
type TokenSyntaxKind = SyntaxKind.Unknown | SyntaxKind.EndOfFileToken | TriviaSyntaxKind | LiteralSyntaxKind | PseudoLiteralSyntaxKind | PunctuationSyntaxKind | SyntaxKind.Identifier | KeywordSyntaxKind;
type JsxTokenSyntaxKind = SyntaxKind.LessThanSlashToken | SyntaxKind.EndOfFileToken | SyntaxKind.ConflictMarkerTrivia | SyntaxKind.JsxText | SyntaxKind.JsxTextAllWhiteSpaces | SyntaxKind.OpenBraceToken | SyntaxKind.LessThanToken;
type JSDocSyntaxKind = SyntaxKind.EndOfFileToken | SyntaxKind.WhitespaceTrivia | SyntaxKind.AtToken | SyntaxKind.NewLineTrivia | SyntaxKind.AsteriskToken | SyntaxKind.OpenBraceToken | SyntaxKind.CloseBraceToken | SyntaxKind.LessThanToken | SyntaxKind.GreaterThanToken | SyntaxKind.OpenBracketToken | SyntaxKind.CloseBracketToken | SyntaxKind.EqualsToken | SyntaxKind.CommaToken | SyntaxKind.DotToken | SyntaxKind.Identifier | SyntaxKind.BacktickToken | SyntaxKind.HashToken | SyntaxKind.Unknown | KeywordSyntaxKind;
type JSDocSyntaxKind = SyntaxKind.EndOfFileToken | SyntaxKind.WhitespaceTrivia | SyntaxKind.AtToken | SyntaxKind.NewLineTrivia | SyntaxKind.AsteriskToken | SyntaxKind.OpenBraceToken | SyntaxKind.CloseBraceToken | SyntaxKind.LessThanToken | SyntaxKind.GreaterThanToken | SyntaxKind.OpenBracketToken | SyntaxKind.CloseBracketToken | SyntaxKind.OpenParenToken | SyntaxKind.CloseParenToken | SyntaxKind.EqualsToken | SyntaxKind.CommaToken | SyntaxKind.DotToken | SyntaxKind.Identifier | SyntaxKind.BacktickToken | SyntaxKind.HashToken | SyntaxKind.Unknown | KeywordSyntaxKind;
enum NodeFlags {
None = 0,
Let = 1,

View File

@ -0,0 +1,31 @@
index.js(3,3): error TS2322: Type 'number' is not assignable to type 'string'.
index.js(20,16): error TS2322: Type '"other"' is not assignable to type '"foo" | "bar"'.
==== index.js (2 errors) ====
/** @type () => string */
function fn1() {
return 42;
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
}
/** @type () => string */
function fn2() {
return "foo";
}
/** @type (arg: string) => string */
function fn3(arg) {
return arg;
}
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj1 = { type: "foo", prop: 10 };
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj2 = { type: "other", prop: 10 };
~~~~
!!! error TS2322: Type '"other"' is not assignable to type '"foo" | "bar"'.
!!! related TS6500 index.js:19:14: The expected type comes from property 'type' which is declared here on type '({ type: "foo"; } | { type: "bar"; }) & { prop: number; }'

View File

@ -0,0 +1,38 @@
//// [tests/cases/compiler/jsdocBracelessTypeTag1.ts] ////
=== index.js ===
/** @type () => string */
function fn1() {
>fn1 : Symbol(fn1, Decl(index.js, 0, 0))
return 42;
}
/** @type () => string */
function fn2() {
>fn2 : Symbol(fn2, Decl(index.js, 3, 1))
return "foo";
}
/** @type (arg: string) => string */
function fn3(arg) {
>fn3 : Symbol(fn3, Decl(index.js, 8, 1))
>arg : Symbol(arg, Decl(index.js, 11, 13))
return arg;
>arg : Symbol(arg, Decl(index.js, 11, 13))
}
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj1 = { type: "foo", prop: 10 };
>obj1 : Symbol(obj1, Decl(index.js, 16, 5))
>type : Symbol(type, Decl(index.js, 16, 14))
>prop : Symbol(prop, Decl(index.js, 16, 27))
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj2 = { type: "other", prop: 10 };
>obj2 : Symbol(obj2, Decl(index.js, 19, 5))
>type : Symbol(type, Decl(index.js, 19, 14))
>prop : Symbol(prop, Decl(index.js, 19, 29))

View File

@ -0,0 +1,65 @@
//// [tests/cases/compiler/jsdocBracelessTypeTag1.ts] ////
=== index.js ===
/** @type () => string */
function fn1() {
>fn1 : () => string
> : ^^^^^^
return 42;
>42 : 42
> : ^^
}
/** @type () => string */
function fn2() {
>fn2 : () => string
> : ^^^^^^
return "foo";
>"foo" : "foo"
> : ^^^^^
}
/** @type (arg: string) => string */
function fn3(arg) {
>fn3 : (arg: string) => string
> : ^ ^^ ^^^^^
>arg : string
> : ^^^^^^
return arg;
>arg : string
> : ^^^^^^
}
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj1 = { type: "foo", prop: 10 };
>obj1 : ({ type: "foo"; } | { type: "bar"; }) & { prop: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ type: "foo", prop: 10 } : { type: "foo"; prop: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>type : "foo"
> : ^^^^^
>"foo" : "foo"
> : ^^^^^
>prop : number
> : ^^^^^^
>10 : 10
> : ^^
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj2 = { type: "other", prop: 10 };
>obj2 : ({ type: "foo"; } | { type: "bar"; }) & { prop: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ type: "other", prop: 10 } : { type: "other"; prop: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>type : "other"
> : ^^^^^^^
>"other" : "other"
> : ^^^^^^^
>prop : number
> : ^^^^^^
>10 : 10
> : ^^

View File

@ -0,0 +1,26 @@
// @strict: true
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @filename: index.js
/** @type () => string */
function fn1() {
return 42;
}
/** @type () => string */
function fn2() {
return "foo";
}
/** @type (arg: string) => string */
function fn3(arg) {
return arg;
}
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj1 = { type: "foo", prop: 10 };
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
const obj2 = { type: "other", prop: 10 };