Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10331,8 +10331,7 @@ func (c *Checker) checkSatisfiesExpression(node *ast.Node) *Type {
if c.isErrorType(targetType) {
return targetType
}
errorNode := core.IfElse(typeNode.Flags&ast.NodeFlagsReparsed != 0, typeNode, node)
c.checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, errorNode, node.Expression(), diagnostics.Type_0_does_not_satisfy_the_expected_type_1, nil)
c.checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, node, node.Expression(), diagnostics.Type_0_does_not_satisfy_the_expected_type_1, nil)
return exprType
}

Expand Down
18 changes: 14 additions & 4 deletions internal/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,20 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
pos := SkipTrivia(sourceFile.Text(), node.Pos())
return GetRangeOfTokenAtPosition(sourceFile, pos)
case ast.KindSatisfiesExpression:
if node.AsSatisfiesExpression().Type.Flags&ast.NodeFlagsReparsed != 0 {
for current := node.Parent; current != nil; current = current.Parent {
for _, jsDoc := range current.JSDoc(nil) {
if tags := jsDoc.AsJSDoc().Tags; tags != nil {
for _, tag := range tags.Nodes {
if ast.IsJSDocSatisfiesTag(tag) {
pos := SkipTrivia(sourceFile.Text(), tag.TagName().Pos())
return GetRangeOfTokenAtPosition(sourceFile, pos)
}
}
}
}
}
Comment on lines +2539 to +2550
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not super fond of this but I couldn't find a better way to go from a reparsed node to the original JSDoc right now, and I tried a bunch. Am I missing something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure there's anything better, given we are in the scanner package and therefore if such a helper existed in astnav it would be unavailable.

But maybe @gabritto / @andrewbranch have a better thought on their minds.

Comment on lines +2539 to +2550
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

When the satisfies type is reparsed, this returns the first @satisfies tag found on any ancestor JSDoc. If a JSDoc comment contains multiple @satisfies tags (which the reparser will apply by wrapping the initializer multiple times), this can highlight the wrong tag for inner/outer SatisfiesExpression nodes. Consider selecting the tag whose TypeExpression.Type().Loc (or pos/end) matches node.AsSatisfiesExpression().Type.Loc, and only fall back to the first tag if no match is found.

Suggested change
for current := node.Parent; current != nil; current = current.Parent {
for _, jsDoc := range current.JSDoc(nil) {
if tags := jsDoc.AsJSDoc().Tags; tags != nil {
for _, tag := range tags.Nodes {
if ast.IsJSDocSatisfiesTag(tag) {
pos := SkipTrivia(sourceFile.Text(), tag.TagName().Pos())
return GetRangeOfTokenAtPosition(sourceFile, pos)
}
}
}
}
}
targetType := node.AsSatisfiesExpression().Type
var firstSatisfiesTag *ast.Node
for current := node.Parent; current != nil; current = current.Parent {
for _, jsDoc := range current.JSDoc(nil) {
if tags := jsDoc.AsJSDoc().Tags; tags != nil {
for _, tag := range tags.Nodes {
if ast.IsJSDocSatisfiesTag(tag) {
if firstSatisfiesTag == nil {
firstSatisfiesTag = tag
}
typeExpr := tag.AsJSDocSatisfiesTag().TypeExpression()
if typeExpr != nil {
if typeNode := typeExpr.Type(); typeNode != nil {
if typeNode.Pos() == targetType.Pos() && typeNode.End() == targetType.End() {
pos := SkipTrivia(sourceFile.Text(), tag.TagName().Pos())
return GetRangeOfTokenAtPosition(sourceFile, pos)
}
}
}
}
}
}
}
}
if firstSatisfiesTag != nil {
pos := SkipTrivia(sourceFile.Text(), firstSatisfiesTag.TagName().Pos())
return GetRangeOfTokenAtPosition(sourceFile, pos)
}

Copilot uses AI. Check for mistakes.
Comment on lines +2539 to +2550
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

current.JSDoc(nil) triggers a GetSourceFileOfNode walk each time (since file is nil), and this code is already walking up the parent chain. This makes the reparsed-@satisfies path potentially O(depth^2). Consider computing sourceFile := ast.GetSourceFileOfNode(node) once and using current.JSDoc(sourceFile) inside the loop.

Copilot uses AI. Check for mistakes.
}
pos := SkipTrivia(sourceFile.Text(), node.AsSatisfiesExpression().Expression.End())
return GetRangeOfTokenAtPosition(sourceFile, pos)
case ast.KindConstructor:
Expand All @@ -2548,10 +2562,6 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
scanner.Scan()
}
return core.NewTextRange(start, scanner.TokenEnd())
// !!!
// case KindJSDocSatisfiesTag:
// pos := scanner.SkipTrivia(sourceFile.Text(), node.tagName.pos)
// return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
}
if errorNode == nil {
// If we don't have a better node, then just set the error on the first token of
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/a.js(21,44): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
/a.js(22,28): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(22,17): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(31,49): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T4'.


Expand Down Expand Up @@ -28,7 +28,7 @@
~
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
const t3 = /** @satisfies {T1} */ ({});
~~
~~~~~~~~~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
!!! related TS2728 /a.js:3:23: 'a' is declared here.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
/a.js(21,44): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
-/a.js(22,17): error TS1360: Type '{}' does not satisfy the expected type 'T1'.
- Property 'a' is missing in type '{}' but required in type 'T1'.
+/a.js(22,28): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
+/a.js(22,17): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(31,49): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T4'.


@@= skipped -28, +27 lines =@@
~
@@= skipped -29, +28 lines =@@
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
const t3 = /** @satisfies {T1} */ ({});
- ~~~~~~~~~
~~~~~~~~~
-!!! error TS1360: Type '{}' does not satisfy the expected type 'T1'.
-!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'T1'.
-!!! related TS2728 /a.js:3:4: 'a' is declared here.
+ ~~
+!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
+!!! related TS2728 /a.js:3:23: 'a' is declared here.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/a.js(24,20): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
/a.js(27,16): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(27,5): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(44,25): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
/a.js(51,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.
/a.js(51,6): error TS1360: Type 'number' does not satisfy the expected type 'string'.


==== /a.js (4 errors) ====
Expand Down Expand Up @@ -34,7 +34,7 @@

/**
* @satisfies {T1}
~~
~~~~~~~~~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
!!! related TS2728 /a.js:3:23: 'a' is declared here.
*/
Expand Down Expand Up @@ -63,6 +63,6 @@
const t7 = { a: "a" };

/** @satisfies {string} */ const t8 = (1);
~~~~~~
~~~~~~~~~
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
+++ new.checkJsdocSatisfiesTag12.errors.txt
@@= skipped -0, +0 lines =@@
/a.js(24,20): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
+/a.js(27,16): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
+/a.js(27,5): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(44,25): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
-/a.js(51,6): error TS1360: Type 'number' does not satisfy the expected type 'string'.
-
-
/a.js(51,6): error TS1360: Type 'number' does not satisfy the expected type 'string'.


-==== /a.js (3 errors) ====
+/a.js(51,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.
+
+
+==== /a.js (4 errors) ====
/**
* @typedef {Object} T1
Expand All @@ -19,17 +16,9 @@

/**
* @satisfies {T1}
+ ~~
+ ~~~~~~~~~
+!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
+!!! related TS2728 /a.js:3:23: 'a' is declared here.
*/
const t3 = {};

@@= skipped -26, +29 lines =@@
const t7 = { a: "a" };

/** @satisfies {string} */ const t8 = (1);
- ~~~~~~~~~
+ ~~~~~~
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/a.js(5,32): error TS2741: Property 'a' is missing in type '{}' but required in type 'Foo'.
/a.js(5,21): error TS2741: Property 'a' is missing in type '{}' but required in type 'Foo'.


==== /a.js (1 errors) ====
Expand All @@ -7,7 +7,7 @@
* @property {number} a
*/
export default /** @satisfies {Foo} */ ({});
~~~
~~~~~~~~~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'Foo'.
!!! related TS2728 /a.js:3:23: 'a' is declared here.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
@@= skipped -0, +0 lines =@@
-/a.js(5,21): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
- Property 'a' is missing in type '{}' but required in type 'Foo'.
+/a.js(5,32): error TS2741: Property 'a' is missing in type '{}' but required in type 'Foo'.
+/a.js(5,21): error TS2741: Property 'a' is missing in type '{}' but required in type 'Foo'.


==== /a.js (1 errors) ====
@@= skipped -7, +6 lines =@@
* @property {number} a
@@= skipped -8, +7 lines =@@
*/
export default /** @satisfies {Foo} */ ({});
- ~~~~~~~~~
~~~~~~~~~
-!!! error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
-!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'Foo'.
-!!! related TS2728 /a.js:3:4: 'a' is declared here.
+ ~~~
+!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'Foo'.
+!!! related TS2728 /a.js:3:23: 'a' is declared here.

Expand Down
Loading