Skip to content

Commit

Permalink
Add some non-failing tests (#106)
Browse files Browse the repository at this point in the history
* Add some non-failing tests

* Extend the test
  • Loading branch information
gabotechs authored Jan 1, 2025
1 parent dc4f536 commit 9ab7a3d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
21 changes: 15 additions & 6 deletions internal/js/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
// 2. If is imported from a workspace.
if l.Cfg == nil || l.Cfg.Workspaces {
workspaces, err := NewWorkspaces(dir)
if err != nil {
return "", err
}
absPath, err = workspaces.ResolveFromWorkspaces(unresolved)
if absPath != "" || err != nil {
return absPath, err
Expand All @@ -59,20 +62,26 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
}

// 3.2 then use it for resolving the base url.
absPath = tsConfig.ResolveFromBaseUrl(unresolved)
resolved := tsConfig.ResolveFromBaseUrl(unresolved)
absPath = getFileAbsPath(resolved)
if absPath != "" {
return absPath, nil
}

// 4. If imported from a path override.
if l.Cfg == nil || l.Cfg.TsConfigPaths {
absPath, err = tsConfig.ResolveFromPaths(unresolved)
if err != nil {
return "", err
candidates := tsConfig.ResolveFromPaths(unresolved)

for _, candidate := range candidates {
absPath = getFileAbsPath(candidate)
if absPath != "" {
break
}
}
if absPath != "" {
return absPath, nil
if absPath == "" && len(candidates) > 0 {
return "", fmt.Errorf("import '%s' was matched to path '%s' in tscofing's paths option, but the resolved path did not match an existing file", unresolved, strings.Join(candidates, "', '"))
}
return absPath, nil
}
return "", nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/js/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestParser_ResolvePath(t *testing.T) {
Name: "Does not resolve invalid path override import",
Cwd: filepath.Join(resolverTestFolder, "src"),
Unresolved: "@/helpers/bar",
ExpectedError: "import '@/helpers/bar' was matched to path '@/helpers/' in tscofing's paths option, but the resolved path did not match an existing file",
ExpectedError: "import '@/helpers/bar' was matched to path '.resolve_test/src/helpers/bar' in tscofing's paths option, but the resolved path did not match an existing file",
},
{
Name: "Empty name does not panic",
Expand Down
22 changes: 6 additions & 16 deletions internal/js/tsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package js

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -41,31 +40,22 @@ func ParseTsConfig(filePath string) (TsConfig, error) {
}

func (t *TsConfig) ResolveFromBaseUrl(unresolved string) string {
baseUrl := t.CompilerOptions.BaseUrl
importFromBaseUrl := filepath.Join(t.path, baseUrl, unresolved)
return getFileAbsPath(importFromBaseUrl)
return filepath.Join(t.path, t.CompilerOptions.BaseUrl, unresolved)
}

func (t *TsConfig) ResolveFromPaths(unresolved string) (string, error) {
var failedMatches []string
func (t *TsConfig) ResolveFromPaths(unresolved string) []string {
var candidates []string

for pathOverride, searchPaths := range t.CompilerOptions.Paths {
pathOverride = strings.ReplaceAll(pathOverride, "*", "")
if strings.HasPrefix(unresolved, pathOverride) {
for _, searchPath := range searchPaths {
searchPath = strings.ReplaceAll(searchPath, "*", "")
newImportFrom := strings.ReplaceAll(unresolved, pathOverride, searchPath)
importFromBaseUrlAndPaths := filepath.Join(t.path, t.CompilerOptions.BaseUrl, newImportFrom)
absPath := getFileAbsPath(importFromBaseUrlAndPaths)
if absPath != "" {
return absPath, nil
}
candidates = append(candidates, importFromBaseUrlAndPaths)
}
failedMatches = append(failedMatches, pathOverride)
}
}
if failedMatches != nil {
return "", fmt.Errorf("import '%s' was matched to path '%s' in tscofing's paths option, but the resolved path did not match an existing file", unresolved, strings.Join(failedMatches, "', '"))
} else {
return "", nil
}
return candidates
}
49 changes: 49 additions & 0 deletions internal/js/tsconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package js

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestTsConfig_ResolveFromPaths(t *testing.T) {
tests := []struct {
Name string
TsConfig TsConfig
Import string
Result []string
}{
{
Name: "without globstar",
TsConfig: TsConfig{
path: "/foo",
CompilerOptions: CompilerOptions{
BaseUrl: "./bar/baz",
Paths: map[string][]string{"@/": {"./src/"}},
},
},
Import: "@/a/b/c",
Result: []string{"/foo/bar/baz/src/a/b/c"},
},
{
Name: "without globstar (2)",
TsConfig: TsConfig{
path: "/foo",
CompilerOptions: CompilerOptions{
BaseUrl: "./bar/baz",
Paths: map[string][]string{"@Environment": {"./src/environments/environments.ts"}},
},
},
Import: "@Environment",
Result: []string{"/foo/bar/baz/src/environments/environments.ts"},
},
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
a := require.New(t)
result := tt.TsConfig.ResolveFromPaths(tt.Import)
a.Equal(tt.Result, result)
})
}
}

0 comments on commit 9ab7a3d

Please sign in to comment.