feat(codegen): deduplicate repeated tags
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 19:09:00 +00:00
parent 0716fe991f
commit 113dab6635
2 changed files with 35 additions and 0 deletions

View file

@ -215,12 +215,17 @@ func validateSlotKeys(slots map[string]string) error {
func orderedSlotEntries(slots map[string]string) []slotEntry {
entries := make([]slotEntry, 0, len(slots))
seenTags := make(map[string]struct{}, len(slots))
for _, slot := range canonicalSlotOrder {
tag, ok := slots[slot]
if !ok {
continue
}
if _, seen := seenTags[tag]; seen {
continue
}
seenTags[tag] = struct{}{}
entries = append(entries, slotEntry{Slot: slot, Tag: tag})
}
return entries

View file

@ -67,6 +67,21 @@ func TestGenerateBundle_Good(t *testing.T) {
assert.True(t, h < c && c < f, "expected canonical HLCRF order in generated bundle")
}
func TestGenerateBundle_DeduplicatesTags(t *testing.T) {
slots := map[string]string{
"H": "nav-bar",
"C": "nav-bar",
"F": "page-footer",
}
js, err := GenerateBundle(slots)
require.NoError(t, err)
assert.Equal(t, 2, strings.Count(js, "extends HTMLElement"))
assert.Equal(t, 1, strings.Count(js, "class NavBar extends HTMLElement"))
assert.Equal(t, 1, strings.Count(js, "class PageFooter extends HTMLElement"))
}
func TestGenerateBundle_Bad_InvalidSlotKey(t *testing.T) {
slots := map[string]string{
"H": "nav-bar",
@ -98,6 +113,21 @@ func TestGenerateTypeDefinitions_Good(t *testing.T) {
assert.Contains(t, dts, "export {};")
}
func TestGenerateTypeDefinitions_DeduplicatesTags(t *testing.T) {
slots := map[string]string{
"H": "nav-bar",
"C": "nav-bar",
"F": "page-footer",
}
dts, err := GenerateTypeDefinitions(slots)
require.NoError(t, err)
assert.Equal(t, 2, strings.Count(dts, "extends HTMLElement"))
assert.Equal(t, 1, strings.Count(dts, `"nav-bar": NavBar;`))
assert.Equal(t, 1, strings.Count(dts, `"page-footer": PageFooter;`))
}
func TestGenerateTypeDefinitions_SkipsInvalidTags(t *testing.T) {
slots := map[string]string{
"H": "nav-bar",