modfile: do not collapse if there are unattached comments within blocks
The existing implementation drops unattached comments
when collapsing block into single line.
Fixes #33280
Change-Id: I388e1468657d461cba771e90d5e328746d927ae2
Reviewed-on: https://go-review.googlesource.com/c/mod/+/541815
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/modfile/read.go b/modfile/read.go
index 5b5bb5e..2205682 100644
--- a/modfile/read.go
+++ b/modfile/read.go
@@ -225,7 +225,7 @@
if ww == 0 {
continue
}
- if ww == 1 {
+ if ww == 1 && len(stmt.RParen.Comments.Before) == 0 {
// Collapse block into single line.
line := &Line{
Comments: Comments{
diff --git a/modfile/rule_test.go b/modfile/rule_test.go
index 96e0bfe..ca11d17 100644
--- a/modfile/rule_test.go
+++ b/modfile/rule_test.go
@@ -90,6 +90,24 @@
)
`,
},
+ {
+ `unattached_comments`,
+ `
+ module m
+ require (
+ foo v0.0.0-00010101000000-000000000000
+ // bar v0.0.0-00010101000000-000000000000
+ )
+ `,
+ "foo", "v0.0.0-00010101000000-000000000000",
+ `
+ module m
+ require (
+ foo v0.0.0-00010101000000-000000000000
+ // bar v0.0.0-00010101000000-000000000000
+ )
+ `,
+ },
}
type require struct {