From 94089f39fb15976b0c8dbe2178bed1aa52e5d753 Mon Sep 17 00:00:00 2001 From: Jvle Date: Fri, 17 Jul 2026 12:52:44 +0800 Subject: [PATCH] fix: normalize license OR parentheses Signed-off-by: Jvle --- pkgsite.go | 12 ++++++++---- pkgsite_test.go | 10 +++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pkgsite.go b/pkgsite.go index 9462759..a21020f 100644 --- a/pkgsite.go +++ b/pkgsite.go @@ -358,10 +358,7 @@ func pkgsiteLicenseExpression(licenses []pkgsiteLicense) string { types = append(types, typ) } sort.Strings(types) - group := types[0] - if len(types) > 1 { - group = "(" + strings.Join(types, " OR ") + ")" - } + group := strings.Join(types, " OR ") seenGroups[group] = true } if len(seenGroups) == 0 { @@ -373,5 +370,12 @@ func pkgsiteLicenseExpression(licenses []pkgsiteLicense) string { groups = append(groups, group) } sort.Strings(groups) + if len(groups) > 1 { + for i, group := range groups { + if strings.Contains(group, " OR ") { + groups[i] = "(" + group + ")" + } + } + } return strings.Join(groups, " AND ") } diff --git a/pkgsite_test.go b/pkgsite_test.go index 0a75e57..2175d63 100644 --- a/pkgsite_test.go +++ b/pkgsite_test.go @@ -48,7 +48,15 @@ func TestPkgsiteLicenseExpression(t *testing.T) { licenses: []pkgsiteLicense{ {FilePath: "LICENSE", Types: []string{"MIT", "Apache-2.0"}}, }, - want: "(Apache-2.0 OR MIT)", + want: "Apache-2.0 OR MIT", + }, + { + name: "parenthesizes OR group when joined with AND", + licenses: []pkgsiteLicense{ + {FilePath: "LICENSE", Types: []string{"MIT", "Apache-2.0"}}, + {FilePath: "COPYING", Types: []string{"BSD-3-Clause"}}, + }, + want: "(Apache-2.0 OR MIT) AND BSD-3-Clause", }, { name: "uses TODO when pkgsite has no SPDX type", -- 2.52.0