Documentation ¶
Index ¶
- Variables
- func AddRuntimeFile(unit *core.ExecutionUnit, templateData any, path string, content []byte) error
- func AddRuntimeFiles(unit *core.ExecutionUnit, files embed.FS, templateData any) error
- func CommentNodes(oldFileContent string, nodesToComment ...string) string
- func DoQuery(c *sitter.Node, q string) query.NextMatchFunc
- func EnsureRuntimeImport(filepath string, runtimePath string, varPrefix string, content string) (newContent string, err error)
- func EnsureRuntimeImportFile(runtimePath string, varPrefix string, file *core.SourceFile) (err error)
- func FileToLocalModule(path string) (module string)
- func FileToModule(path string) (module string)
- func FindDefaultExport(n *sitter.Node) *sitter.Node
- func FindExportForVar(n *sitter.Node, varName string) *sitter.Node
- func FindFileForImport(files map[string]core.File, importingFilePath string, module string) (f core.File, err error)
- func GetFileForModule(results *core.CompilationResult, moduleName string) *core.SourceFile
- func ImportHasName(name string) predicate.Predicate[Import]
- func ImportUsageQuery(n *sitter.Node, importName string) []*sitter.Node
- func ImportedAs(localName string) predicate.Predicate[Import]
- func IsImportInScope(scope ImportScope) predicate.Predicate[Import]
- func IsImportOfKind(importKind ImportKind) predicate.Predicate[Import]
- func IsImportOfModule(sourcePath string) predicate.Predicate[Import]
- func IsImportOfType(importType ImportType) predicate.Predicate[Import]
- func IsRelativeImport(p Import) bool
- func IsRelativeImportOfModule(sourcePath string) predicate.Predicate[Import]
- func NewFile(path string, content io.Reader) (f *core.SourceFile, err error)
- func NewRuntimeImport(ctx RuntimeImport, w io.Writer) error
- func ResolveFileDependencies(fs map[string]core.File) (execunit.FileDependencies, error)
- func RuntimePath(sourcePath string, runtimeModule string) (string, error)
- func SpecificAsyncFuncDecl(n *sitter.Node, funcName string) *sitter.Node
- func SpecificExportQuery(n *sitter.Node, wantName string) *sitter.Node
- func StringLiteralContent(node *sitter.Node) string
- func UnitFileDependencyResolver(unit *core.ExecutionUnit) (execunit.FileDependencies, error)
- func ValidateModule(match map[string]*sitter.Node, f *core.SourceFile) bool
- type AddExecRuntimeFiles
- type AnnotationFilter
- type EmitterSubscriberProxy
- type EmitterSubscriberProxyEntry
- type ExpressHandler
- type FileImports
- type Import
- type ImportKind
- type ImportScope
- type ImportSourceType
- type ImportType
- type JavascriptPlugins
- type NestJsHandler
- type NodeJSExecutable
- type NodePackageJson
- type OrmKind
- type PackageFile
- type Persist
- type Pubsub
- type Runtime
- type RuntimeImport
- type TransformResult
- type VarDeclarations
- type VarParseStructs
- type VarSpec
Constants ¶
This section is empty.
Variables ¶
var ( // ImportTypeNamed represents an import of a specific named export from a module (other than "default") ImportTypeNamed = ImportType("named") // ImportTypeDefault represents an import of the default export from a module ImportTypeDefault = ImportType("default") // ImportTypeNamespace represents an import of all exported names from a module ImportTypeNamespace = ImportType("namespace") // ImportTypeSideEffect represents an import that is not assigned to a variable ImportTypeSideEffect = ImportType("side-effect") // ImportTypeField represents an imported field of a named export // (e.g. const { prop } = require("module").name or const alias = require("module").name.field ...) ImportTypeField = ImportType("field") )
var ( ImportScopeModule = ImportScope("module") ImportScopeLocal = ImportScope("local") )
var ( ImportSourceTypeLocalModule = ImportSourceType("local-module") ImportSourceTypeNodeModule = ImportSourceType("node-module") )
var ( ImportKindCommonJS = ImportKind("commonjs") ImportKindES = ImportKind("es") )
var Language = core.SourceLanguage{ ID: js, Sitter: javascript.GetLanguage(), CapabilityFinder: lang.NewCapabilityFinder("comment", lang.CompositePreprocessor( lang.RegexpRemovePreprocessor(`//\s*`), func(comment string) string { if !strings.HasPrefix(comment, "/*") { return comment } comment = comment[1 : len(comment)-1] comment = multilineCommentMarginRegexp.ReplaceAllString(comment, "") return comment }, )), TurnIntoComment: lang.MakeLineCommenter("// "), }
Functions ¶
func AddRuntimeFile ¶
func AddRuntimeFiles ¶
func CommentNodes ¶
CommentNode TODO: this currently only uses line comments, which is not always the right behaviour.
func DoQuery ¶
func DoQuery(c *sitter.Node, q string) query.NextMatchFunc
DoQuery is a thin wrapper around `query.Exec` to use javascript as the Language.
func EnsureRuntimeImport ¶
func EnsureRuntimeImport(filepath string, runtimePath string, varPrefix string, content string) (newContent string, err error)
EnsureRuntimeImport makes sure that `file` has an import for `runtimePath` (relative to `filepath`) with name based on `varPrefix`. `runtimePath` is relative to klotho_runtime (ie, don't do "klotho_runtime/my_runtime_module") and is often the same as varPrefix
func EnsureRuntimeImportFile ¶
func EnsureRuntimeImportFile(runtimePath string, varPrefix string, file *core.SourceFile) (err error)
EnsureRuntimeImportFile makes sure that `file` has an import for `runtimePath` with name based on `varPrefix`. `runtimePath` is relative to klotho_runtime (ie, don't do "klotho_runtime/my_runtime_module") and is often the same as varPrefix ! Calls `file.Reparse` if the import is missing.
func FileToLocalModule ¶
FileToLocalModule removes all the extraneous parts of the file path while still being resolvable by node's require. Also appends a leading `./` if the path is not already relative to convert from file paths in an execution unit which do not have the leading `./` (required for relative imports by node).
func FileToModule ¶
FileToModule removes all the extraneous parts of the file path while still being resolvable by node's require.
func FindExportForVar ¶
FindExportForVar returns the local variable that is exported as 'varName' (to handle cases where they don't match).
func FindFileForImport ¶
func FindFileForImport(files map[string]core.File, importingFilePath string, module string) (f core.File, err error)
FindFileForImport is the reverse of `FindImportOfFile`.
func GetFileForModule ¶
func GetFileForModule(results *core.CompilationResult, moduleName string) *core.SourceFile
func IsImportInScope ¶
func IsImportInScope(scope ImportScope) predicate.Predicate[Import]
func IsImportOfKind ¶
func IsImportOfKind(importKind ImportKind) predicate.Predicate[Import]
func IsImportOfModule ¶
IsImportOfModule matches any imports with sources that match the following forms for the supplied path:
| `require(X)` | description | | ------------ | ----------- | | `X` | If X is a file, load X as its file extension format. | | `X.js` | If X.js is a file, load X.js as JavaScript text. | | `X/index.js` | If X/index.js is a file, load X/index.js as JavaScript text. | | `X/` | (load as `./X/index.js`) |
Unlike IsRelativeImportOfModule, no './' prefix is applied to sourcePath.
func IsImportOfType ¶
func IsImportOfType(importType ImportType) predicate.Predicate[Import]
func IsRelativeImport ¶
func IsRelativeImportOfModule ¶
IsRelativeImportOfModule matches any local imports with sources that match the following forms for the supplied path:
| `require(X)` | description | | ------------ | ----------- | | `./X` | If X is a file, load X as its file extension format. | | `./X.js` | If X.js is a file, load X.js as JavaScript text. | | `./X/index.js` | If X/index.js is a file, load X/index.js as JavaScript text. | | `./X/` | (load as `./X/index.js`) |
This predicate does not match absolute imports supported by Webpack or Babel
func NewRuntimeImport ¶
func NewRuntimeImport(ctx RuntimeImport, w io.Writer) error
func ResolveFileDependencies ¶
func SpecificAsyncFuncDecl ¶
func StringLiteralContent ¶
func UnitFileDependencyResolver ¶
func UnitFileDependencyResolver(unit *core.ExecutionUnit) (execunit.FileDependencies, error)
UnitFileDependencyResolver resolves the execunit.FileDependencies for the provided core.ExecutionUnit.
func ValidateModule ¶
Types ¶
type AddExecRuntimeFiles ¶
type AddExecRuntimeFiles struct {
// contains filtered or unexported fields
}
func (AddExecRuntimeFiles) Name ¶
func (p AddExecRuntimeFiles) Name() string
func (AddExecRuntimeFiles) Transform ¶
func (p AddExecRuntimeFiles) Transform(result *core.CompilationResult, deps *core.Dependencies) error
type AnnotationFilter ¶
type AnnotationFilter func(declaringFile *core.SourceFile, annot *core.Annotation) bool
func FilterByCapability ¶
func FilterByCapability(capability string) AnnotationFilter
type EmitterSubscriberProxy ¶
type EmitterSubscriberProxy struct { RuntimeImport string Path string Entries []EmitterSubscriberProxyEntry }
type ExpressHandler ¶
type ExpressHandler struct { Config *config.Application // contains filtered or unexported fields }
func (ExpressHandler) Name ¶
func (p ExpressHandler) Name() string
func (ExpressHandler) Transform ¶
func (p ExpressHandler) Transform(result *core.CompilationResult, deps *core.Dependencies) error
type FileImports ¶
FileImports provides a mapping between import sources and the list of imports for each.
func FindImportsAtNode ¶
func FindImportsAtNode(node *sitter.Node) FileImports
FindImportsAtNode returns a map containing a list of imports for each import source starting from the supplied node.
func FindImportsInFile ¶
func FindImportsInFile(file *core.SourceFile) FileImports
FindImportsInFile returns a map containing a list of imports for each import source referenced within the file.
func (FileImports) AsSlice ¶
func (imports FileImports) AsSlice() []Import
AsSlice converts an instance of FileImports to []Import for simpler iteration over all Import values.
type Import ¶
type Import struct { // Source could be the name of a node module, a project-local module, or a filename Source string // Name is the exported name of the Import Name string // ImportNode is the *sitter.Node associated with the Import's import statement ImportNode *sitter.Node // SourceNode is the *sitter.Node associated with the import's Source. // For CJS imports, this will include the 'require()' expression. SourceNode *sitter.Node // Alias is the name with which this import is referred to in its enclosing Scope (i.e. module or local) Alias string Scope ImportScope Type ImportType Kind ImportKind }
func FindImportForVar ¶
FindImportForVar returns the import assigned to the supplied variable name starting from the *sitter.Node 'n' in the source and a boolean reflecting whether an appropriate import was found or not.
func FindNextImportStatement ¶
FindNextImportStatement returns the imports associated with the next import statement/expression starting at the supplied node (typically starting from an annotation comment node).
func (*Import) ImportedAs ¶
ImportedAs returns the name of the import as it will be used locally (either the exported name or local alias).
type ImportKind ¶
type ImportKind string
type ImportScope ¶
type ImportScope string
type ImportSourceType ¶
type ImportSourceType string
func ResolveImportSourceType ¶
func ResolveImportSourceType(source string, projectFilePaths []string, enableAbsolute bool) ImportSourceType
ResolveImportSourceType resolves ImportSourceType for the supplied source using the supplied project file paths as context.
This function returns ImportSourceTypeLocalModule if the source either starts with a period ('.') or a variant exists in the supplied projectFilePaths slice. If the source type cannot be resolved locally, it is assumed to be of type ImportSourceTypeNodeModule.
type ImportType ¶
type ImportType string
type JavascriptPlugins ¶
func NewJavascriptPlugins ¶
func NewJavascriptPlugins(cfg *config.Application, runtime Runtime) *JavascriptPlugins
func (JavascriptPlugins) Name ¶
func (c JavascriptPlugins) Name() string
func (JavascriptPlugins) Transform ¶
func (c JavascriptPlugins) Transform(result *core.CompilationResult, deps *core.Dependencies) error
type NestJsHandler ¶
type NestJsHandler struct { Config *config.Application // contains filtered or unexported fields }
func (NestJsHandler) Name ¶
func (p NestJsHandler) Name() string
func (NestJsHandler) Transform ¶
func (p NestJsHandler) Transform(result *core.CompilationResult, deps *core.Dependencies) error
type NodeJSExecutable ¶
type NodeJSExecutable struct { }
func (NodeJSExecutable) Name ¶
func (l NodeJSExecutable) Name() string
func (NodeJSExecutable) Transform ¶
func (l NodeJSExecutable) Transform(result *core.CompilationResult, dependencies *core.Dependencies) error
type NodePackageJson ¶
type NodePackageJson struct { Dependencies map[string]string DevDependencies map[string]string OtherFields map[string]json.RawMessage // contains filtered or unexported fields }
NodePackageJson represents the type described in https://docs.npmjs.com/cli/v8/configuring-npm/package-json
func (*NodePackageJson) Clone ¶
func (n *NodePackageJson) Clone() *NodePackageJson
func (*NodePackageJson) MarshalJSON ¶
func (n *NodePackageJson) MarshalJSON() ([]byte, error)
func (*NodePackageJson) Merge ¶
func (n *NodePackageJson) Merge(other *NodePackageJson)
func (*NodePackageJson) UnmarshalJSON ¶
func (n *NodePackageJson) UnmarshalJSON(b []byte) error
type PackageFile ¶
type PackageFile struct { Content *NodePackageJson // contains filtered or unexported fields }
func NewPackageFile ¶
func NewPackageFile(path string, content io.Reader) (*PackageFile, error)
func (*PackageFile) Clone ¶
func (f *PackageFile) Clone() core.File
func (*PackageFile) Path ¶
func (f *PackageFile) Path() string
type Persist ¶
type Persist struct {
// contains filtered or unexported fields
}
func (Persist) Transform ¶
func (p Persist) Transform(result *core.CompilationResult, deps *core.Dependencies) error
type Pubsub ¶
type Pubsub struct {
// contains filtered or unexported fields
}
func (Pubsub) Transform ¶
func (p Pubsub) Transform(result *core.CompilationResult, deps *core.Dependencies) error
type Runtime ¶
type Runtime interface { // TransformPersist applies any runtime-specific transformations to the given file for the annotation. Returns the modified source code, to be `Reparse`d by the caller. TransformPersist(file *core.SourceFile, annot *core.Annotation, kind core.PersistKind) error AddKvRuntimeFiles(unit *core.ExecutionUnit) error AddFsRuntimeFiles(unit *core.ExecutionUnit, envVarName string, id string) error AddSecretRuntimeFiles(unit *core.ExecutionUnit) error AddOrmRuntimeFiles(unit *core.ExecutionUnit) error AddRedisNodeRuntimeFiles(unit *core.ExecutionUnit) error AddRedisClusterRuntimeFiles(unit *core.ExecutionUnit) error AddPubsubRuntimeFiles(unit *core.ExecutionUnit) error AddProxyRuntimeFiles(unit *core.ExecutionUnit, proxyType string) error AddExecRuntimeFiles(unit *core.ExecutionUnit, result *core.CompilationResult, deps *core.Dependencies) error }
type RuntimeImport ¶
type TransformResult ¶
type TransformResult struct { // NewFileContent contains the new file content in its entirety, after transformations have been applied. This additionally allows // for changes to the file outside of the annotation's node (such as adding new imports). NewFileContent string // NewAnnotationContent contains just the new annotation node's content. This is required for any further // plugin transformations, applied after the runtime's transformation. NewAnnotationContent string }
TransformResult is the result of a runtime applying a transformation to a specific file and annotation.
type VarDeclarations ¶
type VarDeclarations map[VarSpec]*VarParseStructs
func DiscoverDeclarations ¶
func DiscoverDeclarations(files map[string]core.File, queryMatchType string, queryMatchTypeModule string, requireExport bool, annotationFilter AnnotationFilter) VarDeclarations
DiscoverDeclarations finds the var declarations, using the `varFinder`'s `searchSpec`.
func (VarDeclarations) SplitByFile ¶
func (v VarDeclarations) SplitByFile() map[string]VarDeclarations
SplitByFile splits this VarDeclarations into several, each corresponding to the declarations for a single file.
The keys of the map are the file paths.
type VarParseStructs ¶
type VarParseStructs struct { File *core.SourceFile Annotation *core.Annotation }
VarParseStructs hold data about a variable in a javascript file.
VarSpecStructs often come paired with a `VarSpec`, which tells you the variable name within the file.
type VarSpec ¶
type VarSpec struct { // DefinedIn is the path of the file that this variable is in DefinedIn string // InternalName is the variable name as appears on the left-hand side of the assignment (if assigned directly to exports) or declaration (if not). InternalName string // VarName is the name that is exported for other modules to use VarName string }
VarSpec defines a variable defined in a file
VarSpecs often come paired with `VarParseStructs`, which contain the tree-sitter file and nodes, along with some metadata.