Skip to content

Commit

Permalink
fix: review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joelMuehlena committed Aug 13, 2023
1 parent bb0e82d commit d3abd3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
33 changes: 18 additions & 15 deletions gazelle/js/typescript/tsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -61,9 +60,13 @@ var DefaultConfigPaths = TsConfigPaths{
Map: &map[string][]string{},
}

// Matches strings starting with ../, ./, @ or .
// Used to detect if a path could possibly expanded using baseUrl
var baseurl_validation_regex = regexp.MustCompile(`^(\.\.?\/|@|\.)`)
func isRelativePath(p string) bool {
if path.IsAbs(p) {
return false
}

return strings.HasPrefix(p, "./") || strings.HasPrefix(p, "../")
}

// parseTsConfigJSONFile loads a tsconfig.json file and return the compilerOptions config
func parseTsConfigJSONFile(cm *TsConfigMap, root, dir, tsconfig string) (*TsConfig, error) {
Expand Down Expand Up @@ -118,8 +121,6 @@ func parseTsConfigJSON(cm *TsConfigMap, root, configDir string, tsconfigContent
var BaseUrl string
if c.CompilerOptions.BaseUrl != nil {
BaseUrl = path.Clean(*c.CompilerOptions.BaseUrl)
} else if baseConfig != nil && baseConfig.BaseUrl != "" {
BaseUrl = path.Join(baseConfigRel, baseConfig.BaseUrl)
} else {
BaseUrl = "."
}
Expand Down Expand Up @@ -161,14 +162,16 @@ func parseTsConfigJSON(cm *TsConfigMap, root, configDir string, tsconfigContent
return &config, nil
}

// Returns the path from the bazel-root to the active tsconfig.json file
// Returns the path from the project base to the active tsconfig.json file
// This is used to build the path from the project base to the file being imported
// because gazelle seems to resolve files relative to the project base
// if the passed path is not absolute.
// Or an empty string if the path is absolute
func (c TsConfig) getRelativeExpansionIfLocal(importPath string) string {
func (c TsConfig) expandRelativePath(importPath string) string {
// Absolute paths must never be expanded but everything else must be relative to the bazel-root
// and therefore expanded with the path to the current active tsconfig.json
if !path.IsAbs(importPath) {
BazelLog.Debugf("Found local path %s in tsconfig.json. Should be expanded with tsconfig dir: %s", importPath, c.ConfigDir)
BazelLog.Tracef("Found local path %s in tsconfig.json. Should be expanded with tsconfig dir: %s", importPath, c.ConfigDir)
return c.ConfigDir
}
return ""
Expand All @@ -185,7 +188,7 @@ func (c TsConfig) ExpandPaths(from, p string) []string {
// Check for exact 'paths' matches first
exact := (*pathMap)[p]
for _, m := range exact {
possible = append(possible, path.Clean(path.Join(c.getRelativeExpansionIfLocal(m), c.Paths.Rel, m)))
possible = append(possible, path.Join(c.expandRelativePath(m), c.Paths.Rel, m))
}

// Check for pattern matches next
Expand Down Expand Up @@ -214,25 +217,25 @@ func (c TsConfig) ExpandPaths(from, p string) []string {
matchedText := p[len(m.prefix) : len(p)-len(m.suffix)]
mappedPath := strings.Replace(originalPath, "*", matchedText, 1)

possible = append(possible, path.Join(c.getRelativeExpansionIfLocal(mappedPath), c.Paths.Rel, mappedPath))
possible = append(possible, path.Join(c.expandRelativePath(mappedPath), c.Paths.Rel, mappedPath))
}
}

// Expand paths from baseUrl
// Must not to be absolute or relative to be expanded
// https://www.typescriptlang.org/tsconfig#baseUrl
if !baseurl_validation_regex.MatchString(p) && !path.IsAbs(p) {
possible = append(possible, path.Join(c.getRelativeExpansionIfLocal(p), c.BaseUrl, p))
if !isRelativePath(p) {
possible = append(possible, path.Join(c.expandRelativePath(p), c.BaseUrl, p))
}

BazelLog.Tracef("Found %d possible paths for %s: %v", len(possible), p, possible)

// Add 'rootDirs' as alternate directories for relative imports
// https://www.typescriptlang.org/tsconfig#rootDirs
for _, v := range c.VirtualRootDirs {
possible = append(possible, path.Join(v, p))
}

BazelLog.Tracef("Found %d possible paths for %s: %v", len(possible), p, possible)

return possible
}

Expand Down
32 changes: 13 additions & 19 deletions gazelle/js/typescript/tsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,33 @@ func assertExpand(t *testing.T, options *TsConfig, p string, expected ...string)
}
}

func TestBaseUrlRegex(t *testing.T) {
t.Parallel()

t.Run("valid strings", func(t *testing.T) {
t.Parallel()
func TestIsRelativePath(t *testing.T) {
t.Run("relative path strings", func(t *testing.T) {

shouldNotMatch := []string{
"example",
"valid",
"another",
"example/test",
"/absolute/path",
"another/not/relative/path",
".dotfile",
}

for _, s := range shouldNotMatch {
if baseurl_validation_regex.MatchString(s) {
t.Errorf("Expected no match for '%s', but it matched", s)
if isRelativePath(s) {
t.Errorf("isRelativePath(%s) should not be relative but was matched as it would", s)
}
}

})

t.Run("invalid strings", func(t *testing.T) {
t.Parallel()

t.Run("not relative path strings", func(t *testing.T) {
shouldMatch := []string{
"./path",
"../parent",
"@username",
".invalid",
}

for _, s := range shouldMatch {
if !baseurl_validation_regex.MatchString(s) {
t.Errorf("Expected a match for '%s', but it didn't match", s)
if !isRelativePath(s) {
t.Errorf("isRelativePath(%s) should be relative but was NOT matched as it would", s)
}
}
})
Expand Down Expand Up @@ -164,7 +158,7 @@ func TestTypescriptApi(t *testing.T) {
},
}

assertExpand(t, config, "@org/liba/test", "libs/ts/liba/src/test")
assertExpand(t, config, "@org/liba/test", "libs/ts/liba/src/test", "tsconfig_test/@org/liba/test")
})

t.Run("tsconfig paths expansion basic", func(t *testing.T) {
Expand All @@ -181,7 +175,7 @@ func TestTypescriptApi(t *testing.T) {
}
}`)

assertExpand(t, config, "@org/lib", "tsconfig_test/b/src/lib")
assertExpand(t, config, "@org/lib", "tsconfig_test/b/src/lib", "tsconfig_test/@org/lib")
})

t.Run("tsconfig paths expansion", func(t *testing.T) {
Expand Down

0 comments on commit d3abd3f

Please sign in to comment.