-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support Golang * Lint * Refactor imports * Introduce extra forces for packages * Add some tests * Allow imports from multiple packages and tweak linter * Fix test * Improve go imports correctness * Load LOC for go files * Fix message * Improve package aliasing correctness * Add file object testing
- Loading branch information
Showing
24 changed files
with
849 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package golang | ||
|
||
type Config struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package golang | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gabotechs/dep-tree/internal/language" | ||
) | ||
|
||
func (l *Language) ParseExports(file *language.FileInfo) (*language.ExportsResult, error) { | ||
content := file.Content.(*File) | ||
results := language.ExportsResult{} | ||
for symbol := range content.AstFile.Scope.Objects { | ||
if len(symbol) == 0 { | ||
continue | ||
} | ||
if symbol[:1] == strings.ToUpper(symbol[:1]) { | ||
results.Exports = append(results.Exports, language.ExportEntry{ | ||
Symbols: []language.ExportSymbol{{ | ||
Original: symbol, | ||
}}, | ||
AbsPath: file.AbsPath, | ||
}) | ||
} | ||
} | ||
return &results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package golang | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExports(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Expected []string | ||
}{ | ||
{ | ||
Name: "exports.go", | ||
Expected: []string{}, | ||
}, | ||
{ | ||
Name: "config.go", | ||
Expected: []string{"Config"}, | ||
}, | ||
{ | ||
Name: "language.go", | ||
Expected: []string{"Extensions", "Language", "NewLanguage"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
a := require.New(t) | ||
|
||
lang, err := NewLanguage(".", &Config{}) | ||
a.NoError(err) | ||
file, err := lang.ParseFile(tt.Name) | ||
a.NoError(err) | ||
exports, err := lang.ParseExports(file) | ||
a.NoError(err) | ||
|
||
actualExports := make([]string, 0) | ||
for _, export := range exports.Exports { | ||
for _, symbol := range export.Symbols { | ||
actualExports = append(actualExports, symbol.Original) | ||
} | ||
} | ||
sort.Strings(tt.Expected) | ||
sort.Strings(actualExports) | ||
|
||
a.Equal(tt.Expected, actualExports) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package golang | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/gabotechs/dep-tree/internal/utils" | ||
"golang.org/x/mod/modfile" | ||
) | ||
|
||
type GoMod struct { | ||
Module string | ||
} | ||
|
||
func _ParseGoMod(file string) (*GoMod, error) { | ||
modBytes, err := os.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
goMod, err := modfile.Parse(file, modBytes, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &GoMod{ | ||
Module: goMod.Module.Mod.Path, | ||
}, nil | ||
} | ||
|
||
var ParseGoMod = utils.Cached1In1OutErr(_ParseGoMod) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package golang | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGoMod(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Expected GoMod | ||
}{ | ||
{ | ||
Name: "../../go.mod", | ||
Expected: GoMod{ | ||
Module: "github.com/gabotechs/dep-tree", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
a := require.New(t) | ||
result, err := ParseGoMod(tt.Name) | ||
a.NoError(err) | ||
a.Equal(tt.Expected, *result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.