autoimport

package
v0.0.0-...-f77836c Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: Apache-2.0 Imports: 41 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TypeToAutoImportableTypeNode

func TypeToAutoImportableTypeNode(
	c *checker.Checker,
	importAdder ImportAdder,
	t *checker.Type,
	contextNode *ast.Node,
) *ast.TypeNode

Types

type BucketState

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

BucketState represents the dirty state of a bucket. In general, a bucket can be used for an auto-imports request if it is clean or if the only edited file is the one that was requested for auto-imports. Most edits within a file will not change the imports available to that file. However, one exception causes the bucket to be rebuilt after a change to a single file: local files are newly added to the project by a manual import. This can only happen after a full (non-clone) program update. When this happens, the `newProgramStructure` flag is set until the next time the bucket is rebuilt, when this condition will be checked.

func (BucketState) Dirty

func (b BucketState) Dirty() bool

func (BucketState) DirtyFile

func (b BucketState) DirtyFile() tspath.Path

func (BucketState) DirtyPackages

func (b BucketState) DirtyPackages() *collections.Set[string]

type BucketStats

type BucketStats struct {
	Path            tspath.Path
	ExportCount     int
	FileCount       int
	State           BucketState
	DependencyNames *collections.Set[string]
	PackageNames    *collections.Set[string]
}

type CacheStats

type CacheStats struct {
	ProjectBuckets     []BucketStats
	NodeModulesBuckets []BucketStats
}

type Export

type Export struct {
	ExportID
	ModuleFileName string
	Syntax         ExportSyntax
	Flags          ast.SymbolFlags

	Target                     ExportID
	IsTypeOnly                 bool
	ScriptElementKind          lsutil.ScriptElementKind
	ScriptElementKindModifiers collections.Set[lsutil.ScriptElementKindModifier]

	// The file where the export was found.
	Path tspath.Path

	NodeModulesDirectory tspath.Path
	PackageName          string
	// contains filtered or unexported fields
}

func SymbolToExport

func SymbolToExport(symbol *ast.Symbol, ch *checker.Checker) *Export

func (*Export) AmbientModuleName

func (e *Export) AmbientModuleName() string

func (*Export) IsRenameable

func (e *Export) IsRenameable() bool

func (*Export) IsUnresolvedAlias

func (e *Export) IsUnresolvedAlias() bool

func (*Export) Name

func (e *Export) Name() string

type ExportID

type ExportID struct {
	ModuleID   ModuleID
	ExportName string
}

type ExportSyntax

type ExportSyntax int
const (
	ExportSyntaxNone ExportSyntax = iota
	// export const x = {}
	ExportSyntaxModifier
	// export { x }
	ExportSyntaxNamed
	// export default function f() {}
	ExportSyntaxDefaultModifier
	// export default f
	ExportSyntaxDefaultDeclaration
	// export = x
	ExportSyntaxEquals
	// export as namespace x
	ExportSyntaxUMD
	// export * from "module"
	ExportSyntaxStar
	// module.exports = {}
	ExportSyntaxCommonJSModuleExports
	// exports.x = {}
	ExportSyntaxCommonJSExportsProperty
)

func (ExportSyntax) String

func (i ExportSyntax) String() string

type Fix

type Fix struct {
	*lsproto.AutoImportFix

	ModuleSpecifierKind      modulespecifiers.ResultKind
	IsReExport               bool
	ModuleFileName           string
	TypeOnlyAliasDeclaration *ast.Declaration
}

func (*Fix) Edits

func (f *Fix) Edits(
	ctx context.Context,
	file *ast.SourceFile,
	compilerOptions *core.CompilerOptions,
	formatOptions *lsutil.FormatCodeSettings,
	converters *lsconv.Converters,
	preferences *lsutil.UserPreferences,
) ([]*lsproto.TextEdit, string)

type FixAndExport

type FixAndExport struct {
	Fix    *Fix
	Export *Export
}

type ImportAdder

type ImportAdder interface {
	HasFixes() bool
	AddImportFromExportedSymbol(symbol *ast.Symbol, isValidTypeOnlyUseSite bool)
	Edits() []*lsproto.TextEdit
}

func NewImportAdder

func NewImportAdder(
	ctx context.Context,
	program *compiler.Program,
	checker *checker.Checker,
	file *ast.SourceFile,
	view *View,
	formatOptions *lsutil.FormatCodeSettings,
	converters *lsconv.Converters,
	preferences *lsutil.UserPreferences,
) ImportAdder

type Index

type Index[T Named] struct {
	// contains filtered or unexported fields
}

Index stores entries with an index mapping uppercase letters to entries whose name starts with that letter, and lowercase letters to entries whose name contains a word starting with that letter.

func (*Index[T]) Clone

func (idx *Index[T]) Clone(filter func(T) bool) *Index[T]

Clone creates a new Index containing only entries for which filter returns true.

func (*Index[T]) Find

func (idx *Index[T]) Find(name string, caseSensitive bool) []T

func (*Index[T]) SearchWordPrefix

func (idx *Index[T]) SearchWordPrefix(prefix string) []T

SearchWordPrefix returns each entry whose name contains a word beginning with the first character of 'prefix', and whose name contains all characters of 'prefix' in order (case-insensitive). If 'filter' is provided, only entries for which filter(entry) returns true are included.

type ModuleID

type ModuleID string

ModuleID uniquely identifies a module across multiple declarations. If the export is from an ambient module declaration, this is the module name. If the export is from a module augmentation, this is the Path() of the resolved module file. Otherwise this is the Path() of the exporting source file.

type Named

type Named interface {
	Name() string
}

Named is a constraint for types that can provide their name.

type QueryKind

type QueryKind int
const (
	QueryKindWordPrefix QueryKind = iota
	QueryKindExactMatch
	QueryKindCaseInsensitiveMatch
)

type Registry

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

func NewRegistry

func NewRegistry(toPath func(fileName string) tspath.Path, preferences *lsutil.UserPreferences) *Registry

func (*Registry) Clone

func (r *Registry) Clone(ctx context.Context, change RegistryChange, host RegistryCloneHost, logger *logging.LogTree) (*Registry, error)

func (*Registry) GetCacheStats

func (r *Registry) GetCacheStats() *CacheStats

func (*Registry) IsPreparedForImportingFile

func (r *Registry) IsPreparedForImportingFile(fileName string, projectPath tspath.Path, preferences *lsutil.UserPreferences) bool

func (*Registry) NodeModulesDirectories

func (r *Registry) NodeModulesDirectories() map[tspath.Path]string

type RegistryBucket

type RegistryBucket struct {

	// Paths maps file paths to package names. For project buckets, the package name
	// is always empty string. For node_modules buckets, this enables reverse lookup
	// from path to package for granular updates. Only paths eligible for granular
	// update (project reference packages) have entries here.
	Paths map[tspath.Path]string
	// PackageFiles maps package names to their file paths and file names.
	// All package directory names in node_modules are keys; indexed packages have
	// non-nil maps with path→fileName entries, unindexed packages have nil maps.
	// This enables efficient removal of a package's files during granular updates
	// without iterating through all entries. Only defined for node_modules buckets.
	PackageFiles map[string]map[tspath.Path]string
	// ResolvedPackageNames is only defined for project buckets. It is the set of
	// package names that were resolved from imports in the project's program files.
	// This is passed to node_modules buckets so they include packages that are
	// directly imported even if not listed in package.json dependencies.
	ResolvedPackageNames *collections.Set[string]
	// DependencyNames is only defined for node_modules buckets. It is the set of
	// package names that will be included in the bucket if present in the directory,
	// computed from package.json dependencies plus resolved package names from
	// active programs. If nil, all packages are included because at least one open
	// file has access to this node_modules directory without being filtered by a
	// package.json.
	DependencyNames *collections.Set[string]
	// AmbientModuleNames is only defined for node_modules buckets. It is the set of
	// ambient module names found while extracting exports in the bucket.
	AmbientModuleNames map[string][]string
	// Entrypoints is only defined for node_modules buckets. Keys are package entrypoint
	// file paths, and values describe the ways of importing the package that would resolve
	// to that file.
	Entrypoints map[tspath.Path][]*module.ResolvedEntrypoint
	Index       *Index[*Export]
	// contains filtered or unexported fields
}

func (*RegistryBucket) Clone

func (b *RegistryBucket) Clone() *RegistryBucket

type RegistryChange

type RegistryChange struct {
	RequestedFile tspath.Path
	OpenFiles     map[tspath.Path]string
	Changed       collections.Set[lsproto.DocumentUri]
	Created       collections.Set[lsproto.DocumentUri]
	Deleted       collections.Set[lsproto.DocumentUri]
	// RebuiltPrograms maps from project path to:
	//   - true: the program was rebuilt with a different set of file names
	//   - false: the program was rebuilt but the set of file names is unchanged
	RebuiltPrograms map[tspath.Path]bool
	UserPreferences *lsutil.UserPreferences
}

type RegistryCloneHost

type RegistryCloneHost interface {
	module.ResolutionHost
	FS() vfs.FS
	GetDefaultProject(path tspath.Path) (tspath.Path, *compiler.Program)
	GetProgramForProject(projectPath tspath.Path) *compiler.Program
	GetPackageJson(fileName string) *packagejson.InfoCacheEntry
	GetSourceFile(fileName string, path tspath.Path) *ast.SourceFile
	Dispose()
}

type View

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

func NewView

func NewView(registry *Registry, importingFile *ast.SourceFile, projectKey tspath.Path, program *compiler.Program, preferences modulespecifiers.UserPreferences) *View

func (*View) CompareFixesForRanking

func (v *View) CompareFixesForRanking(a, b *Fix) int

CompareFixesForRanking returns negative if `a` is better than `b`. Sorting with this comparator will place the best fix first. Fixes of equal desirability will be considered equal.

func (*View) CompareFixesForSorting

func (v *View) CompareFixesForSorting(a, b *Fix) int

CompareFixesForSorting returns negative if `a` is better than `b`. Sorting with this comparator will place the best fix first. After rank sorting, fixes will be sorted by arbitrary but stable criteria to ensure a deterministic order.

func (*View) GetCompletions

func (v *View) GetCompletions(ctx context.Context, prefix string, position lsproto.Position, forJSX bool, isTypeOnlyLocation bool) []*FixAndExport

func (*View) GetFixes

func (v *View) GetFixes(ctx context.Context, export *Export, forJSX bool, isValidTypeOnlyUseSite bool, usagePosition *lsproto.Position) []*Fix

func (*View) GetModuleSpecifier

func (v *View) GetModuleSpecifier(
	export *Export,
	userPreferences modulespecifiers.UserPreferences,
) (string, modulespecifiers.ResultKind)

func (*View) Search

func (v *View) Search(query string, kind QueryKind) []*Export

func (*View) SearchByExportID

func (v *View) SearchByExportID(id ExportID) []*Export

Jump to

Keyboard shortcuts

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