Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Automatically enforce satisfies for getStaticPaths #874

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
34 changes: 34 additions & 0 deletions internal/printer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package printer
import (
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -46,6 +47,39 @@ func getTSXComponentName(filename string) string {
}
}

func trimExt(filename string) string {
return strings.TrimSuffix(filename, filepath.Ext(filename))
}

func getParamsTypeFromFilename(filename string) string {
defaultType := "Record<string, string | number>"
if filename == "<stdin>" {
return defaultType
}
if len(filename) == 0 {
return defaultType
}
parts := strings.Split(filename, "/")
params := make([]string, 0)
r, err := regexp.Compile(`\[(?:\.{3})?([^]]+)\]`)
if err != nil {
return defaultType
}
for _, part := range parts {
if !strings.ContainsAny(part, "[]") {
continue
}
part = trimExt(part)
for _, match := range r.FindAllStringSubmatch(part, -1) {
params = append(params, fmt.Sprintf(`"%s"`, match[1]))
}
}
if len(params) == 0 {
return defaultType
}
return fmt.Sprintf("Record<%s, string | number>", strings.Join(params, " | "))
}

func getComponentName(filename string) string {
if len(filename) == 0 {
return "$$Component"
Expand Down
52 changes: 52 additions & 0 deletions internal/printer/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package printer

import (
"strings"
"testing"

"github.com/withastro/compiler/internal/test_utils"
)

type paramsTestcase struct {
name string
want string
}

func TestUtilParamsType(t *testing.T) {
tests := []paramsTestcase{
{
name: "/src/pages/index.astro",
want: `Record<string, string | number>`,
},
{
name: "/src/pages/blog/[slug].astro",
want: `Record<"slug", string | number>`,
},
{
name: "/src/pages/[lang]/blog/[slug].astro",
want: `Record<"lang" | "slug", string | number>`,
},
{
name: "/src/pages/[...fallback].astro",
want: `Record<"fallback", string | number>`,
},
{
name: "/src/pages/[year]-[month]-[day]/[post].astro",
want: `Record<"year" | "month" | "day" | "post", string | number>`,
},
{
name: "/src/pages/post-[id]/[post].astro",
want: `Record<"id" | "post", string | number>`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := getParamsTypeFromFilename(tt.name)
// compare to expected string, show diff if mismatch
if diff := test_utils.ANSIDiff(strings.TrimSpace(tt.want), strings.TrimSpace(string(result))); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
Loading