vet/pkg/code/languages/common.go
abhisek e6f6288701
feat: Code analysis framework infra
feat: Building code graph

Refactor to support import processing

Handle relative import name fixup

Add docs for code analysis framework

Update docs to include additional examples

feat: Function call graph

Update code graph to link function decl and calls

Include call node in function calls

feat: Flatten vulnerabilities in CSV reporter

refactor: Maintain separation of concerns for code analysis framework

refactor: Separate storage entities in its own package

feat: Add callback support in code graph builder

docs: Fix code analysis framework docs
Signed-off-by: abhisek <abhisek.datta@gmail.com>
2024-07-11 15:09:11 +05:30

63 lines
1.8 KiB
Go

package languages
import (
"context"
"fmt"
"io"
"github.com/safedep/vet/pkg/code"
"github.com/safedep/vet/pkg/code/nodes"
sitter "github.com/smacker/go-tree-sitter"
)
// Base implementation of a common source language
type commonSourceLanguage struct {
parser *sitter.Parser
lang *sitter.Language
}
func (l *commonSourceLanguage) ParseSource(file code.SourceFile) (*nodes.CST, error) {
fileReader, err := file.Open()
if err != nil {
return nil, fmt.Errorf("failed to open source file: %w", err)
}
defer fileReader.Close()
data, err := io.ReadAll(fileReader)
if err != nil {
return nil, fmt.Errorf("failed to read source: %w", err)
}
tree, err := l.parser.ParseCtx(context.Background(), nil, data)
if err != nil {
return nil, err
}
return nodes.NewCST(tree, l.lang, data), nil
}
func (l *commonSourceLanguage) GetImportNodes(cst *nodes.CST) ([]nodes.CSTImportNode, error) {
return nil, fmt.Errorf("language does not support import nodes")
}
func (l *commonSourceLanguage) GetFunctionDeclarationNodes(cst *nodes.CST) ([]nodes.CSTFunctionNode, error) {
return nil, fmt.Errorf("language does not support function declaration nodes")
}
func (l *commonSourceLanguage) GetFunctionCallNodes(cst *nodes.CST) ([]nodes.CSTFunctionCallNode, error) {
return nil, fmt.Errorf("language does not support function call nodes")
}
func (l *commonSourceLanguage) ResolveImportNameFromPath(relPath string) (string, error) {
return "", fmt.Errorf("language does not support import name resolution")
}
func (l *commonSourceLanguage) ResolveImportPathsFromName(importName string) ([]string, error) {
return nil, fmt.Errorf("language does not support import path resolution")
}
func (l *commonSourceLanguage) GetMeta() code.SourceLanguageMeta {
return code.SourceLanguageMeta{}
}