Skip to content

Add this/satisfies tag #1157

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 9 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,22 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
} else if parent.Kind == ast.KindReturnStatement {
ret := parent.AsReturnStatement()
ret.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), ret.Expression)
ret.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), ret.Expression, true /*isAssertion*/)
} else if parent.Kind == ast.KindParenthesizedExpression {
paren := parent.AsParenthesizedExpression()
paren.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), paren.Expression)
paren.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), paren.Expression, true /*isAssertion*/)
} else if parent.Kind == ast.KindExpressionStatement &&
parent.AsExpressionStatement().Expression.Kind == ast.KindBinaryExpression {
bin := parent.AsExpressionStatement().Expression.AsBinaryExpression()
if kind := ast.GetAssignmentDeclarationKind(bin); kind != ast.JSDeclarationKindNone {
bin.Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, parent.AsExpressionStatement().Expression)
}
}
case ast.KindJSDocSatisfiesTag:
if parent.Kind == ast.KindParenthesizedExpression {
paren := parent.AsParenthesizedExpression()
paren.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocSatisfiesTag().TypeExpression, nil), paren.Expression, false /*isAssertion*/)
}
case ast.KindJSDocTemplateTag:
if fun, ok := getFunctionLikeHost(parent); ok {
if fun.TypeParameters() == nil {
Expand Down Expand Up @@ -265,6 +270,32 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
}
case ast.KindJSDocThisTag:
if fun, ok := getFunctionLikeHost(parent); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this supposed to do for function-like-expressions that don't have a this, like arrow functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Only add a this parameter if we don't already have one
params := fun.Parameters()
if len(params) == 0 || params[0].Name().Text() != "this" {
thisParam := p.factory.NewParameterDeclaration(
nil, /* decorators */
nil, /* modifiers */
p.factory.NewIdentifier("this"),
nil, /* questionToken */
nil, /* type */
nil, /* initializer */
)
thisParam.AsParameterDeclaration().Type = p.makeNewType(tag.AsJSDocThisTag().TypeExpression, thisParam)
thisParam.Loc = tag.AsJSDocThisTag().TagName.Loc
thisParam.Flags = p.contextFlags | ast.NodeFlagsReparsed

newParams := p.nodeSlicePool.NewSlice(len(params) + 1)
newParams[0] = thisParam
for i, param := range params {
newParams[i+1] = param
}

fun.FunctionLikeData().Parameters = p.newNodeList(thisParam.Loc, newParams)
}
}
case ast.KindJSDocReturnTag:
if fun, ok := getFunctionLikeHost(parent); ok {
if fun.Type() == nil {
Expand Down Expand Up @@ -319,7 +350,6 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
}
// !!! other attached tags (@this, @satisfies) support goes here
}

func (p *Parser) makeQuestionIfOptional(parameter *ast.JSDocParameterTag) *ast.Node {
Expand Down Expand Up @@ -366,8 +396,13 @@ func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) {
return nil, false
}

func (p *Parser) makeNewTypeAssertion(t *ast.TypeNode, e *ast.Node) *ast.Node {
assert := p.factory.NewTypeAssertion(t, e)
func (p *Parser) makeNewCast(t *ast.TypeNode, e *ast.Node, isAssertion bool) *ast.Node {
var assert *ast.Node
if isAssertion {
assert = p.factory.NewTypeAssertion(t, e)
} else {
assert = p.factory.NewSatisfiesExpression(e, t)
}
assert.Flags = p.contextFlags | ast.NodeFlagsReparsed
assert.Loc = core.NewTextRange(e.Pos(), e.End())
return assert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/a.js(9,26): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
/a.js(15,31): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
/a.js(23,31): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
/a.js(31,26): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.


==== /a.js (4 errors) ====
==== /a.js (2 errors) ====
class Test {
constructor() {
/** @type {number[]} */
Expand Down Expand Up @@ -34,9 +32,6 @@
/** @this {Test} */
function (d) {
console.log(d === this.data.length)
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
!!! related TS2738 /a.js:22:9: An outer value of 'this' is shadowed by this container.
}, this)
}

Expand All @@ -45,9 +40,6 @@
/** @this {Test} */
function (d) {
return d === this.data.length
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
!!! related TS2738 /a.js:30:9: An outer value of 'this' is shadowed by this container.
}, this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class Test {
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(log, Decl(lib.dom.d.ts, --, --))
>d : Symbol(d, Decl(a.js, 21, 18))
>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
>this.data : Symbol(data, Decl(a.js, 1, 19))
>this : Symbol((Missing), Decl(a.js, 20, 13))
>data : Symbol(data, Decl(a.js, 1, 19))
>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Expand All @@ -89,6 +94,11 @@ class Test {

return d === this.data.length
>d : Symbol(d, Decl(a.js, 29, 18))
>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
>this.data : Symbol(data, Decl(a.js, 1, 19))
>this : Symbol((Missing), Decl(a.js, 28, 13))
>data : Symbol(data, Decl(a.js, 1, 19))
>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
->this : Symbol(this)
->data : Symbol(Test.data, Decl(a.js, 1, 19))
->length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
+>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
+>this.data : Symbol(data, Decl(a.js, 1, 19))
+>this : Symbol((Missing), Decl(a.js, 20, 13))
+>data : Symbol(data, Decl(a.js, 1, 19))
+>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Expand All @@ -111,7 +116,7 @@

/** @this {Test} */
function (d) {
@@= skipped -64, +59 lines =@@
@@= skipped -64, +64 lines =@@

return d === this.data.length
>d : Symbol(d, Decl(a.js, 29, 18))
Expand All @@ -120,6 +125,11 @@
->this : Symbol(this)
->data : Symbol(Test.data, Decl(a.js, 1, 19))
->length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
+>this.data.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
+>this.data : Symbol(data, Decl(a.js, 1, 19))
+>this : Symbol((Missing), Decl(a.js, 28, 13))
+>data : Symbol(data, Decl(a.js, 1, 19))
+>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

}, this)
>this : Symbol(Test, Decl(a.js, 0, 0))
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Test {

/** @this {Test} */
function (d) {
>function (d) { console.log(d === this.data.length) } : (d: number) => void
>function (d) { console.log(d === this.data.length) } : (this: Test, d: number) => void
>d : number

console.log(d === this.data.length)
Expand All @@ -94,11 +94,11 @@ class Test {
>log : (...data: any[]) => void
>d === this.data.length : boolean
>d : number
>this.data.length : any
>this.data : any
>this : any
>data : any
>length : any
>this.data.length : number
>this.data : number[]
>this : Test
>data : number[]
>length : number

}, this)
>this : this
Expand All @@ -117,17 +117,17 @@ class Test {

/** @this {Test} */
function (d) {
>function (d) { return d === this.data.length } : (d: number) => boolean
>function (d) { return d === this.data.length } : (this: Test, d: number) => boolean
>d : number

return d === this.data.length
>d === this.data.length : boolean
>d : number
>this.data.length : any
>this.data : any
>this : any
>data : any
>length : any
>this.data.length : number
>this.data : number[]
>this : Test
>data : number[]
>length : number

}, this)
>this : this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,27 @@

const t1 = /** @satisfies {T1} */ ({ a: 1 });
>t1 : Symbol(t1, Decl(a.js, 19, 5))
>a : Symbol(a, Decl(a.js, 19, 36))

const t2 = /** @satisfies {T1} */ ({ a: 1, b: 1 });
>t2 : Symbol(t2, Decl(a.js, 20, 5))
>a : Symbol(a, Decl(a.js, 20, 36))
>b : Symbol(b, Decl(a.js, 20, 42))

const t3 = /** @satisfies {T1} */ ({});
>t3 : Symbol(t3, Decl(a.js, 21, 5))

/** @type {T2} */
const t4 = /** @satisfies {T2} */ ({ a: "a" });
>t4 : Symbol(t4, Decl(a.js, 24, 5))
>a : Symbol(a, Decl(a.js, 24, 36))

/** @type {(m: string) => string} */
const t5 = /** @satisfies {T3} */((m) => m.substring(0));
>t5 : Symbol(t5, Decl(a.js, 27, 5))
>m : Symbol(m, Decl(a.js, 27, 35))
>m.substring : Symbol(substring, Decl(lib.es5.d.ts, --, --))
>m : Symbol(m, Decl(a.js, 27, 35))
>substring : Symbol(substring, Decl(lib.es5.d.ts, --, --))

const t6 = /** @satisfies {[number, number]} */ ([1, 2]);
>t6 : Symbol(t6, Decl(a.js, 28, 5))

const t7 = /** @satisfies {T4} */ ({ a: 'test' });
>t7 : Symbol(t7, Decl(a.js, 29, 5))
>a : Symbol(a, Decl(a.js, 29, 36))

const t8 = /** @satisfies {T4} */ ({ a: 'test', b: 'test' });
>t8 : Symbol(t8, Decl(a.js, 30, 5))
>a : Symbol(a, Decl(a.js, 30, 36))
>b : Symbol(b, Decl(a.js, 30, 47))

Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
--- old.checkJsdocSatisfiesTag1.symbols
+++ new.checkJsdocSatisfiesTag1.symbols
@@= skipped -40, +40 lines =@@
@@= skipped -21, +21 lines =@@

const t1 = /** @satisfies {T1} */ ({ a: 1 });
>t1 : Symbol(t1, Decl(a.js, 19, 5))
->a : Symbol(a, Decl(a.js, 19, 36))

const t2 = /** @satisfies {T1} */ ({ a: 1, b: 1 });
>t2 : Symbol(t2, Decl(a.js, 20, 5))
->a : Symbol(a, Decl(a.js, 20, 36))
->b : Symbol(b, Decl(a.js, 20, 42))

const t3 = /** @satisfies {T1} */ ({});
>t3 : Symbol(t3, Decl(a.js, 21, 5))
@@= skipped -13, +10 lines =@@
/** @type {T2} */
const t4 = /** @satisfies {T2} */ ({ a: "a" });
>t4 : Symbol(t4, Decl(a.js, 24, 5))
->a : Symbol(a, Decl(a.js, 24, 36))

/** @type {(m: string) => string} */
const t5 = /** @satisfies {T3} */((m) => m.substring(0));
>t5 : Symbol(t5, Decl(a.js, 27, 5))
>m : Symbol(m, Decl(a.js, 27, 35))
->m : Symbol(m, Decl(a.js, 27, 35))
->m.substring : Symbol(String.substring, Decl(lib.es5.d.ts, --, --))
+>m.substring : Symbol(substring, Decl(lib.es5.d.ts, --, --))
>m : Symbol(m, Decl(a.js, 27, 35))
->m : Symbol(m, Decl(a.js, 27, 35))
->substring : Symbol(String.substring, Decl(lib.es5.d.ts, --, --))
+>substring : Symbol(substring, Decl(lib.es5.d.ts, --, --))

const t6 = /** @satisfies {[number, number]} */ ([1, 2]);
>t6 : Symbol(t6, Decl(a.js, 28, 5))
>t6 : Symbol(t6, Decl(a.js, 28, 5))

const t7 = /** @satisfies {T4} */ ({ a: 'test' });
>t7 : Symbol(t7, Decl(a.js, 29, 5))
->a : Symbol(a, Decl(a.js, 29, 36))

const t8 = /** @satisfies {T4} */ ({ a: 'test', b: 'test' });
>t8 : Symbol(t8, Decl(a.js, 30, 5))
->a : Symbol(a, Decl(a.js, 30, 36))
->b : Symbol(b, Decl(a.js, 30, 47))
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,34 @@
const t1 = /** @satisfies {T1} */ ({ a: 1 });
>t1 : { a: number; }
>({ a: 1 }) : { a: number; }
>{ a: 1 } : { a: number; }
>a : number
>1 : 1

const t2 = /** @satisfies {T1} */ ({ a: 1, b: 1 });
>t2 : { a: number; b: number; }
>({ a: 1, b: 1 }) : { a: number; b: number; }
>{ a: 1, b: 1 } : { a: number; b: number; }
>a : number
>1 : 1
>b : number
>1 : 1

const t3 = /** @satisfies {T1} */ ({});
>t3 : {}
>({}) : {}
>{} : {}

/** @type {T2} */
const t4 = /** @satisfies {T2} */ ({ a: "a" });
>t4 : T2
>({ a: "a" }) : { a: string; }
>{ a: "a" } : { a: string; }
>a : string
>"a" : "a"

/** @type {(m: string) => string} */
const t5 = /** @satisfies {T3} */((m) => m.substring(0));
>t5 : (m: string) => string
>((m) => m.substring(0)) : (m: string) => string
>(m) => m.substring(0) : (m: string) => string
>m : string
>m.substring(0) : string
>m.substring : (start: number, end?: number) => string
>m : string
>substring : (start: number, end?: number) => string
>0 : 0

const t6 = /** @satisfies {[number, number]} */ ([1, 2]);
>t6 : number[]
>([1, 2]) : number[]
>[1, 2] : number[]
>1 : 1
>2 : 2
>t6 : [number, number]
>([1, 2]) : [number, number]

const t7 = /** @satisfies {T4} */ ({ a: 'test' });
>t7 : { a: string; }
>({ a: 'test' }) : { a: string; }
>{ a: 'test' } : { a: string; }
>a : string
>'test' : "test"

const t8 = /** @satisfies {T4} */ ({ a: 'test', b: 'test' });
>t8 : { a: string; b: string; }
>({ a: 'test', b: 'test' }) : { a: string; b: string; }
>{ a: 'test', b: 'test' } : { a: string; b: string; }
>a : string
>'test' : "test"
>b : string
>'test' : "test"

This file was deleted.

Loading
Loading