Skip to content
Merged
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
5 changes: 3 additions & 2 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -11001,7 +11001,6 @@ func (node *SourceFile) GetOrCreateToken(
) *TokenNode {
node.tokenCacheMu.Lock()
defer node.tokenCacheMu.Unlock()

loc := core.NewTextRange(pos, end)
if node.tokenCache == nil {
node.tokenCache = make(map[core.TextRange]*Node)
Expand All @@ -11014,7 +11013,9 @@ func (node *SourceFile) GetOrCreateToken(
}
return token
}

if parent.Flags&NodeFlagsReparsed != 0 {
panic(fmt.Sprintf("Cannot create token from reparsed node of kind %v", parent.Kind))
}
token := createToken(kind, node, pos, end, flags)
token.Loc = loc
token.Parent = parent
Expand Down
20 changes: 2 additions & 18 deletions internal/astnav/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ func getTokenAtPosition(
// `left` tracks the lower boundary of the node/token that could be returned,
// and is eventually the scanner's start position, if the scanner is used.
left := 0
// `allowReparsed` is set when we're navigating inside an AsExpression or
// SatisfiesExpression, which allows visiting their reparsed children to reach
// the actual identifier from JSDoc type assertions.
allowReparsed := false

testNode := func(node *ast.Node) int {
if node.Kind != ast.KindEndOfFile && node.End() == position && includePrecedingTokenAtEndPosition != nil {
Expand All @@ -92,15 +88,8 @@ func getTokenAtPosition(
// We can't abort visiting children, so once a match is found, we set `next`
// and do nothing on subsequent visits.
if node != nil && next == nil {
// Skip reparsed nodes unless:
// 1. The node itself is AsExpression or SatisfiesExpression, OR
// 2. We're already inside an AsExpression or SatisfiesExpression (allowReparsed=true)
// These are special cases where reparsed nodes from JSDoc type assertions
// should still be navigable to reach identifiers.
isSpecialReparsed := node.Flags&ast.NodeFlagsReparsed != 0 &&
(node.Kind == ast.KindAsExpression || node.Kind == ast.KindSatisfiesExpression)

if node.Flags&ast.NodeFlagsReparsed == 0 || isSpecialReparsed || allowReparsed {
// Skip reparsed nodes
if node.Flags&ast.NodeFlagsReparsed == 0 {
result := testNode(node)
switch result {
case -1:
Expand Down Expand Up @@ -211,11 +200,6 @@ func getTokenAtPosition(
current = next
left = current.Pos()
next = nil
// When navigating into AsExpression or SatisfiesExpression, allow visiting
// their reparsed children to reach identifiers from JSDoc type assertions.
if current.Kind == ast.KindAsExpression || current.Kind == ast.KindSatisfiesExpression {
allowReparsed = true
}
}
}

Expand Down
22 changes: 10 additions & 12 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10408,18 +10408,15 @@ func (c *Checker) getInstantiationExpressionType(exprType *Type, node *ast.Node)
}

func (c *Checker) checkSatisfiesExpression(node *ast.Node) *Type {
c.checkSourceElement(node.Type())
return c.checkSatisfiesExpressionWorker(node.Expression(), node.Type(), CheckModeNormal)
}

func (c *Checker) checkSatisfiesExpressionWorker(expression *ast.Node, target *ast.Node, checkMode CheckMode) *Type {
exprType := c.checkExpressionEx(expression, checkMode)
targetType := c.getTypeFromTypeNode(target)
typeNode := node.Type()
c.checkSourceElement(typeNode)
exprType := c.checkExpression(node.Expression())
targetType := c.getTypeFromTypeNode(typeNode)
if c.isErrorType(targetType) {
return targetType
}
errorNode := ast.FindAncestor(target.Parent, func(n *ast.Node) bool { return ast.IsSatisfiesExpression(n) })
c.checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, errorNode, expression, diagnostics.Type_0_does_not_satisfy_the_expected_type_1, nil)
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)
return exprType
}

Expand Down Expand Up @@ -11935,14 +11932,15 @@ func (c *Checker) checkAssertion(node *ast.Node, checkMode CheckMode) *Type {
}

func (c *Checker) checkAssertionDeferred(node *ast.Node) {
typeNode := node.Type()
exprType := c.getRegularTypeOfObjectLiteral(c.getBaseTypeOfLiteralType(c.assertionLinks.Get(node).exprType))
targetType := c.getTypeFromTypeNode(node.Type())
targetType := c.getTypeFromTypeNode(typeNode)
if !c.isErrorType(targetType) {
widenedType := c.getWidenedType(exprType)
if !c.isTypeComparableTo(targetType, widenedType) {
errNode := node
if node.Flags&ast.NodeFlagsReparsed != 0 {
errNode = node.Type()
if typeNode.Flags&ast.NodeFlagsReparsed != 0 {
errNode = typeNode
}
c.checkTypeComparableTo(exprType, targetType, errNode, diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first)
}
Expand Down
5 changes: 1 addition & 4 deletions internal/ls/folding.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ func (l *LanguageService) addRegionOutliningSpans(sourceFile *ast.SourceFile) []
}

func visitNode(ctx context.Context, n *ast.Node, depthRemaining int, sourceFile *ast.SourceFile, l *LanguageService) []*lsproto.FoldingRange {
if depthRemaining == 0 {
return nil
}
if ctx.Err() != nil {
if n.Flags&ast.NodeFlagsReparsed != 0 || depthRemaining == 0 || ctx.Err() != nil {
return nil
}
foldingRange := make([]*lsproto.FoldingRange, 0, 40)
Expand Down
14 changes: 7 additions & 7 deletions internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
if parent.Expression() != nil && tag.TypeExpression() != nil {
parent.AsMutable().SetExpression(p.makeNewCast(
p.factory.DeepCloneReparse(tag.TypeExpression().Type()),
p.factory.DeepCloneReparse(parent.Expression()),
parent.Expression(),
true /*isAssertion*/))
p.finishMutatedNode(parent)
return
Expand All @@ -335,7 +335,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
if declaration.Initializer() != nil && tag.TypeExpression() != nil {
declaration.AsMutable().SetInitializer(p.makeNewCast(
p.factory.DeepCloneReparse(tag.TypeExpression().Type()),
p.factory.DeepCloneReparse(declaration.Initializer()),
declaration.Initializer(),
false /*isAssertion*/))
p.finishMutatedNode(declaration)
break
Expand All @@ -348,7 +348,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
if parent.Initializer() != nil && tag.TypeExpression() != nil {
parent.AsMutable().SetInitializer(p.makeNewCast(
p.factory.DeepCloneReparse(tag.TypeExpression().Type()),
p.factory.DeepCloneReparse(parent.Initializer()),
parent.Initializer(),
false /*isAssertion*/))
p.finishMutatedNode(parent)
}
Expand All @@ -357,7 +357,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
if shorthand.ObjectAssignmentInitializer != nil && tag.AsJSDocSatisfiesTag().TypeExpression != nil {
shorthand.ObjectAssignmentInitializer = p.makeNewCast(
p.factory.DeepCloneReparse(tag.AsJSDocSatisfiesTag().TypeExpression.Type()),
p.factory.DeepCloneReparse(shorthand.ObjectAssignmentInitializer),
shorthand.ObjectAssignmentInitializer,
false /*isAssertion*/)
p.finishMutatedNode(parent)
}
Expand All @@ -366,7 +366,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
if parent.Expression() != nil && tag.TypeExpression() != nil {
parent.AsMutable().SetExpression(p.makeNewCast(
p.factory.DeepCloneReparse(tag.TypeExpression().Type()),
p.factory.DeepCloneReparse(parent.Expression()),
parent.Expression(),
false /*isAssertion*/))
p.finishMutatedNode(parent)
}
Expand All @@ -376,7 +376,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
if kind := ast.GetAssignmentDeclarationKind(bin); kind != ast.JSDeclarationKindNone && tag.TypeExpression() != nil {
bin.Right = p.makeNewCast(
p.factory.DeepCloneReparse(tag.TypeExpression().Type()),
p.factory.DeepCloneReparse(bin.Right),
bin.Right,
false /*isAssertion*/)
p.finishMutatedNode(bin.AsNode())
}
Expand Down Expand Up @@ -602,7 +602,7 @@ func (p *Parser) makeNewCast(t *ast.TypeNode, e *ast.Node, isAssertion bool) *as
} else {
assert = p.factory.NewSatisfiesExpression(e, t)
}
p.finishReparsedNode(assert, e)
p.finishNodeWithEnd(assert, e.Pos(), e.End())
return assert
}

Expand Down
1 change: 1 addition & 0 deletions internal/testutil/tsbaseline/type_symbol_baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func (walker *typeWriterWalker) writeTypeOrSymbol(node *ast.Node, isSymbolWalk b
// Don't try to get the type of something that's already a type.
// Exception for `T` in `type T = something` because that may evaluate to some interesting type.
if ast.IsPartOfTypeNode(node) ||
(node.Kind == ast.KindAsExpression || node.Kind == ast.KindSatisfiesExpression) && node.Type().Flags&ast.NodeFlagsReparsed != 0 ||
ast.IsIdentifier(node) &&
(ast.GetMeaningFromDeclaration(node.Parent)&ast.SemanticMeaningValue) == 0 &&
!(ast.IsTypeOrJSTypeAliasDeclaration(node.Parent) && node == node.Parent.Name()) {
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,38): error TS2741: 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(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,7 +4,7 @@
/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,38): error TS2741: 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(31,49): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T4'.


Expand All @@ -16,7 +16,7 @@
-!!! 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
Expand Up @@ -18,7 +18,6 @@
const t1 = { a: 1 };
>t1 : { a: number; }
>{ a: 1 } : { a: number; }
>{ a: 1 } : { a: number; }
>a : number
>1 : 1

Expand Down

This file was deleted.

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(29,14): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(27,16): 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,42): error TS1360: Type 'number' does not satisfy the expected type 'string'.
/a.js(51,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.


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

/**
* @satisfies {T1}
*/
const t3 = {};
~
~~
!!! 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 = {};

/**
* @satisfies {Array.<number, number>}
Expand All @@ -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,34 +2,34 @@
+++ 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(29,14): error TS2741: Property 'a' is missing in type '{}' but required in type 'T1'.
+/a.js(27,16): 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 (3 errors) ====
+/a.js(51,42): error TS1360: Type 'number' does not satisfy the expected type 'string'.
+/a.js(51,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.
+
+
+==== /a.js (4 errors) ====
/**
* @typedef {Object} T1
* @property {number} a
@@= skipped -34, +35 lines =@@
@@= skipped -32, +33 lines =@@

/**
* @satisfies {T1}
*/
const t3 = {};
+ ~
+ ~~
+!!! 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 = {};

/**
* @satisfies {Array.<number, number>}
@@= skipped -24, +27 lines =@@
@@= 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,43): error TS2741: 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 (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,7 +3,7 @@
@@= 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,43): error TS2741: 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 (1 errors) ====
Expand All @@ -15,7 +15,7 @@
-!!! 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