Skip to content

Commit 89ce4c7

Browse files
author
Jay Conrod
committed
modfile: remove trailing newline from comment tokens
In v0.2.0, the go.mod lexer removed trailing LF bytes from comment tokens. This regressed in v0.3.0. Documentation on Comment.Token says the trailing newline should not be included. This CL fixes the lexer to strip trailing newlines again. It will now strip both LF and CRLF newlines. It also includes a test to ensure comments are attached at the right place in the syntax tree with the right content. Fixes golang/go#39913 Change-Id: I7fba0ed3c85f0a3c23fefc6b7fecfe6df7777aea Reviewed-on: https://go-review.googlesource.com/c/mod/+/240557 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 0b26df4 commit 89ce4c7

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

modfile/read.go

+8
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,17 @@ func (in *input) startToken() {
477477

478478
// endToken marks the end of an input token.
479479
// It records the actual token string in tok.text.
480+
// A single trailing newline (LF or CRLF) will be removed from comment tokens.
480481
func (in *input) endToken(kind tokenKind) {
481482
in.token.kind = kind
482483
text := string(in.tokenStart[:len(in.tokenStart)-len(in.remaining)])
484+
if kind.isComment() {
485+
if strings.HasSuffix(text, "\r\n") {
486+
text = text[:len(text)-2]
487+
} else {
488+
text = strings.TrimSuffix(text, "\n")
489+
}
490+
}
483491
in.token.text = text
484492
in.token.endPos = in.pos
485493
}

modfile/read_test.go

+117
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,120 @@ func TestGoVersion(t *testing.T) {
445445
})
446446
}
447447
}
448+
449+
func TestComments(t *testing.T) {
450+
for _, test := range []struct {
451+
desc, input, want string
452+
}{
453+
{
454+
desc: "comment_only",
455+
input: `
456+
// a
457+
// b
458+
`,
459+
want: `
460+
comments before "// a"
461+
comments before "// b"
462+
`,
463+
}, {
464+
desc: "line",
465+
input: `
466+
// a
467+
468+
// b
469+
module m // c
470+
// d
471+
472+
// e
473+
`,
474+
want: `
475+
comments before "// a"
476+
line before "// b"
477+
line suffix "// c"
478+
comments before "// d"
479+
comments before "// e"
480+
`,
481+
}, {
482+
desc: "block",
483+
input: `
484+
// a
485+
486+
// b
487+
block ( // c
488+
// d
489+
490+
// e
491+
x // f
492+
// g
493+
494+
// h
495+
) // i
496+
// j
497+
498+
// k
499+
`,
500+
want: `
501+
comments before "// a"
502+
block before "// b"
503+
lparen suffix "// c"
504+
blockline before "// d"
505+
blockline before ""
506+
blockline before "// e"
507+
blockline suffix "// f"
508+
rparen before "// g"
509+
rparen before ""
510+
rparen before "// h"
511+
rparen suffix "// i"
512+
comments before "// j"
513+
comments before "// k"
514+
`,
515+
}, {
516+
desc: "cr_removed",
517+
input: "// a\r\r\n",
518+
want: `comments before "// a\r"`,
519+
},
520+
} {
521+
t.Run(test.desc, func(t *testing.T) {
522+
f, err := ParseLax("go.mod", []byte(test.input), nil)
523+
if err != nil {
524+
t.Fatal(err)
525+
}
526+
527+
buf := &bytes.Buffer{}
528+
printComments := func(prefix string, cs *Comments) {
529+
for _, c := range cs.Before {
530+
fmt.Fprintf(buf, "%s before %q\n", prefix, c.Token)
531+
}
532+
for _, c := range cs.Suffix {
533+
fmt.Fprintf(buf, "%s suffix %q\n", prefix, c.Token)
534+
}
535+
for _, c := range cs.After {
536+
fmt.Fprintf(buf, "%s after %q\n", prefix, c.Token)
537+
}
538+
}
539+
540+
printComments("file", &f.Syntax.Comments)
541+
for _, stmt := range f.Syntax.Stmt {
542+
switch stmt := stmt.(type) {
543+
case *CommentBlock:
544+
printComments("comments", stmt.Comment())
545+
case *Line:
546+
printComments("line", stmt.Comment())
547+
case *LineBlock:
548+
printComments("block", stmt.Comment())
549+
printComments("lparen", stmt.LParen.Comment())
550+
for _, line := range stmt.Line {
551+
printComments("blockline", line.Comment())
552+
}
553+
printComments("rparen", stmt.RParen.Comment())
554+
}
555+
}
556+
557+
got := strings.TrimSpace(buf.String())
558+
want := strings.TrimSpace(test.want)
559+
if got != want {
560+
t.Errorf("got:\n%s\nwant:\n%s", got, want)
561+
}
562+
})
563+
}
564+
}

0 commit comments

Comments
 (0)