feat: [go] add pre extentions

上级 272e057e
......@@ -68,10 +68,18 @@ func AnalysisGo() []core_domain.CodeDataStruct {
func CommentAnalysis(path string, app app_concept.AbstractAnalysisApp, filter func(path string) bool, isFunctionBase bool) []core_domain.CodeDataStruct {
var results []core_domain.CodeFile
files := cocafile.GetFilesWithFilter(path, filter)
fmt.Println(files)
var imports []core_domain.CodeImport
for _, file := range files {
content, _ := ioutil.ReadFile(file)
codeImports := app.AnalysisImport(string(content), file)
imports = append(imports, codeImports...)
}
for _, file := range files {
fmt.Fprintf(output, "Process file: %s\n", file)
content, _ := ioutil.ReadFile(file)
app.SetExtensions(imports)
result := app.Analysis(string(content), file)
results = append(results, result)
}
......
......@@ -4,4 +4,6 @@ import "github.com/phodal/coca/pkg/domain/core_domain"
type AbstractAnalysisApp interface {
Analysis(code string, path string) core_domain.CodeFile
AnalysisImport(s string, file string) []core_domain.CodeImport
SetExtensions(extension interface{})
}
......@@ -6,10 +6,23 @@ import (
)
type GoIdentApp struct {
Extensions interface{}
}
func (g *GoIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
parser := cocago.NewCocagoParser()
return *parser.ProcessString(code, fileName)
var imports []core_domain.CodeImport
if g.Extensions != nil {
imports = g.Extensions.([]core_domain.CodeImport)
}
return *parser.ProcessString(code, fileName, imports)
}
func (g *GoIdentApp) AnalysisImport(code string, fileName string) []core_domain.CodeImport {
parser := cocago.NewCocagoParser()
return parser.ProcessImports(code, fileName)
}
func (g *GoIdentApp) SetExtensions(extension interface{}) {
g.Extensions = extension
}
......@@ -22,7 +22,7 @@ type PythonIdentApp struct {
}
func (j *PythonIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
func (p *PythonIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
scriptParser := ProcessTsString(code)
context := scriptParser.Root()
......@@ -31,3 +31,11 @@ func (j *PythonIdentApp) Analysis(code string, fileName string) core_domain.Code
return listener.GetCodeFileInfo()
}
func (p *PythonIdentApp) SetExtensions(extension interface{}) {
}
func (p *PythonIdentApp) AnalysisImport(code string, fileName string) []core_domain.CodeImport {
return nil
}
......@@ -19,10 +19,9 @@ func ProcessTsString(code string) *parser.TypeScriptParser {
}
type TypeScriptIdentApp struct {
}
func (j *TypeScriptIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
func (t *TypeScriptIdentApp) Analysis(code string, fileName string) core_domain.CodeFile {
scriptParser := ProcessTsString(code)
context := scriptParser.Program()
......@@ -31,3 +30,11 @@ func (j *TypeScriptIdentApp) Analysis(code string, fileName string) core_domain.
return listener.GetNodeInfo()
}
func (t *TypeScriptIdentApp) SetExtensions(extension interface{}) {
}
func (t *TypeScriptIdentApp) AnalysisImport(code string, fileName string) []core_domain.CodeImport {
return nil
}
......@@ -18,6 +18,7 @@ import (
var currentPackage *core_domain.CodePackage
type CocagoParser struct {
Imports []core_domain.CodeImport
}
var output io.Writer
......@@ -41,11 +42,12 @@ func (n *CocagoParser) ProcessFile(fileName string) core_domain.CodeFile {
code := string(content)
codeFile := n.ProcessString(code, fileName)
codeFile := n.ProcessString(code, fileName, nil)
return *codeFile
}
func (n *CocagoParser) ProcessString(code string, fileName string) *core_domain.CodeFile {
func (n *CocagoParser) ProcessString(code string, fileName string, codeImports []core_domain.CodeImport) *core_domain.CodeFile {
n.Imports = codeImports
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fileName, code, 0)
if err != nil {
......@@ -57,6 +59,31 @@ func (n *CocagoParser) ProcessString(code string, fileName string) *core_domain.
return codeFile
}
func (n *CocagoParser) ProcessImports(code string, fileName string) []core_domain.CodeImport {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fileName, code, 0)
if err != nil {
panic(err)
}
imports := n.VisitorImport(f, fset, fileName)
return imports
}
func (n *CocagoParser) VisitorImport(f *ast.File, fset *token.FileSet, fileName string) []core_domain.CodeImport {
var imports []core_domain.CodeImport
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.ImportSpec:
imp := BuildImport(x)
imports = append(imports, *imp)
}
return true
})
return imports
}
func (n *CocagoParser) Visitor(f *ast.File, fset *token.FileSet, fileName string) *core_domain.CodeFile {
var currentStruct core_domain.CodeDataStruct
var currentFile core_domain.CodeFile
......@@ -246,7 +273,7 @@ func BuildExpr(expr ast.Expr) (string, string, string) {
for _, arg := range x.Args {
argType, argValue, argKind := BuildExpr(arg)
if argType == "selector" {
callArgs = append(callArgs, argValue + "." + argKind)
callArgs = append(callArgs, argValue+"."+argKind)
}
}
return "call", value, strings.Join(callArgs, ",")
......@@ -290,7 +317,7 @@ func AddStructType(currentNodeName string, x *ast.StructType, currentFile *core_
ioproperties = append(ioproperties, *property)
}
// todo : when dsMap key-value create it
// todo : when dsMap key-value create it
if dsMap[currentNodeName] != nil {
dsMap[currentNodeName].InOutProperties = ioproperties
}
......
......@@ -113,7 +113,7 @@ func main() {
defer l.Unlock()
fmt.Println("1")
}
`, "call")
`, "call", nil)
calls := results.Members[0].FunctionNodes[0].FunctionCalls
fmt.Println(calls)
g.Expect(len(results.Fields)).To(Equal(1))
......@@ -142,7 +142,7 @@ func main() {
defer l.Unlock()
fmt.Println("1")
}
`, "call")
`, "call", nil)
calls := results.Members[0].FunctionNodes[0].FunctionCalls
fmt.Println(calls)
g.Expect(calls[0].Package).To(Equal("sync"))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册