Skip to content

Commit

Permalink
chore: better lang inference
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Jan 31, 2024
1 parent 5d4abb5 commit 74d7104
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
45 changes: 39 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,51 @@ $ dep-tree check`,
return root
}

func inferLang(files []string) string {
score := struct {
js int
python int
rust int
}{}
top := struct {
lang string
v int
}{}
for _, file := range files {
switch {
case utils.EndsWith(file, js.Extensions):
score.js += 1
if score.js > top.v {
top.v = score.js
top.lang = "js"
}
case utils.EndsWith(file, rust.Extensions):
score.rust += 1
if score.rust > top.v {
top.v = score.rust
top.lang = "rust"
}
case utils.EndsWith(file, python.Extensions):
score.python += 1
if score.python > top.v {
top.v = score.python
top.lang = "python"
}
}
}
return top.lang
}

func makeParserBuilder(files []string, cfg *config.Config) (language.NodeParserBuilder, error) {
if len(files) == 0 {
return nil, errors.New("at least one file must be provided")
}
// TODO: There's smarter ways to check which language are we in than just reading the
// first encountered file extension.
switch {
case utils.EndsWith(files[0], js.Extensions):
switch inferLang(files) {
case "js":
return language.ParserBuilder(js.MakeJsLanguage, &cfg.Js, cfg), nil
case utils.EndsWith(files[0], rust.Extensions):
case "rust":
return language.ParserBuilder(rust.MakeRustLanguage, &cfg.Rust, cfg), nil
case utils.EndsWith(files[0], python.Extensions):
case "python":
return language.ParserBuilder(python.MakePythonLanguage, &cfg.Python, cfg), nil
default:
return nil, fmt.Errorf("file \"%s\" not supported", files[0])
Expand Down
36 changes: 36 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,39 @@ func TestRoot(t *testing.T) {
})
}
}

func TestInferLang(t *testing.T) {
tests := []struct {
Name string
Files []string
Expected string
}{
{
Name: "only 1 file",
Files: []string{"foo.js"},
Expected: "js",
},
{
Name: "majority of files",
Files: []string{"foo.js", "bar.js", "foo.rs", "foo.py"},
Expected: "js",
},
{
Name: "unrelated files",
Files: []string{"foo.js", "foo.pdf"},
Expected: "js",
},
{
Name: "no match",
Files: []string{"foo.pdf", "bar.docx"},
Expected: "",
},
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
a := require.New(t)
a.Equal(tt.Expected, inferLang(tt.Files))
})
}
}

0 comments on commit 74d7104

Please sign in to comment.