Skip to content

Commit

Permalink
fix: package.json and tsconfig per file
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Dec 26, 2023
1 parent da67cea commit 40c672d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion internal/graph/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func hash(s string) int64 {
return int64(h.Sum32())
}

var hashCached = utils.Cached(hash)
var hashCached = utils.Cached1In1Out(hash)

func MakeNode[T any](id string, data T) *Node[T] {
return &Node[T]{
Expand Down
1 change: 1 addition & 0 deletions internal/js/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type ExportsCacheKey string

//nolint:gocyclo
func (l *Language) ParseExports(file *js_grammar.File) (*language.ExportsEntries, error) {
exports := make([]language.ExportEntry, 0)
var errors []error
Expand Down
32 changes: 9 additions & 23 deletions internal/js/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"path"
"path/filepath"

"github.com/gabotechs/dep-tree/internal/js/js_grammar"
"github.com/gabotechs/dep-tree/internal/language"
Expand All @@ -17,14 +16,16 @@ var Extensions = []string{

type Language struct {
PackageJsonPath string
ProjectRoot string
TsConfig TsConfig
Cfg *Config
}

var _ language.Language[js_grammar.File] = &Language{}

func findPackageJson(searchPath string) (TsConfig, string, error) {
// findPackageJson starts from a search path and goes up dir by dir
// until a package.json file is found. If one is found, it returns the
// dir where it was found and a parsed TsConfig object in case that there
// was also a tsconfig.json file.
func _findPackageJson(searchPath string) (TsConfig, string, error) {
if len(searchPath) < 2 {
return TsConfig{}, "", nil
}
Expand All @@ -41,33 +42,18 @@ func findPackageJson(searchPath string) (TsConfig, string, error) {
}
return tsConfig, searchPath, err
} else {
return findPackageJson(path.Dir(searchPath))
return _findPackageJson(path.Dir(searchPath))
}
}

var findPackageJson = utils.Cached1In2OutErr(_findPackageJson)

func MakeJsLanguage(ctx context.Context, entrypoint string, cfg *Config) (context.Context, language.Language[js_grammar.File], error) {
entrypointAbsPath, err := filepath.Abs(entrypoint)
if err != nil {
return ctx, nil, err
}
if !utils.FileExists(entrypoint) {
return ctx, nil, fmt.Errorf("file %s does not exist", entrypoint)
}

tsConfig, packageJsonPath, err := findPackageJson(entrypointAbsPath)
if err != nil {
return ctx, nil, err
}
projectRoot := path.Dir(entrypointAbsPath)
if packageJsonPath != "" {
projectRoot = packageJsonPath
}
return ctx, &Language{
PackageJsonPath: packageJsonPath,
ProjectRoot: projectRoot,
TsConfig: tsConfig,
Cfg: cfg,
}, nil
return ctx, &Language{Cfg: cfg}, nil
}

func (l *Language) ParseFile(id string) (*js_grammar.File, error) {
Expand Down
13 changes: 9 additions & 4 deletions internal/js/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
return absPath, nil
}

tsConfig, rootFolder, err := findPackageJson(dir)
if err != nil {
return "", err
}

// 2. If is imported from baseUrl.
baseUrl := l.TsConfig.CompilerOptions.BaseUrl
importFromBaseUrl := path.Join(l.ProjectRoot, baseUrl, unresolved)
baseUrl := tsConfig.CompilerOptions.BaseUrl
importFromBaseUrl := path.Join(rootFolder, baseUrl, unresolved)
absPath = getFileAbsPath(importFromBaseUrl)
if absPath != "" {
return absPath, nil
Expand All @@ -43,7 +48,7 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
if l.Cfg != nil && l.Cfg.FollowTsConfigPaths == false {
return absPath, nil
}
pathOverrides := l.TsConfig.CompilerOptions.Paths
pathOverrides := tsConfig.CompilerOptions.Paths
if pathOverrides == nil {
return absPath, nil
}
Expand All @@ -54,7 +59,7 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
for _, searchPath := range searchPaths {
searchPath = strings.ReplaceAll(searchPath, "*", "")
newImportFrom := strings.ReplaceAll(unresolved, pathOverride, searchPath)
importFromBaseUrlAndPaths := path.Join(l.ProjectRoot, baseUrl, newImportFrom)
importFromBaseUrlAndPaths := path.Join(rootFolder, baseUrl, newImportFrom)
absPath = getFileAbsPath(importFromBaseUrlAndPaths)
if absPath != "" {
return absPath, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func _pythonFilesInDir(dir string) []string {
return pythonFiles
}

var pythonFilesInDir = utils.Cached(_pythonFilesInDir)
var pythonFilesInDir = utils.Cached1In1Out(_pythonFilesInDir)

// resolveFromSlicesAndSearchPath returns multiple valid resolved paths.
func resolveFromSlicesAndSearchPath(searchPath string, slices []string) *ResolveResult {
Expand Down
22 changes: 21 additions & 1 deletion internal/utils/cached.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package utils

func Cached[I comparable, O any](f func(I) O) func(I) O {
func Cached1In1Out[I comparable, O any](f func(I) O) func(I) O {
cache := make(map[I]O)
return func(x I) O {
if _, ok := cache[x]; !ok {
Expand All @@ -10,3 +10,23 @@ func Cached[I comparable, O any](f func(I) O) func(I) O {
return value
}
}

type out2[O1 any, O2 any] struct {
o1 O1
o2 O2
}

func Cached1In2OutErr[I comparable, O1 any, O2 any](f func(I) (O1, O2, error)) func(I) (O1, O2, error) {
cache := make(map[I]out2[O1, O2])
return func(x I) (O1, O2, error) {
if _, ok := cache[x]; !ok {
o1, o2, err := f(x)
if err != nil {
return o1, o2, err
}
cache[x] = out2[O1, O2]{o1, o2}
}
value, _ := cache[x]
return value.o1, value.o2, nil
}
}
4 changes: 2 additions & 2 deletions internal/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func _FileExists(path string) bool {
return !stat.IsDir()
}

var FileExists = Cached(_FileExists)
var FileExists = Cached1In1Out(_FileExists)

func _DirExists(path string) bool {
stat, err := os.Stat(path)
Expand All @@ -20,4 +20,4 @@ func _DirExists(path string) bool {
return stat.IsDir()
}

var DirExists = Cached(_DirExists)
var DirExists = Cached1In1Out(_DirExists)

0 comments on commit 40c672d

Please sign in to comment.