Skip to content

Signature help implementation #861

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 45 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f7b2484
initial implementation
navya9singh Apr 1, 2025
202e970
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Apr 1, 2025
fff762c
adding test setup
navya9singh Apr 18, 2025
cbdb32b
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Apr 22, 2025
5d8476c
cleanup
navya9singh Apr 22, 2025
dcd021f
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Apr 22, 2025
473fbed
adding more tests and fixes
navya9singh Apr 29, 2025
0d73105
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Apr 29, 2025
d68643b
adding more tests and fixes
navya9singh Apr 30, 2025
ed043a8
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Apr 30, 2025
e8c5dae
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Apr 30, 2025
0c01e98
adding more tests
navya9singh May 12, 2025
7acd18e
cleanup
navya9singh May 12, 2025
4a359ec
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 12, 2025
0ccbd20
removing exported function
navya9singh May 12, 2025
151f03a
removing getSymbolOfDeclaration export
navya9singh May 12, 2025
85cdb01
cleanup
navya9singh May 12, 2025
a22ab20
removing deleted code
navya9singh May 12, 2025
e57cf78
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 12, 2025
50a3bbe
removing submodule updates
navya9singh May 12, 2025
4bc1228
fixing tests
navya9singh May 15, 2025
f415ca2
fixing formatting and lint errors
navya9singh May 15, 2025
ace6765
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 15, 2025
e836eb1
small fix
navya9singh May 15, 2025
31bfa2a
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 15, 2025
aafc78f
adding noembed
navya9singh May 15, 2025
1bf462c
adding exported functions to export.go
navya9singh May 16, 2025
8f34a4f
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 16, 2025
7a4c633
addressing pr comments
navya9singh May 19, 2025
d8db5cc
addressing comments
navya9singh May 20, 2025
9c81431
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 20, 2025
628f4b4
cleanup
navya9singh May 20, 2025
0ff32ac
addressing comments
navya9singh May 24, 2025
7469666
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 24, 2025
e621d71
fixing merge conflicts
navya9singh May 24, 2025
7792a6e
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 28, 2025
21b43cd
renames
navya9singh May 28, 2025
c61cf81
removing pointer to slice
navya9singh May 28, 2025
5b1f838
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 28, 2025
98de4c1
fixing lint errors
navya9singh May 28, 2025
093ff05
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh May 29, 2025
8fca88c
update with node builder
navya9singh Jun 2, 2025
09d531a
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Jun 2, 2025
ddb78d2
addressing comments
navya9singh Jun 3, 2025
9a3b8f7
Merge branch 'main' of https://github.com/navya9singh/typescript-go-r…
navya9singh Jun 3, 2025
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
12 changes: 12 additions & 0 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,18 @@ func IsStringOrNumericLiteralLike(node *Node) bool {
return IsStringLiteralLike(node) || IsNumericLiteral(node)
}

func IsStringTextContainingNode(node *Node) bool {
return node.Kind == KindStringLiteral || IsTemplateLiteralKind(node.Kind)
}

func IsTemplateLiteralKind(kind Kind) bool {
return KindFirstTemplateToken <= kind && kind <= KindLastTemplateToken
}

func IsTemplateLiteralToken(node *Node) bool {
return IsTemplateLiteralKind(node.Kind)
}

func IsSignedNumericLiteral(node *Node) bool {
if node.Kind == KindPrefixUnaryExpression {
node := node.AsPrefixUnaryExpression()
Expand Down
10 changes: 10 additions & 0 deletions internal/astnav/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,13 @@ func getNodeVisitor(
},
})
}

// !!!
func HasQuestionToken(node *ast.Node) bool {
Copy link
Member

Choose a reason for hiding this comment

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

Should this go into the ast package?

switch node.Kind {
case ast.KindParameter, ast.KindMethodDeclaration, ast.KindMethodSignature, ast.KindShorthandPropertyAssignment,
ast.KindPropertyAssignment, ast.KindPropertyDeclaration, ast.KindPropertySignature:
return node.AsParameterDeclaration().QuestionToken != nil
}
return false
}
337 changes: 172 additions & 165 deletions internal/checker/checker.go

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (c *Checker) isEmptyArrayAssignment(node *ast.Node) bool {
func (c *Checker) getTypeAtFlowCall(f *FlowState, flow *ast.FlowNode) FlowType {
signature := c.getEffectsSignature(flow.Node)
if signature != nil {
predicate := c.getTypePredicateOfSignature(signature)
predicate := c.GetTypePredicateOfSignature(signature)
if predicate != nil && (predicate.kind == TypePredicateKindAssertsThis || predicate.kind == TypePredicateKindAssertsIdentifier) {
flowType := c.getTypeAtFlowNode(f, flow.Antecedent)
t := c.finalizeEvolvingArrayType(flowType.t)
Expand All @@ -294,7 +294,7 @@ func (c *Checker) getTypeAtFlowCall(f *FlowState, flow *ast.FlowNode) FlowType {
}
return c.newFlowType(narrowedType, flowType.incomplete)
}
if c.getReturnTypeOfSignature(signature).flags&TypeFlagsNever != 0 {
if c.GetReturnTypeOfSignature(signature).flags&TypeFlagsNever != 0 {
return FlowType{t: c.unreachableNeverType}
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ func (c *Checker) narrowTypeByCallExpression(f *FlowState, t *Type, callExpressi
if assumeTrue || !isCallChain(callExpression) {
signature := c.getEffectsSignature(callExpression)
if signature != nil {
predicate = c.getTypePredicateOfSignature(signature)
predicate = c.GetTypePredicateOfSignature(signature)
}
}
if predicate != nil && (predicate.kind == TypePredicateKindThis || predicate.kind == TypePredicateKindIdentifier) {
Expand Down Expand Up @@ -801,7 +801,7 @@ func (c *Checker) narrowTypeByInstanceof(f *FlowState, t *Type, expr *ast.Binary
// participate in `instanceof`, as per Step 2 of https://tc39.es/ecma262/#sec-instanceofoperator.
var predicate *TypePredicate
if signature := c.getEffectsSignature(expr.AsNode()); signature != nil {
predicate = c.getTypePredicateOfSignature(signature)
predicate = c.GetTypePredicateOfSignature(signature)
}
if predicate != nil && predicate.kind == TypePredicateKindIdentifier && predicate.parameterIndex == 0 {
return c.getNarrowedType(t, predicate.t, assumeTrue, true /*checkDerived*/)
Expand Down Expand Up @@ -940,10 +940,10 @@ func (c *Checker) getInstanceType(constructorType *Type) *Type {
if prototypePropertyType != nil && !IsTypeAny(prototypePropertyType) {
return prototypePropertyType
}
constructSignatures := c.getSignaturesOfType(constructorType, SignatureKindConstruct)
constructSignatures := c.GetSignaturesOfType(constructorType, SignatureKindConstruct)
if len(constructSignatures) != 0 {
return c.getUnionType(core.Map(constructSignatures, func(signature *Signature) *Type {
return c.getReturnTypeOfSignature(c.getErasedSignature(signature))
return c.GetReturnTypeOfSignature(c.getErasedSignature(signature))
}))
}
// We use the empty object type to indicate we don't know the type of objects created by
Expand Down Expand Up @@ -2035,7 +2035,7 @@ func (c *Checker) getEffectsSignature(node *ast.Node) *Signature {
if funcType != nil {
apparentType = c.getApparentType(funcType)
}
signatures := c.getSignaturesOfType(core.OrElse(apparentType, c.unknownType), SignatureKindCall)
signatures := c.GetSignaturesOfType(core.OrElse(apparentType, c.unknownType), SignatureKindCall)
switch {
case len(signatures) == 1 && signatures[0].typeParameters == nil:
signature = signatures[0]
Expand All @@ -2062,7 +2062,7 @@ func (c *Checker) getSymbolHasInstanceMethodOfObjectType(t *Type) *Type {
hasInstanceProperty := c.getPropertyOfType(t, hasInstancePropertyName)
if hasInstanceProperty != nil {
hasInstancePropertyType := c.getTypeOfSymbol(hasInstanceProperty)
if hasInstancePropertyType != nil && len(c.getSignaturesOfType(hasInstancePropertyType, SignatureKindCall)) != 0 {
if hasInstancePropertyType != nil && len(c.GetSignaturesOfType(hasInstancePropertyType, SignatureKindCall)) != 0 {
return hasInstancePropertyType
}
}
Expand Down Expand Up @@ -2161,7 +2161,7 @@ func (c *Checker) isDeclarationWithExplicitTypeAnnotation(node *ast.Node) bool {
}

func (c *Checker) hasTypePredicateOrNeverReturnType(sig *Signature) bool {
return c.getTypePredicateOfSignature(sig) != nil || sig.declaration != nil && core.OrElse(c.getReturnTypeFromAnnotation(sig.declaration), c.unknownType).flags&TypeFlagsNever != 0
return c.GetTypePredicateOfSignature(sig) != nil || sig.declaration != nil && core.OrElse(c.getReturnTypeFromAnnotation(sig.declaration), c.unknownType).flags&TypeFlagsNever != 0
}

func (c *Checker) getExplicitThisType(node *ast.Node) *Type {
Expand Down Expand Up @@ -2486,14 +2486,14 @@ func (c *Checker) isReachableFlowNodeWorker(f *FlowState, flow *ast.FlowNode, no
case flags&ast.FlowFlagsCall != 0:
signature := c.getEffectsSignature(flow.Node)
if signature != nil {
predicate := c.getTypePredicateOfSignature(signature)
predicate := c.GetTypePredicateOfSignature(signature)
if predicate != nil && predicate.kind == TypePredicateKindAssertsIdentifier && predicate.t == nil {
predicateArgument := flow.Node.Arguments()[predicate.parameterIndex]
if predicateArgument != nil && c.isFalseExpression(predicateArgument) {
return false
}
}
if c.getReturnTypeOfSignature(signature).flags&TypeFlagsNever != 0 {
if c.GetReturnTypeOfSignature(signature).flags&TypeFlagsNever != 0 {
return false
}
}
Expand Down
38 changes: 19 additions & 19 deletions internal/checker/inference.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,22 +652,22 @@ func (c *Checker) inferFromObjectTypes(n *InferenceState, source *Type, target *
return
}
if c.isArrayOrTupleType(source) {
if isTupleType(target) {
if IsTupleType(target) {
sourceArity := c.getTypeReferenceArity(source)
targetArity := c.getTypeReferenceArity(target)
elementTypes := c.getTypeArguments(target)
elementInfos := target.TargetTupleType().elementInfos
// When source and target are tuple types with the same structure (fixed, variadic, and rest are matched
// to the same kind in each position), simply infer between the element types.
if isTupleType(source) && c.isTupleTypeStructureMatching(source, target) {
if IsTupleType(source) && c.isTupleTypeStructureMatching(source, target) {
for i := range targetArity {
c.inferFromTypes(n, c.getTypeArguments(source)[i], elementTypes[i])
}
return
}
startLength := 0
endLength := 0
if isTupleType(source) {
if IsTupleType(source) {
startLength = min(source.TargetTupleType().fixedLength, target.TargetTupleType().fixedLength)
if target.TargetTupleType().combinedFlags&ElementFlagsVariable != 0 {
endLength = min(getEndElementCount(source.TargetTupleType(), ElementFlagsFixed), getEndElementCount(target.TargetTupleType(), ElementFlagsFixed))
Expand All @@ -677,7 +677,7 @@ func (c *Checker) inferFromObjectTypes(n *InferenceState, source *Type, target *
for i := range startLength {
c.inferFromTypes(n, c.getTypeArguments(source)[i], elementTypes[i])
}
if !isTupleType(source) || sourceArity-startLength-endLength == 1 && source.TargetTupleType().elementInfos[startLength].flags&ElementFlagsRest != 0 {
if !IsTupleType(source) || sourceArity-startLength-endLength == 1 && source.TargetTupleType().elementInfos[startLength].flags&ElementFlagsRest != 0 {
// Single rest element remains in source, infer from that to every element in target
restType := c.getTypeArguments(source)[startLength]
for i := startLength; i < targetArity-endLength; i++ {
Expand All @@ -703,7 +703,7 @@ func (c *Checker) inferFromObjectTypes(n *InferenceState, source *Type, target *
// if T is constrained by a fixed-size tuple we might be able to use its arity to infer T
if info := getInferenceInfoForType(n, elementTypes[startLength]); info != nil {
constraint := c.getBaseConstraintOfType(info.typeParameter)
if constraint != nil && isTupleType(constraint) && constraint.TargetTupleType().combinedFlags&ElementFlagsVariable == 0 {
if constraint != nil && IsTupleType(constraint) && constraint.TargetTupleType().combinedFlags&ElementFlagsVariable == 0 {
impliedArity := constraint.TargetTupleType().fixedLength
c.inferFromTypes(n, c.sliceTupleType(source, startLength, sourceArity-(startLength+impliedArity)), elementTypes[startLength])
c.inferFromTypes(n, c.getElementTypeOfSliceOfTupleType(source, startLength+impliedArity, endLength, false, false), elementTypes[startLength+1])
Expand All @@ -714,7 +714,7 @@ func (c *Checker) inferFromObjectTypes(n *InferenceState, source *Type, target *
// if T is constrained by a fixed-size tuple we might be able to use its arity to infer T
if info := getInferenceInfoForType(n, elementTypes[startLength+1]); info != nil {
constraint := c.getBaseConstraintOfType(info.typeParameter)
if constraint != nil && isTupleType(constraint) && constraint.TargetTupleType().combinedFlags&ElementFlagsVariable == 0 {
if constraint != nil && IsTupleType(constraint) && constraint.TargetTupleType().combinedFlags&ElementFlagsVariable == 0 {
impliedArity := constraint.TargetTupleType().fixedLength
endIndex := sourceArity - getEndElementCount(target.TargetTupleType(), ElementFlagsFixed)
startIndex := endIndex - impliedArity
Expand Down Expand Up @@ -766,12 +766,12 @@ func (c *Checker) inferFromProperties(n *InferenceState, source *Type, target *T
}

func (c *Checker) inferFromSignatures(n *InferenceState, source *Type, target *Type, kind SignatureKind) {
sourceSignatures := c.getSignaturesOfType(source, kind)
sourceSignatures := c.GetSignaturesOfType(source, kind)
sourceLen := len(sourceSignatures)
if sourceLen > 0 {
// We match source and target signatures from the bottom up, and if the source has fewer signatures
// than the target, we infer from the first source signature to the excess target signatures.
targetSignatures := c.getSignaturesOfType(target, kind)
targetSignatures := c.GetSignaturesOfType(target, kind)
targetLen := len(targetSignatures)
for i := range targetLen {
sourceIndex := max(sourceLen-targetLen+i, 0)
Expand Down Expand Up @@ -824,17 +824,17 @@ func (c *Checker) applyToParameterTypes(source *Signature, target *Signature, ca
}

func (c *Checker) applyToReturnTypes(source *Signature, target *Signature, callback func(s *Type, t *Type)) {
targetTypePredicate := c.getTypePredicateOfSignature(target)
targetTypePredicate := c.GetTypePredicateOfSignature(target)
if targetTypePredicate != nil {
sourceTypePredicate := c.getTypePredicateOfSignature(source)
sourceTypePredicate := c.GetTypePredicateOfSignature(source)
if sourceTypePredicate != nil && c.typePredicateKindsMatch(sourceTypePredicate, targetTypePredicate) && sourceTypePredicate.t != nil && targetTypePredicate.t != nil {
callback(sourceTypePredicate.t, targetTypePredicate.t)
return
}
}
targetReturnType := c.getReturnTypeOfSignature(target)
targetReturnType := c.GetReturnTypeOfSignature(target)
if c.couldContainTypeVariables(targetReturnType) {
callback(c.getReturnTypeOfSignature(source), targetReturnType)
callback(c.GetReturnTypeOfSignature(source), targetReturnType)
}
}

Expand Down Expand Up @@ -956,7 +956,7 @@ func (c *Checker) createReverseMappedType(source *Type, target *Type, constraint
}
return c.createArrayTypeEx(elementType, c.isReadonlyArrayType(source))
}
if isTupleType(source) {
if IsTupleType(source) {
elementTypes := core.Map(c.getElementTypes(source), func(t *Type) *Type {
return c.inferReverseMappedType(t, target, constraint)
})
Expand Down Expand Up @@ -990,7 +990,7 @@ func (c *Checker) createReverseMappedType(source *Type, target *Type, constraint
func (c *Checker) isPartiallyInferableType(t *Type) bool {
return t.objectFlags&ObjectFlagsNonInferrableType == 0 || isObjectLiteralType(t) && core.Some(c.getPropertiesOfType(t), func(prop *ast.Symbol) bool {
return c.isPartiallyInferableType(c.getTypeOfSymbol(prop))
}) || isTupleType(t) && core.Some(c.getElementTypes(t), c.isPartiallyInferableType)
}) || IsTupleType(t) && core.Some(c.getElementTypes(t), c.isPartiallyInferableType)
}

func (c *Checker) inferReverseMappedType(source *Type, target *Type, constraint *Type) *Type {
Expand Down Expand Up @@ -1115,7 +1115,7 @@ func (c *Checker) replaceIndexedAccess(instantiable *Type, t *Type, replacement
func (c *Checker) typesDefinitelyUnrelated(source *Type, target *Type) bool {
// Two tuple types with incompatible arities are definitely unrelated.
// Two object types that each have a property that is unmatched in the other are definitely unrelated.
if isTupleType(source) && isTupleType(target) {
if IsTupleType(source) && IsTupleType(target) {
return tupleTypesDefinitelyUnrelated(source, target)
}
return c.getUnmatchedProperty(source, target, false /*requireOptionalProperties*/, true /*matchDiscriminantProperties*/) != nil &&
Expand Down Expand Up @@ -1235,7 +1235,7 @@ func (c *Checker) inferFromIntraExpressionSites(n *InferenceContext) {
if ast.IsMethodDeclaration(site.node) {
contextualType = c.getContextualTypeForObjectLiteralMethod(site.node, ContextFlagsNoConstraints)
} else {
contextualType = c.getContextualType(site.node, ContextFlagsNoConstraints)
contextualType = c.GetContextualType(site.node, ContextFlagsNoConstraints)
}
if contextualType != nil {
c.inferTypes(n.inferences, site.t, contextualType, InferencePriorityNone, false)
Expand Down Expand Up @@ -1415,11 +1415,11 @@ func (c *Checker) isTypeParameterAtTopLevel(t *Type, tp *Type, depth int) bool {
}

func (c *Checker) isTypeParameterAtTopLevelInReturnType(signature *Signature, typeParameter *Type) bool {
typePredicate := c.getTypePredicateOfSignature(signature)
typePredicate := c.GetTypePredicateOfSignature(signature)
if typePredicate != nil {
return typePredicate.t != nil && c.isTypeParameterAtTopLevel(typePredicate.t, typeParameter, 0)
}
return c.isTypeParameterAtTopLevel(c.getReturnTypeOfSignature(signature), typeParameter, 0)
return c.isTypeParameterAtTopLevel(c.GetReturnTypeOfSignature(signature), typeParameter, 0)
}

func (c *Checker) getTypeFromInference(inference *InferenceInfo) *Type {
Expand Down Expand Up @@ -1523,7 +1523,7 @@ func (c *Checker) isSkipDirectInferenceNode(node *ast.Node) bool {

// Returns `true` if `type` has the shape `[T[0]]` where `T` is `typeParameter`
func (c *Checker) isTupleOfSelf(tp *Type, t *Type) bool {
return isTupleType(t) && c.getTupleElementType(t, 0) == c.getIndexedAccessType(tp, c.getNumberLiteralType(0)) && c.getTypeOfPropertyOfType(t, "1") == nil
return IsTupleType(t) && c.getTupleElementType(t, 0) == c.getIndexedAccessType(tp, c.getNumberLiteralType(0)) && c.getTypeOfPropertyOfType(t, "1") == nil
}

func newInferenceInfo(typeParameter *Type) *InferenceInfo {
Expand Down
Loading