From 9ab7a3d83c715bd5fde0a7f570918ed285590ca1 Mon Sep 17 00:00:00 2001 From: Gabriel <45515538+gabotechs@users.noreply.github.com> Date: Wed, 1 Jan 2025 17:23:15 +0100 Subject: [PATCH] Add some non-failing tests (#106) * Add some non-failing tests * Extend the test --- internal/js/resolve.go | 21 +++++++++++----- internal/js/resolve_test.go | 2 +- internal/js/tsconfig.go | 22 +++++----------- internal/js/tsconfig_test.go | 49 ++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 internal/js/tsconfig_test.go diff --git a/internal/js/resolve.go b/internal/js/resolve.go index 7d157be..60acabf 100644 --- a/internal/js/resolve.go +++ b/internal/js/resolve.go @@ -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 @@ -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 } diff --git a/internal/js/resolve_test.go b/internal/js/resolve_test.go index 49e5c9e..95489ee 100644 --- a/internal/js/resolve_test.go +++ b/internal/js/resolve_test.go @@ -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", diff --git a/internal/js/tsconfig.go b/internal/js/tsconfig.go index 3f42b78..b0bfd83 100644 --- a/internal/js/tsconfig.go +++ b/internal/js/tsconfig.go @@ -2,7 +2,6 @@ package js import ( "encoding/json" - "fmt" "os" "path/filepath" "strings" @@ -41,13 +40,12 @@ 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) { @@ -55,17 +53,9 @@ func (t *TsConfig) ResolveFromPaths(unresolved string) (string, error) { 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 } diff --git a/internal/js/tsconfig_test.go b/internal/js/tsconfig_test.go new file mode 100644 index 0000000..819cb2a --- /dev/null +++ b/internal/js/tsconfig_test.go @@ -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) + }) + } +}