diff --git a/codegen/codegen.go b/codegen/codegen.go index 1b29e27..f658e8f 100644 --- a/codegen/codegen.go +++ b/codegen/codegen.go @@ -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 diff --git a/codegen/codegen_test.go b/codegen/codegen_test.go index 7519285..f0702c1 100644 --- a/codegen/codegen_test.go +++ b/codegen/codegen_test.go @@ -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",