ts

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Var represents the symbolic constant for identifying variable declarations in
	// TypeScript code within the context of the Finder's operations.
	Var = Symbol("var")

	// Class represents a TypeScript class symbol for use in code analysis and
	// manipulation tasks within the package. It identifies class declarations when
	// performing operations such as finding symbols or determining their positions
	// in source code.
	Class = Symbol("class")

	// Interface represents the TypeScript interface symbol that can be used to
	// filter TypeScript entities during code analysis.
	Interface = Symbol("iface")

	// Func represents the identifier for a TypeScript function symbol.
	Func = Symbol("func")

	// Method represents a TypeScript method symbol.
	Method = Symbol("method")

	// Property represents a TypeScript object property symbol used for identifying
	// such properties within source code during static analysis.
	Property = Symbol("prop")
)

Variables

View Source
var (
	FileExtensions = []string{".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"}
)

FileExtensions holds a list of recognized file extensions for TypeScript and JavaScript source files. It includes extensions for both standard and module-specific file types.

Functions

func InsertComment

func InsertComment(comment string, code []byte, pos Position) ([]byte, error)

InsertComment inserts a given comment into a slice of bytes representing code at the specified position. It returns the modified code with the comment inserted and an error if the position is out of range. The comment is inserted in a way that aligns with the indentation of the line at the given position. If successful, it returns the updated code as a []byte and nil error; otherwise, it returns nil and an error describing the invalid position.

func NormalizeGeneratedComment added in v0.0.10

func NormalizeGeneratedComment(doc string) string

NormalizeGeneratedComment ensures the consistency and readability of a generated comment by trimming excess whitespace, removing leading asterisks commonly used in block comments, and stripping any trailing comment terminators. It also filters out lines beginning with an "@" symbol, which are often used for annotations in documentation comments. The result is a clean, normalized string ready for further processing or insertion into code.

func Prompt

func Prompt(input generate.PromptInput) string

Prompt constructs a TSDoc comment template based on the provided input which includes the identifier and source code context. It determines the type of the identifier, such as a property, method, or function, and formats a prompt accordingly. The generated prompt instructs the user to write a natural language description of the identified code element without using technical jargon or including extraneous information such as external links or code examples. References to other types within the comment should be enclosed using {@link} syntax, and the style should align with typical TypeScript library documentation conventions.

func Target

func Target(identifier string) string

Target constructs a descriptive string for an identifier by categorizing it and appending relevant information based on its type, such as the name of a class, the signature of a function, or the association of a method or property with its owner. It handles various identifier types including variables, classes, interfaces, functions, methods, properties, and custom types. If the identifier does not conform to expected patterns or types, it is returned as-is.

Types

type Finder

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

Finder provides functionality for locating specific symbols within TypeScript code. It supports customization through options that can specify which symbols to look for, whether to include documented symbols in the search, and an optional logger for logging purposes. Finder offers methods to execute searches that return either a list of found symbol names or the position of a particular symbol. It handles the execution context and potential errors, returning structured results based on the TypeScript code provided.

func NewFinder

func NewFinder(opts ...FinderOption) *Finder

NewFinder constructs a new Finder instance with the provided options. It returns a pointer to the created Finder. If no logger is provided via options, it assigns a no-operation logger by default. Options can be used to specify which symbols to look for and whether to include documented symbols in the search results.

func (*Finder) Find

func (f *Finder) Find(ctx context.Context, code []byte) ([]string, error)

Find searches for specified symbols in the provided TypeScript code and returns a list of findings. It respects the configured symbols and documentation inclusion settings of the Finder instance. If an error occurs during the search, it is returned along with an empty list. The context parameter allows the search to be canceled or have a deadline.

func (*Finder) Position

func (f *Finder) Position(ctx context.Context, identifier string, code []byte) (Position, error)

Position locates the position of a specified identifier within a given body of code and returns its location as a Position. If the identifier cannot be found or another error occurs, an error is returned instead. The search is conducted within the provided context for cancellation and timeout handling.

type FinderOption

type FinderOption func(*Finder)

FinderOption configures a Finder instance, allowing customization of its behavior during the symbol searching process. It's used to specify which symbols to include, whether to include documented symbols, and to set a logger for outputting information during the search operations.

func IncludeDocumented

func IncludeDocumented(include bool) FinderOption

IncludeDocumented configures a Finder to consider documented symbols in its search. If set to true, the Finder will include symbols with associated documentation in its results; otherwise, it will exclude them. This option can be passed to NewFinder when creating a new instance of Finder.

func Symbols

func Symbols(symbols ...Symbol) FinderOption

Symbols specifies a set of TypeScript symbols to be included in the search by a Finder. It configures a Finder to consider only the provided symbols when performing code analysis operations.

func WithLogger

func WithLogger(log *slog.Logger) FinderOption

WithLogger configures a Finder with a specified logger. It allows for logging within the Finder's operations, utilizing the provided *slog.Logger. This option can be passed to NewFinder to influence its logging behavior.

type Option

type Option func(*Service)

Option represents a configuration function used to customize the behavior of a Service. Each Option takes a pointer to a Service and applies settings or parameters to it, allowing for flexible and composable service configuration. Options are typically passed to the New function to construct a new Service instance with the desired customizations.

func Model

func Model(model string) Option

Model configures a Service with the provided model identifier. It returns an Option that, when applied to a Service, sets its internal model field to the given string. This model identifier is typically used by the Service for operations that require knowledge of a specific model, such as data processing or interaction with external APIs that use different models.

func WithFinder

func WithFinder(f *Finder) Option

WithFinder specifies a finder to be used by the service for operations such as searching and position finding within code. It accepts a *Finder and returns an Option to configure a Service.

type Position

type Position struct {
	Line      int
	Character int
}

Position represents the location within a text document, such as source code. It holds information about a specific point in the document, typically identified by a line number and a character offset on that line. This can be used to pinpoint exact locations, such as where a syntax error occurs or where a particular identifier is defined.

type Service

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

Service provides a set of operations for working with TypeScript code. It allows for the retrieval of supported file extensions, finding specific elements within code, minifying TypeScript source code, generating prompts for code suggestions, and patching existing code with documentation comments. Customization of the service can be achieved using provided options such as specifying a custom finder or model. The service encapsulates functionality that leverages external tools and libraries to process and enhance TypeScript code in various ways.

func New

func New(opts ...Option) *Service

New initializes a new Service with the provided options. If no model is specified through the options, it uses the default model. If no Finder is provided, it initializes a new default Finder. It returns an initialized *Service.

func (*Service) Extensions

func (svc *Service) Extensions() []string

Extensions retrieves the list of file extensions that the Service recognizes and is capable of processing. It returns a slice of strings, each representing a supported file extension.

func (*Service) Find

func (svc *Service) Find(code []byte) ([]string, error)

Find retrieves a list of strings based on the provided code slice. It utilizes the service's internal finder to perform the search within a default context. If the search is successful, it returns the results along with a nil error. If it fails, it returns an empty slice and an error detailing what went wrong.

func (*Service) Minify

func (svc *Service) Minify(code []byte) ([]byte, error)

Minify reduces the size of TypeScript code by removing unnecessary characters without changing its functionality and returns the minified code or an error if the minification fails.

func (*Service) Patch

func (svc *Service) Patch(ctx context.Context, identifier, doc string, code []byte) ([]byte, error)

Patch applies a documentation patch to the source code at the location of a specified identifier. It creates or updates existing documentation based on the provided doc string. If the identifier cannot be located or if any errors occur during the process, an error is returned along with a nil byte slice. Otherwise, it returns the patched source code as a byte slice. The operation is context-aware and can be cancelled through the provided context.Context.

func (*Service) Prompt

func (svc *Service) Prompt(input generate.PromptInput) string

Prompt invokes the generation of a prompt based on the provided input and returns the generated content as a string. It utilizes the underlying prompt generation logic to transform the input into a textual representation suitable for various applications.

type Symbol

type Symbol string

Symbol represents a distinct element or token in the TypeScript language that can be targeted for identification within the source code. It encapsulates a classification of TypeScript entities such as variables, classes, interfaces, functions, methods, and properties. Each instance of Symbol corresponds to a specific kind of these language constructs, allowing for operations like searching and analysis to be performed on the corresponding code segments that define or reference these entities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳