Skip to content

Commit 9ab7a3d

Browse files
authored
Add some non-failing tests (#106)
* Add some non-failing tests * Extend the test
1 parent dc4f536 commit 9ab7a3d

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

internal/js/resolve.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
3434
// 2. If is imported from a workspace.
3535
if l.Cfg == nil || l.Cfg.Workspaces {
3636
workspaces, err := NewWorkspaces(dir)
37+
if err != nil {
38+
return "", err
39+
}
3740
absPath, err = workspaces.ResolveFromWorkspaces(unresolved)
3841
if absPath != "" || err != nil {
3942
return absPath, err
@@ -59,20 +62,26 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
5962
}
6063

6164
// 3.2 then use it for resolving the base url.
62-
absPath = tsConfig.ResolveFromBaseUrl(unresolved)
65+
resolved := tsConfig.ResolveFromBaseUrl(unresolved)
66+
absPath = getFileAbsPath(resolved)
6367
if absPath != "" {
6468
return absPath, nil
6569
}
6670

6771
// 4. If imported from a path override.
6872
if l.Cfg == nil || l.Cfg.TsConfigPaths {
69-
absPath, err = tsConfig.ResolveFromPaths(unresolved)
70-
if err != nil {
71-
return "", err
73+
candidates := tsConfig.ResolveFromPaths(unresolved)
74+
75+
for _, candidate := range candidates {
76+
absPath = getFileAbsPath(candidate)
77+
if absPath != "" {
78+
break
79+
}
7280
}
73-
if absPath != "" {
74-
return absPath, nil
81+
if absPath == "" && len(candidates) > 0 {
82+
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, "', '"))
7583
}
84+
return absPath, nil
7685
}
7786
return "", nil
7887
}

internal/js/resolve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestParser_ResolvePath(t *testing.T) {
6969
Name: "Does not resolve invalid path override import",
7070
Cwd: filepath.Join(resolverTestFolder, "src"),
7171
Unresolved: "@/helpers/bar",
72-
ExpectedError: "import '@/helpers/bar' was matched to path '@/helpers/' in tscofing's paths option, but the resolved path did not match an existing file",
72+
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",
7373
},
7474
{
7575
Name: "Empty name does not panic",

internal/js/tsconfig.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package js
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"os"
76
"path/filepath"
87
"strings"
@@ -41,31 +40,22 @@ func ParseTsConfig(filePath string) (TsConfig, error) {
4140
}
4241

4342
func (t *TsConfig) ResolveFromBaseUrl(unresolved string) string {
44-
baseUrl := t.CompilerOptions.BaseUrl
45-
importFromBaseUrl := filepath.Join(t.path, baseUrl, unresolved)
46-
return getFileAbsPath(importFromBaseUrl)
43+
return filepath.Join(t.path, t.CompilerOptions.BaseUrl, unresolved)
4744
}
4845

49-
func (t *TsConfig) ResolveFromPaths(unresolved string) (string, error) {
50-
var failedMatches []string
46+
func (t *TsConfig) ResolveFromPaths(unresolved string) []string {
47+
var candidates []string
48+
5149
for pathOverride, searchPaths := range t.CompilerOptions.Paths {
5250
pathOverride = strings.ReplaceAll(pathOverride, "*", "")
5351
if strings.HasPrefix(unresolved, pathOverride) {
5452
for _, searchPath := range searchPaths {
5553
searchPath = strings.ReplaceAll(searchPath, "*", "")
5654
newImportFrom := strings.ReplaceAll(unresolved, pathOverride, searchPath)
5755
importFromBaseUrlAndPaths := filepath.Join(t.path, t.CompilerOptions.BaseUrl, newImportFrom)
58-
absPath := getFileAbsPath(importFromBaseUrlAndPaths)
59-
if absPath != "" {
60-
return absPath, nil
61-
}
56+
candidates = append(candidates, importFromBaseUrlAndPaths)
6257
}
63-
failedMatches = append(failedMatches, pathOverride)
6458
}
6559
}
66-
if failedMatches != nil {
67-
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, "', '"))
68-
} else {
69-
return "", nil
70-
}
60+
return candidates
7161
}

internal/js/tsconfig_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package js
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestTsConfig_ResolveFromPaths(t *testing.T) {
10+
tests := []struct {
11+
Name string
12+
TsConfig TsConfig
13+
Import string
14+
Result []string
15+
}{
16+
{
17+
Name: "without globstar",
18+
TsConfig: TsConfig{
19+
path: "/foo",
20+
CompilerOptions: CompilerOptions{
21+
BaseUrl: "./bar/baz",
22+
Paths: map[string][]string{"@/": {"./src/"}},
23+
},
24+
},
25+
Import: "@/a/b/c",
26+
Result: []string{"/foo/bar/baz/src/a/b/c"},
27+
},
28+
{
29+
Name: "without globstar (2)",
30+
TsConfig: TsConfig{
31+
path: "/foo",
32+
CompilerOptions: CompilerOptions{
33+
BaseUrl: "./bar/baz",
34+
Paths: map[string][]string{"@Environment": {"./src/environments/environments.ts"}},
35+
},
36+
},
37+
Import: "@Environment",
38+
Result: []string{"/foo/bar/baz/src/environments/environments.ts"},
39+
},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.Name, func(t *testing.T) {
44+
a := require.New(t)
45+
result := tt.TsConfig.ResolveFromPaths(tt.Import)
46+
a.Equal(tt.Result, result)
47+
})
48+
}
49+
}

0 commit comments

Comments
 (0)