checker

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

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

Checker performs static type checking on the AST.

func NewChecker

func NewChecker() *Checker

NewChecker creates a new type checker with standard built-in types.

func NewCheckerWithInitializers

func NewCheckerWithInitializers(initializers []builtins.BuiltinInitializer) *Checker

NewCheckerWithInitializers creates a new type checker with custom built-in initializers.

func (*Checker) Check

func (c *Checker) Check(program *parser.Program) []errors.PaseratiError

Check analyzes the given program AST for type errors.

func (*Checker) EnableModuleMode

func (c *Checker) EnableModuleMode(modulePath string, moduleLoader modules.ModuleLoader)

EnableModuleMode sets up the checker for module-aware type checking

func (*Checker) GetEnvironment

func (c *Checker) GetEnvironment() *Environment

GetEnvironment returns the current type environment

func (*Checker) GetImportBindings

func (c *Checker) GetImportBindings() map[string]*ImportBinding

GetImportBindings returns all import bindings from the module environment This is used by the compiler to synchronize import information

func (*Checker) GetModuleExports

func (c *Checker) GetModuleExports() map[string]types.Type

GetModuleExports returns the exports from the current module (if in module mode)

func (*Checker) GetProgram

func (c *Checker) GetProgram() *parser.Program

GetProgram returns the program AST being checked

func (*Checker) IsModuleMode

func (c *Checker) IsModuleMode() bool

IsModuleMode returns true if the checker is in module-aware mode

func (*Checker) SetAllowSuperInEval added in v0.9.3

func (c *Checker) SetAllowSuperInEval(allow bool)

SetAllowSuperInEval sets whether super expressions are allowed in eval contexts This is used when compiling direct eval code that was called from a method context

type ContextualType

type ContextualType struct {
	ExpectedType types.Type // The type expected in this context
	IsContextual bool       // Whether this is a contextual hint vs. required type
}

ContextualType represents type information that flows from context to sub-expressions

type Environment

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

Environment manages type information within scopes.

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

NewEnclosedEnvironment creates a new environment nested within an outer one.

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment creates a new top-level type environment.

func NewFunctionEnvironment

func NewFunctionEnvironment(outer *Environment) *Environment

NewFunctionEnvironment creates a new function-scoped environment

func NewGlobalEnvironment

func NewGlobalEnvironment(initializers []builtins.BuiltinInitializer) *Environment

NewGlobalEnvironment creates a new top-level global environment. It populates the environment with built-in types using the new initializer system.

func NewStandardGlobalEnvironment

func NewStandardGlobalEnvironment() *Environment

NewStandardGlobalEnvironment creates a new global environment with standard built-in types. This is a convenience function that uses the default set of initializers.

func (*Environment) AddOverloadSignature

func (e *Environment) AddOverloadSignature(name string, sig *parser.FunctionSignature)

AddOverloadSignature adds a function signature to the pending overloads for the given function name.

func (*Environment) ClearTypeParameters

func (e *Environment) ClearTypeParameters()

ClearTypeParameters removes all type parameters from the current scope. This is useful when exiting a generic function.

func (*Environment) CompleteOverloadedFunction

func (e *Environment) CompleteOverloadedFunction(name string, overloadSignatures []*types.Signature) bool

CompleteOverloadedFunction creates a unified ObjectType from pending signatures, then stores it and clears the pending overloads.

func (*Environment) CompleteOverloadedFunctionUTS

func (e *Environment) CompleteOverloadedFunctionUTS(name string, overloadSignatures []*types.Signature, implementation *types.Signature) bool

CompleteOverloadedFunctionUTS creates an ObjectType with multiple call signatures from pending signatures and implementation, then stores it and clears the pending overloads. This is the UTS replacement for CompleteOverloadedFunction.

func (*Environment) Define

func (e *Environment) Define(name string, typ types.Type, isConst bool) bool

Define adds a new *variable* type binding and its const status to the current environment scope. Returns false if the name conflicts with an existing variable/const in this scope. Note: TypeScript-style declaration merging allows the same name to exist as both a value and a type.

func (*Environment) DefineTypeAlias

func (e *Environment) DefineTypeAlias(name string, typ types.Type) bool

DefineTypeAlias adds a new *type alias* binding to the current environment scope. Returns false if the alias name conflicts with an existing type alias in this scope, unless the existing alias is a forward reference (which can be overwritten). Note: TypeScript-style declaration merging allows the same name to exist as both a value and a type.

func (*Environment) DefineTypeParameter

func (e *Environment) DefineTypeParameter(name string, param *types.TypeParameter) bool

DefineTypeParameter defines a type parameter in the current scope. Returns true if successful, false if the parameter name already exists.

func (*Environment) GetAllTypeAliases

func (e *Environment) GetAllTypeAliases() map[string]types.Type

GetAllTypeAliases returns all type aliases in the current environment (not including outer scopes)

func (*Environment) GetAllVariables

func (e *Environment) GetAllVariables() map[string]SymbolInfo

GetAllVariables returns all variables in the current environment (not including outer scopes)

func (*Environment) GetCurrentScopeTypeParameters

func (e *Environment) GetCurrentScopeTypeParameters() map[string]*types.TypeParameter

GetCurrentScopeTypeParameters returns all type parameters defined in the current scope. This is useful for creating generic function types.

func (*Environment) GetFunctionScope

func (e *Environment) GetFunctionScope() *Environment

GetFunctionScope returns the nearest function scope (or global scope) Used for var hoisting - var declarations should be added to function scope, not block scope

func (*Environment) GetPendingOverloads

func (e *Environment) GetPendingOverloads(name string) []*parser.FunctionSignature

GetPendingOverloads returns the pending overload signatures for the given function name.

func (*Environment) GetPrimitivePrototypeMethodType

func (e *Environment) GetPrimitivePrototypeMethodType(primitiveName, methodName string) types.Type

GetPrimitivePrototypeMethodType returns the type of a method on a primitive prototype This replaces the old builtins.GetPrototypeMethodType function

func (*Environment) IsOverloadedFunction

func (e *Environment) IsOverloadedFunction(name string) bool

IsOverloadedFunction checks if a function name has overloads (either pending or completed).

func (*Environment) IsTypeParameterInScope

func (e *Environment) IsTypeParameterInScope(name string) bool

IsTypeParameterInScope checks if a type parameter name is currently in scope.

func (*Environment) PopWithObject

func (e *Environment) PopWithObject()

PopWithObject removes the most recent with object from the stack

func (*Environment) PushWithObject

func (e *Environment) PushWithObject(withObj WithObject)

PushWithObject adds a new with object to the current environment's stack

func (*Environment) Resolve

func (e *Environment) Resolve(name string) (typ types.Type, isConst bool, found bool)

Resolve looks up a *variable* name in the current environment and its outer scopes. Returns the type, whether it's constant, and true if found. Otherwise returns nil, false, false.

func (*Environment) ResolveOverloadedFunction

func (e *Environment) ResolveOverloadedFunction(name string) (*types.ObjectType, bool)

ResolveOverloadedFunction looks up an overloaded function by name in this environment and outer scopes.

func (*Environment) ResolveType

func (e *Environment) ResolveType(name string) (types.Type, bool)

ResolveType looks up a *type name* (could be alias or primitive) in the current environment and its outer scopes. Returns the resolved type and true if found, otherwise nil and false.

func (*Environment) ResolveTypeParameter

func (e *Environment) ResolveTypeParameter(name string) (*types.TypeParameter, bool)

ResolveTypeParameter looks up a type parameter by name. It searches the current scope and outer scopes. Returns the TypeParameter and true if found, nil and false otherwise.

func (*Environment) ResolveWithFallback

func (e *Environment) ResolveWithFallback(name string) (types.Type, bool, bool)

ResolveWithFallback tries to resolve an identifier, checking with objects if not found as a variable Returns: (type, isFromWith, found)

func (*Environment) Update

func (e *Environment) Update(name string, typ types.Type) bool

Update modifies the type of an *existing* variable symbol in the current environment scope. It does NOT change the IsConst status. Returns true if the symbol was found and updated, false otherwise.

type ExportBinding

type ExportBinding struct {
	LocalName    string           // Name used locally in this module
	ExportName   string           // Name when exported (may differ due to aliases)
	ExportedType types.Type       // Type being exported
	Declaration  parser.Statement // Original declaration (if any)
	IsReExport   bool             // True if this is a re-export from another module
	SourceModule string           // For re-exports, the source module path
	IsTypeOnly   bool             // True if this is a type-only export
}

ExportBinding represents an exported name's binding information

type FunctionCheckContext

type FunctionCheckContext struct {
	FunctionName             string                  // For logging and recursion
	TypeParameters           []*parser.TypeParameter // Generic type parameters (if any)
	Parameters               []*parser.Parameter     // Parameter nodes
	RestParameter            *parser.RestParameter   // Rest parameter node (if any)
	ReturnTypeAnnotation     parser.Expression       // Return type annotation (if any)
	Body                     parser.Node             // Function body (block or expression)
	IsArrow                  bool                    // Whether this is an arrow function
	IsGenerator              bool                    // Whether this is a generator function (function*)
	IsAsync                  bool                    // Whether this is an async function
	AllowSelfReference       bool                    // Whether to allow recursive self-reference
	AllowOverloadCompletion  bool                    // Whether to check for overload completion
	ContextualParameterTypes []types.Type            // Contextual parameter types from expected signature
	ContextualReturnType     types.Type              // Contextual return type (nil for generic inference)
}

FunctionCheckContext holds the common context for function checking

type ImportBinding

type ImportBinding struct {
	LocalName    string            // Name used locally in this module
	SourceModule string            // Path of the source module
	SourceName   string            // Name in the source module ("default" for default imports)
	ImportType   ImportBindingType // Type of import binding
	ResolvedType types.Type        // Resolved type from source module
}

ImportBinding represents an imported name's binding information

type ImportBindingType

type ImportBindingType int

ImportBindingType represents different kinds of import bindings

const (
	ImportDefault   ImportBindingType = iota // import defaultName from "module"
	ImportNamed                              // import { name } from "module"
	ImportNamespace                          // import * as name from "module"
)

type IndexSignatureError

type IndexSignatureError struct {
	PropertyName string
	PropertyType types.Type
	ExpectedType types.Type
	KeyType      types.Type
}

IndexSignatureError represents an error when a property doesn't match index signature constraints

type ModuleEnvironment

type ModuleEnvironment struct {
	*Environment // Embed base environment

	// Module-specific information
	ModulePath   string               // Current module's resolved path
	ModuleLoader modules.ModuleLoader // Reference to module loader

	// Import/Export tracking
	ImportedNames map[string]*ImportBinding // local_name -> import info
	ExportedNames map[string]*ExportBinding // export_name -> export info
	DefaultExport *ExportBinding            // Default export info

	// Module dependencies (for circular dependency detection)
	Dependencies map[string]bool // Set of module paths this module depends on
}

ModuleEnvironment extends Environment with module-aware type resolution

func NewModuleEnvironment

func NewModuleEnvironment(parent *Environment, modulePath string, loader modules.ModuleLoader) *ModuleEnvironment

NewModuleEnvironment creates a new module-aware environment

func (*ModuleEnvironment) DefineExport

func (me *ModuleEnvironment) DefineExport(localName, exportName string, exportedType types.Type, decl parser.Statement)

DefineExport adds an export binding to the module environment

func (*ModuleEnvironment) DefineImport

func (me *ModuleEnvironment) DefineImport(localName, sourceModule, sourceName string, importType ImportBindingType)

DefineImport adds an import binding to the module environment

func (*ModuleEnvironment) DefineReExport

func (me *ModuleEnvironment) DefineReExport(exportName, sourceModule, sourceName string, isTypeOnly bool)

DefineReExport adds a re-export binding (export { name } from "module")

func (*ModuleEnvironment) GetAllExports

func (me *ModuleEnvironment) GetAllExports() map[string]types.Type

GetAllExports returns all exported names and their types

func (*ModuleEnvironment) GetExportedType

func (me *ModuleEnvironment) GetExportedType(exportName string) (types.Type, bool)

GetExportedType gets the type of an exported name

func (*ModuleEnvironment) HasCircularDependency

func (me *ModuleEnvironment) HasCircularDependency(targetModule string) bool

HasCircularDependency checks if adding a dependency would create a cycle

func (*ModuleEnvironment) ResolveImportedType

func (me *ModuleEnvironment) ResolveImportedType(localName string) types.Type

ResolveImportedType resolves the actual type of an imported name

func (*ModuleEnvironment) UpdateExportType

func (me *ModuleEnvironment) UpdateExportType(exportName string, newType types.Type)

UpdateExportType updates the type of an exported binding (used when type is refined)

type SymbolInfo

type SymbolInfo struct {
	Type    types.Type
	IsConst bool
}

--- NEW: Symbol Information ---

type TypeGuard

type TypeGuard struct {
	VariableName      string     // The variable being narrowed (e.g., "x" or "this.value" or "obj.prop")
	NarrowedType      types.Type // The type it's narrowed to (e.g., types.String)
	IsNegated         bool       // true for !== checks, false for === checks
	DiscriminantProp  string     // For discriminated unions: the property being checked (e.g., "kind")
	DiscriminantValue types.Type // For discriminated unions: the value being compared (e.g., literal "num")
}

TypeGuard represents a detected type guard pattern

type TypeParameterConstraint

type TypeParameterConstraint struct {
	TypeParameter *types.TypeParameter
	InferredType  types.Type
	Confidence    int // Higher = more confident
}

TypeParameterConstraint represents a constraint on a type parameter

type WithObject

type WithObject struct {
	ExprType   types.Type            // Type of the with expression
	Properties map[string]types.Type // Known properties and their types
}

WithObject represents an object in a with statement

Jump to

Keyboard shortcuts

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