Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/references/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var codeExts = map[string]bool{
}

var quotedSpecRe = regexp.MustCompile(`(?i)['"\x60]([^'"\x60]*(?:\$\{[^'"\x60]*\}|[*{}])?[^'"\x60]*\.(?:avif|gif|heic|heif|jpe?g|png|svg|webp)(?:\?[^'"\x60]*)?)['"\x60]`)
var templateExprRe = regexp.MustCompile(`\$\{[^}]*\}`)
var cssSpecRe = regexp.MustCompile(`(?i)url\(\s*['"]?([^'")\s]+\.(?:avif|gif|heic|heif|jpe?g|png|svg|webp)(?:\?[^'")\s]*)?)['"]?\s*\)`)

func BuildMap(ctx context.Context, projects []Project, assets []Asset) (map[string][]Reference, error) {
Expand Down Expand Up @@ -348,6 +349,7 @@ func resolvePattern(importerRepoPath, specifier string, aliases map[string]strin
if spec == "" {
return ""
}
spec = templateExprRe.ReplaceAllString(spec, "*")
if resolved := resolveAlias(spec, aliases); resolved != "" {
return resolved
}
Expand Down
27 changes: 27 additions & 0 deletions internal/references/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,33 @@ func TestReferenceHelperFunctions(t *testing.T) {
}
}

func TestBuildMapResolvesTemplateLiteralPattern(t *testing.T) {
root := t.TempDir()
mustWrite(t, filepath.Join(root, "apps", "myapp", "src", "views", "Dashboard", "icons", "apple.webp"), "image")
mustWrite(t, filepath.Join(root, "apps", "myapp", "src", "views", "Dashboard", "icons", "banana.webp"), "image")
mustWrite(t, filepath.Join(root, "apps", "myapp", "src", "views", "Dashboard", "CategoryIcon.tsx"),
"function getIcon(code: string) {\n return new URL(`./icons/${code}.webp`, import.meta.url).href\n}")

refs, err := BuildMap(context.Background(),
[]Project{{ID: "p", Path: root}},
[]Asset{
{ProjectID: "p", RepoPath: "apps/myapp/src/views/Dashboard/icons/apple.webp"},
{ProjectID: "p", RepoPath: "apps/myapp/src/views/Dashboard/icons/banana.webp"},
},
)
if err != nil {
t.Fatal(err)
}
a := refs["p\x00apps/myapp/src/views/Dashboard/icons/apple.webp"]
if len(a) != 1 || a[0].File != "apps/myapp/src/views/Dashboard/CategoryIcon.tsx" {
t.Fatalf("template literal apple refs = %#v, want 1 ref from CategoryIcon.tsx", a)
}
b := refs["p\x00apps/myapp/src/views/Dashboard/icons/banana.webp"]
if len(b) != 1 || b[0].File != "apps/myapp/src/views/Dashboard/CategoryIcon.tsx" {
t.Fatalf("template literal banana refs = %#v, want 1 ref from CategoryIcon.tsx", b)
}
}

func TestBuildMapHonorsContextCancellation(t *testing.T) {
root := t.TempDir()
mustWrite(t, filepath.Join(root, "src", "App.tsx"), `import logo from "./logo.png"`)
Expand Down
Loading