Skip to content

Commit a16401c

Browse files
committed
Consolidate utils
1 parent f6405d9 commit a16401c

3 files changed

Lines changed: 38 additions & 44 deletions

File tree

internal/cadence/linter.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package cadence
2121
import (
2222
"errors"
2323
"fmt"
24-
"path/filepath"
2524
"strings"
2625

2726
"github.com/onflow/flow-cli/internal/util"
@@ -197,8 +196,8 @@ func (l *linter) handleImport(
197196
}, nil
198197
default:
199198
// Normalize relative path imports to absolute paths
200-
if l.isPathLocation(importedLocation) {
201-
importedLocation = l.normalizePathLocation(checker.Location, importedLocation)
199+
if util.IsPathLocation(importedLocation) {
200+
importedLocation = util.NormalizePathLocation(checker.Location, importedLocation)
202201
}
203202

204203
filepath, err := l.resolveImportFilepath(importedLocation, checker.Location)
@@ -244,37 +243,6 @@ func (l *linter) handleImport(
244243
}
245244
}
246245

247-
// isPathLocation returns true if the location is a file path (contains .cdc)
248-
func (l *linter) isPathLocation(location common.Location) bool {
249-
stringLocation, ok := location.(common.StringLocation)
250-
if !ok {
251-
return false
252-
}
253-
return strings.Contains(stringLocation.String(), ".cdc")
254-
}
255-
256-
// normalizePathLocation normalizes a relative path import against a base location
257-
func (l *linter) normalizePathLocation(base, relative common.Location) common.Location {
258-
baseString, baseOk := base.(common.StringLocation)
259-
relativeString, relativeOk := relative.(common.StringLocation)
260-
261-
if !baseOk || !relativeOk {
262-
return relative
263-
}
264-
265-
basePath := baseString.String()
266-
relativePath := relativeString.String()
267-
268-
// If the relative path is absolute, return it as-is
269-
if filepath.IsAbs(relativePath) {
270-
return relative
271-
}
272-
273-
// Join relative to the parent directory of the base
274-
normalizedPath := filepath.Join(filepath.Dir(basePath), relativePath)
275-
return common.StringLocation(normalizedPath)
276-
}
277-
278246
func (l *linter) resolveImportFilepath(
279247
location common.Location,
280248
parentLocation common.Location,

internal/test/test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func importResolver(scriptPath string, state *flowkit.State) cdcTests.ImportReso
453453
relativePath := location.String()
454454

455455
if strings.Contains(relativePath, helperScriptSubstr) {
456-
importedScriptFilePath := absolutePath(scriptPath, relativePath)
456+
importedScriptFilePath := util.AbsolutePath(scriptPath, relativePath)
457457
scriptCode, err := state.ReadFile(importedScriptFilePath)
458458
if err != nil {
459459
return "", nil
@@ -482,7 +482,7 @@ func importResolver(scriptPath string, state *flowkit.State) cdcTests.ImportReso
482482

483483
func fileResolver(scriptPath string, state *flowkit.State) cdcTests.FileResolver {
484484
return func(path string) (string, error) {
485-
importFilePath := absolutePath(scriptPath, path)
485+
importFilePath := util.AbsolutePath(scriptPath, path)
486486

487487
content, err := state.ReadFile(importFilePath)
488488
if err != nil {
@@ -493,14 +493,6 @@ func fileResolver(scriptPath string, state *flowkit.State) cdcTests.FileResolver
493493
}
494494
}
495495

496-
func absolutePath(basePath, filePath string) string {
497-
if filepath.IsAbs(filePath) {
498-
return filePath
499-
}
500-
501-
return filepath.Join(filepath.Dir(basePath), filePath)
502-
}
503-
504496
type result struct {
505497
Results map[string]cdcTests.Results
506498
CoverageReport *runtime.CoverageReport

internal/util/files.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"fmt"
2323
"path/filepath"
2424
"strings"
25+
26+
"github.com/onflow/cadence/common"
2527
)
2628

2729
func AddCDCExtension(name string) string {
@@ -34,3 +36,35 @@ func AddCDCExtension(name string) string {
3436
func StripCDCExtension(name string) string {
3537
return strings.TrimSuffix(name, filepath.Ext(name))
3638
}
39+
40+
// AbsolutePath resolves a relative path against a base file path.
41+
// If the relative path is already absolute, it returns it as-is.
42+
// Otherwise, it joins the relative path to the parent directory of the base path.
43+
func AbsolutePath(basePath, relativePath string) string {
44+
if filepath.IsAbs(relativePath) {
45+
return relativePath
46+
}
47+
return filepath.Join(filepath.Dir(basePath), relativePath)
48+
}
49+
50+
// IsPathLocation returns true if the location is a file path (contains .cdc)
51+
func IsPathLocation(location common.Location) bool {
52+
stringLocation, ok := location.(common.StringLocation)
53+
if !ok {
54+
return false
55+
}
56+
return strings.Contains(stringLocation.String(), ".cdc")
57+
}
58+
59+
// NormalizePathLocation normalizes a relative path import against a base location
60+
func NormalizePathLocation(base, relative common.Location) common.Location {
61+
baseString, baseOk := base.(common.StringLocation)
62+
relativeString, relativeOk := relative.(common.StringLocation)
63+
64+
if !baseOk || !relativeOk {
65+
return relative
66+
}
67+
68+
normalizedPath := AbsolutePath(baseString.String(), relativeString.String())
69+
return common.StringLocation(normalizedPath)
70+
}

0 commit comments

Comments
 (0)