tsresolve

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package tsresolve implements the TypeScript/JavaScript in-process type resolver. It implements the typresolve.Resolver interface, resolving TypeScript and JavaScript call expressions, property access, JSX elements, and type references using tree-sitter AST walking and a shared type registry built from extracted definitions.

Supported dialects: .ts, .tsx, .js, .jsx

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildImportMap

func BuildImportMap(root *sitter.Node, content []byte) map[string]ImportInfo

BuildImportMap builds per-file import map from the TypeScript AST root. Returns a map from local binding name to ImportInfo. Handles ES6 imports (named, default, namespace) and CommonJS require().

func BuildRegistry

func BuildRegistry(defs []typresolve.ResolverDef) *typresolve.Registry

BuildRegistry builds a typresolve.Registry from ResolverDef entries. Registers functions, methods (with receiver type extracted from QN), classes, interfaces, and type aliases. TS methods have QN format: "repoURL://module/path.ClassName.MethodName".

func BuiltinWrapperClass

func BuiltinWrapperClass(builtinName string) string

BuiltinWrapperClass maps builtin primitive type names to their wrapper class for method dispatch. For example, "string" maps to "String" so that "hello".toUpperCase() can find methods on the String prototype. Returns empty string if the builtin has no wrapper class.

func EvalExprType

func EvalExprType(ctx *ResolveContext, node *sitter.Node) *typresolve.Type

EvalExprType evaluates the type of a TypeScript expression AST node using scope lookup, registry lookup, import resolution, member dispatch, and async unwrapping. This is the TS port of ts_eval_expr_type from the C reference implementation.

func IsBuiltinType

func IsBuiltinType(name string) bool

IsBuiltinType returns true if name is a TypeScript/JavaScript builtin type.

func IsPassthroughUtility

func IsPassthroughUtility(name string) bool

IsPassthroughUtility returns true for TypeScript utility types that pass through to their inner type for method dispatch: Partial, Required, Readonly, NonNullable, Pick, Omit, Awaited.

func LiteralType

func LiteralType(nodeType string) *typresolve.Type

LiteralType maps tree-sitter literal node types to their corresponding builtin types. Returns nil if the node type is not a recognized literal.

func LookupField

func LookupField(reg *typresolve.Registry, typeQN string, fieldName string) *typresolve.Type

LookupField looks up a field/property on a named type, following the prototype chain (EmbeddedTypes) up to maxLookupDepth levels deep. Returns the field's type if found, nil otherwise.

func LookupMember

func LookupMember(reg *typresolve.Registry, typeQN string, memberName string) *typresolve.RegisteredFunc

LookupMember looks up a method on a named type, following the prototype chain (stored as EmbeddedTypes) up to maxLookupDepth levels deep. For builtin primitive types (string, number), delegates to the wrapper class (String, Number). Returns the registered function if found, nil otherwise.

func LookupMemberType

func LookupMemberType(reg *typresolve.Registry, typeQN string, memberName string) *typresolve.Type

LookupMemberType combines field and method lookup. First checks fields (returns the field type directly), then checks methods (returns the method's signature type). This is the Go equivalent of lookup_member_type in the C reference.

Fix #7: Resolves type aliases before member lookup. Fix #8: For intersection types (stored as Named with TypeParams containing multiple members), dispatches lookup across all intersection members.

func LookupMemberTypeOnType

func LookupMemberTypeOnType(reg *typresolve.Registry, t *typresolve.Type, memberName string) *typresolve.Type

LookupMemberTypeOnType performs member lookup on an arbitrary Type value, handling intersection types (Struct merging), struct fields, Named dispatch, and builtin wrapper classes. Used by evalMemberExpression for compound types.

func ParseTypeNode

func ParseTypeNode(node *sitter.Node, content []byte, moduleQN string, imports map[string]ImportInfo) *typresolve.Type

ParseTypeNode parses a tree-sitter TypeScript type AST node into a typresolve.Type. Handles type_annotation, type_identifier, generic_type, union_type, intersection_type, array_type, tuple_type, parenthesized_type, function_type, and predefined_type nodes. Falls back to ParseTypeText for complex cases.

func ParseTypeText

func ParseTypeText(text string, moduleQN string) *typresolve.Type

ParseTypeText parses a TypeScript type annotation text string into a typresolve.Type. This is the TS equivalent of Go's ParseTypeNode and Python's ParseAnnotation. Handles builtin primitives, generic instantiation, array shorthand, union, intersection, tuple, function types, and qualified names.

func ProcessStatement

func ProcessStatement(ctx *ResolveContext, node *sitter.Node)

ProcessStatement processes a TypeScript statement node to bind variables into the current scope. Handles variable_declarator (const/let/var with type annotations or initializer inference), for_in_statement, for_of_statement, destructuring patterns (object and array), catch_clause bindings, lexical_declaration, variable_declaration, and assignment_expression.

This is the TS port of ts_process_statement from the C reference implementation (lines 1694-1927).

func RegisterStdlib

func RegisterStdlib(reg *typresolve.Registry)

RegisterStdlib populates a Registry with TypeScript/JavaScript standard library types and their methods. Mirrors cbm_ts_stdlib_register from the C reference implementation: Array (with typed callbacks), Promise, Map, Set, String, Number, Boolean, Date, RegExp, Error, Math, Object, JSON, console, and DOM essentials (EventTarget, Node, Element, HTMLElement, Document, Event, Response).

func ResolveBuiltinType

func ResolveBuiltinType(name string) *typresolve.Type

ResolveBuiltinType returns a typresolve.Builtin type for primitive types (string, number, boolean, etc.). Returns nil for non-primitive builtins like Array or Promise, which are Named types with methods.

func ResolveCallsInFile

func ResolveCallsInFile(ctx *ResolveContext, root *sitter.Node, fileHash types.Hash, repoURL string, filePath string) []types.Edge

ResolveCallsInFile walks a TypeScript file's AST resolving call expressions, new expressions, and JSX elements, emitting edges. Uses the shared registry, per-file scope chain, and import map.

func ResolveModulePath

func ResolveModulePath(importSource string, currentFile string) string

ResolveModulePath resolves a TypeScript import source path to a qualified name prefix. Only resolves relative imports (starting with "." or ".."). Returns "" for bare module specifiers (non-relative).

func UnwrapPromise

func UnwrapPromise(t *typresolve.Type) *typresolve.Type

UnwrapPromise extracts the inner type T from a Promise<T> type. Promise<T> is represented as Named("Promise") with Elem = T. If the type is Named("Promise") without an Elem, returns Unknown. For non-Promise types, returns the input unchanged.

Types

type ImportInfo

type ImportInfo struct {
	ModulePath   string // source module path (e.g. "./utils", "express")
	OriginalName string // original name before aliasing
	IsNamespace  bool   // true for "import * as X from ..."
	IsDefault    bool   // true for "import X from ..."
}

ImportInfo describes a single TypeScript import binding. Captures the source module path and the style of import (named, default, namespace, or require).

func ResolveImport

func ResolveImport(imports map[string]ImportInfo, name string) (ImportInfo, bool)

ResolveImport resolves an import local name to its ImportInfo.

type ResolveContext

type ResolveContext struct {
	Registry         *typresolve.Registry
	Scope            *typresolve.Scope
	Imports          map[string]ImportInfo // from imports.go (Agent A)
	ModuleQN         string                // current module qualified name
	Content          []byte                // source file content
	EnclosingFuncQN  string                // QN of enclosing function
	EnclosingClassQN string                // QN of enclosing class (for this/super)
}

ResolveContext holds per-file state for TypeScript type resolution.

type TypeScriptResolver

type TypeScriptResolver struct {
	// contains filtered or unexported fields
}

TypeScriptResolver implements typresolve.Resolver for TypeScript/JavaScript.

func NewTypeScriptResolver

func NewTypeScriptResolver() *TypeScriptResolver

NewTypeScriptResolver creates a new TypeScript resolver.

func (*TypeScriptResolver) InitWorkspace

func (r *TypeScriptResolver) InitWorkspace(ctx context.Context, defs []typresolve.ResolverDef) error

InitWorkspace builds a Registry from TS definitions and registers the TypeScript standard library (Array, Promise, Map, Set, String, Number, etc.).

func (*TypeScriptResolver) Language

func (r *TypeScriptResolver) Language() string

Language returns "typescript".

func (*TypeScriptResolver) ResolveFile

ResolveFile walks the TypeScript AST and resolves calls and references.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL