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
5 changes: 5 additions & 0 deletions cmd/internal/preview/codegen/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import (
"fmt"
"log/slog"
"path/filepath"
"regexp"
"strings"

"github.com/k-kohey/axe/internal/preview/buildlock"
)

var nonIdentifierRe = regexp.MustCompile(`[^A-Za-z0-9_]`)

// ReplacementModuleName generates the module name for a thunk dylib, matching
// Xcode's convention: {Module}_PreviewReplacement_{FileName}_{N}.
func ReplacementModuleName(moduleName, sourceFileName string, counter int) string {
base := strings.TrimSuffix(filepath.Base(sourceFileName), filepath.Ext(sourceFileName))
// "Foo+Bar.swift" or "FooView.ui.swift" would otherwise yield an invalid module identifier.
base = nonIdentifierRe.ReplaceAllString(base, "_")
return fmt.Sprintf("%s_PreviewReplacement_%s_%d", moduleName, base, counter)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/internal/preview/codegen/thunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func TestReplacementModuleName(t *testing.T) {
{"MyApp", "HomeView.swift", 3, "MyApp_PreviewReplacement_HomeView_3"},
// Should use only the base name, not subdirectory path
{"App", "Views/HogeView.swift", 1, "App_PreviewReplacement_HogeView_1"},
// Non-identifier characters in the file name are replaced
{"MyFeature", "FooView.ui.swift", 0, "MyFeature_PreviewReplacement_FooView_ui_0"},
{"App", "Foo+Bar.swift", 2, "App_PreviewReplacement_Foo_Bar_2"},
}
for _, tt := range tests {
got := ReplacementModuleName(tt.moduleName, tt.sourceFileName, tt.counter)
Expand Down
Loading