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 10, 2023
1 parent bb0e82d commit 9f8aa4e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
14 changes: 7 additions & 7 deletions gazelle/js/typescript/tsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,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 +159,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 {
// 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 +185,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.getRelativeExpansionIfLocal(m), c.Paths.Rel, m))
}

// Check for pattern matches next
Expand Down Expand Up @@ -225,14 +225,14 @@ func (c TsConfig) ExpandPaths(from, p string) []string {
possible = append(possible, path.Join(c.getRelativeExpansionIfLocal(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
5 changes: 0 additions & 5 deletions gazelle/js/typescript/tsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ 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()

shouldNotMatch := []string{
"example",
Expand All @@ -68,8 +65,6 @@ func TestBaseUrlRegex(t *testing.T) {
})

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

shouldMatch := []string{
"./path",
"../parent",
Expand Down

0 comments on commit 9f8aa4e

Please sign in to comment.