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
Changes from all 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
44 changes: 39 additions & 5 deletions internal/parser/reparser.go
Original file line number Diff line number Diff line change
@@ -224,10 +224,10 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
case 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*/)
case 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*/)
case ast.KindExpressionStatement:
if parent.AsExpressionStatement().Expression.Kind == ast.KindBinaryExpression {
bin := parent.AsExpressionStatement().Expression.AsBinaryExpression()
@@ -236,6 +236,11 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
}
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 {
@@ -266,6 +271,31 @@ 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.

params := fun.Parameters()
if len(params) == 0 || params[0].Name().Kind != ast.KindThisKeyword {
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 {
@@ -351,7 +381,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 {
@@ -398,8 +427,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.NewAsExpression(e, t)
} else {
assert = p.factory.NewSatisfiesExpression(e, t)
}
assert.Flags = p.contextFlags | ast.NodeFlagsReparsed
assert.Loc = core.NewTextRange(e.Pos(), e.End())
return assert
15 changes: 8 additions & 7 deletions internal/testutil/tsbaseline/type_symbol_baseline.go
Original file line number Diff line number Diff line change
@@ -327,14 +327,15 @@ func forEachASTNode(node *ast.Node) []*ast.Node {
for len(work) > 0 {
elem := work[len(work)-1]
work = work[:len(work)-1]
if elem.Flags&ast.NodeFlagsReparsed != 0 && elem.Kind != ast.KindTypeAssertionExpression {
continue
if elem.Flags&ast.NodeFlagsReparsed == 0 || elem.Kind == ast.KindAsExpression || elem.Kind == ast.KindSatisfiesExpression {
if elem.Flags&ast.NodeFlagsReparsed == 0 {
result = append(result, elem)
}
elem.ForEachChild(addChild)
slices.Reverse(resChildren)
work = append(work, resChildren...)
resChildren = resChildren[:0]
}
result = append(result, elem)
elem.ForEachChild(addChild)
slices.Reverse(resChildren)
work = append(work, resChildren...)
resChildren = resChildren[:0]
}
return result
}
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ const foo1 = value => /** @type {string} */({ ...value });
>value => /** @type {string} */({ ...value }) : <T>(value: T | undefined) => T
>value : T | undefined
>({ ...value }) : string
>{ ...value } : string
>{ ...value } : {}
>value : T | undefined

@@ -25,9 +24,7 @@ const foo2 = value => /** @type {string} */(/** @type {T} */({ ...value }));
>value => /** @type {string} */(/** @type {T} */({ ...value })) : <T>(value: T | undefined) => T
>value : T | undefined
>(/** @type {T} */({ ...value })) : string
>({ ...value }) : string
>({ ...value }) : T
>{ ...value } : T
>{ ...value } : {}
>value : T | undefined

Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ const cloneObjectGood = value => /** @type {T} */({ ...value });
>value => /** @type {T} */({ ...value }) : <T>(value: T | undefined) => T
>value : T | undefined
>({ ...value }) : T
>{ ...value } : T
>{ ...value } : {}
>value : T | undefined

Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@ module.exports = /** @type {FooFun} */(void 0);
>module : { "export=": (foo: typeof import("./file")) => string; }
>exports : (foo: typeof import("./file")) => string
>(void 0) : (foo: typeof import("./file")) => string
>void 0 : (foo: typeof import("./file")) => string
>void 0 : undefined
>0 : 0

Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ function foo(fns) {

return /** @type {any} */ (null);
>(null) : any
>null : any
}

const result = foo({
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ function filter(predicate) {

return /** @type {any} */ (null);
>(null) : any
>null : any
}

const a = filter(
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@

export default /** @type {NumberLike[]} */([ ]);
>([ ]) : (string | number)[]
>[ ] : (string | number)[]
>[ ] : undefined[]

=== b.ts ===
Original file line number Diff line number Diff line change
@@ -89,7 +89,6 @@ function flatMap(array, iterable = identity) {
>push : (...items: unknown[]) => number
>.../** @type {unknown[]} */(iterable(array[i])) : unknown
>(iterable(array[i])) : unknown[]
>iterable(array[i]) : unknown[]
>iterable(array[i]) : unknown
>iterable : (x: unknown) => unknown
>array[i] : unknown
Original file line number Diff line number Diff line change
@@ -84,7 +84,6 @@ function flatMap(array, iterable = identity) {
>push : (...items: unknown[]) => number
>.../** @type {unknown[]} */(iterable(array[i])) : unknown
>(iterable(array[i])) : unknown[]
>iterable(array[i]) : unknown[]
>iterable(array[i]) : unknown
>iterable : (x: unknown) => unknown
>array[i] : unknown
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@ export default _default;
export default function () {
return /** @type {import('./GeometryType.js').default} */ ('Point');
>('Point') : any
>'Point' : any
>'Point' : "Point"
}

Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@
let c = /** @type {'a' | 'b'} */ (x); // Ok
>c : "a" | "b"
>(x) : "a" | "b"
>x : "a" | "b"
>x : string

c;
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@ const getStringGetter = (key) => {

return /** @type {string} */ (cache.get(key))
>(cache.get(key)) : string
>cache.get(key) : string
>cache.get(key) : string | Set<string> | undefined
>cache.get : (key: string) => string | Set<string> | undefined
>cache : Map<string, string | Set<string>>
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ let value = "";

switch (/** @type {"foo" | "bar"} */ (value)) {
>(value) : "bar" | "foo"
>value : "bar" | "foo"
>value : string

case "bar":
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ function source(type = "javascript") {
>( type ? sources.get(type) : sources.get("some other thing") ) : String

type
>type ? sources.get(type) : sources.get("some other thing") : String
>type ? sources.get(type) : sources.get("some other thing") : string | undefined
>type : string

Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@
const x = /** @type {Foo} */ ({});
>x : Foo
>({}) : Foo
>{} : Foo
>{} : {}

x.foo; // number | undefined
@@ -20,7 +19,6 @@ x.foo; // number | undefined
const y = /** @type {Required<Foo>} */ ({});
>y : Required<Foo>
>({}) : Required<Foo>
>{} : Required<Foo>
>{} : {}

y.foo; // number
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[]} */
@@ -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)
}

@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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))
@@ -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))
Original file line number Diff line number Diff line change
@@ -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))
@@ -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))
@@ -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
@@ -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)
@@ -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
@@ -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
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ const foo = (a) => {
>(a).y !== 0 : boolean
>(a).y : number
>(a) : { x: number; } & { y: number; }
>a : { x: number; } & { y: number; }
>a : { x: number; }
>y : number
>0 : 0
Original file line number Diff line number Diff line change
@@ -62,9 +62,9 @@ const t5 = /** @satisfies {T3} */((m) => m.substring(0));
>0 : 0

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

Loading
Oops, something went wrong.