Skip to content

Commit 33f56ac

Browse files
committed
wip: add util to infer expected params from filename
1 parent 09abfe4 commit 33f56ac

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

internal/printer/utils.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package printer
33
import (
44
"errors"
55
"fmt"
6+
"path/filepath"
67
"regexp"
78
"strings"
89

@@ -46,6 +47,39 @@ func getTSXComponentName(filename string) string {
4647
}
4748
}
4849

50+
func trimExt(filename string) string {
51+
return strings.TrimSuffix(filename, filepath.Ext(filename))
52+
}
53+
54+
func getParamsTypeFromFilename(filename string) string {
55+
defaultType := "Record<string, string | number>"
56+
if filename == "<stdin>" {
57+
return defaultType
58+
}
59+
if len(filename) == 0 {
60+
return defaultType
61+
}
62+
parts := strings.Split(filename, "/")
63+
params := make([]string, 0)
64+
r, err := regexp.Compile(`\[(?:\.{3})?([^]]+)\]`)
65+
if err != nil {
66+
return defaultType
67+
}
68+
for _, part := range parts {
69+
if !strings.ContainsAny(part, "[]") {
70+
continue
71+
}
72+
part = trimExt(part)
73+
for _, match := range r.FindAllStringSubmatch(part, -1) {
74+
params = append(params, fmt.Sprintf(`"%s"`, match[1]))
75+
}
76+
}
77+
if len(params) == 0 {
78+
return defaultType
79+
}
80+
return fmt.Sprintf("Record<%s, string | number>", strings.Join(params, " | "))
81+
}
82+
4983
func getComponentName(filename string) string {
5084
if len(filename) == 0 {
5185
return "$$Component"

internal/printer/utils_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package printer
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/withastro/compiler/internal/test_utils"
8+
)
9+
10+
type paramsTestcase struct {
11+
name string
12+
want string
13+
}
14+
15+
func TestUtilParamsType(t *testing.T) {
16+
tests := []paramsTestcase{
17+
{
18+
name: "/src/pages/index.astro",
19+
want: `Record<string, string | number>`,
20+
},
21+
{
22+
name: "/src/pages/blog/[slug].astro",
23+
want: `Record<"slug", string | number>`,
24+
},
25+
{
26+
name: "/src/pages/[lang]/blog/[slug].astro",
27+
want: `Record<"lang" | "slug", string | number>`,
28+
},
29+
{
30+
name: "/src/pages/[...fallback].astro",
31+
want: `Record<"fallback", string | number>`,
32+
},
33+
{
34+
name: "/src/pages/[year]-[month]-[day]/[post].astro",
35+
want: `Record<"year" | "month" | "day" | "post", string | number>`,
36+
},
37+
{
38+
name: "/src/pages/post-[id]/[post].astro",
39+
want: `Record<"id" | "post", string | number>`,
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
result := getParamsTypeFromFilename(tt.name)
46+
// compare to expected string, show diff if mismatch
47+
if diff := test_utils.ANSIDiff(strings.TrimSpace(tt.want), strings.TrimSpace(string(result))); diff != "" {
48+
t.Errorf("mismatch (-want +got):\n%s", diff)
49+
}
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)