Skip to content

Commit 5a5a749

Browse files
authored
Merge pull request #442 from tsingbx/printer
fix index out of bound when write comment
2 parents 47c09e3 + 4e18907 commit 5a5a749

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

internal/go/printer/printer.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment
359359
return
360360
}
361361

362-
if pos.Line == p.last.Line && (prev == nil || prev.Text[1] != '/') {
362+
if pos.Line == p.last.Line && (prev == nil || len(prev.Text) > 1 && prev.Text[1] != '/') {
363363
// comment on the same line as last item:
364364
// separate with at least one separator
365365
hasSep := false
@@ -457,7 +457,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment
457457

458458
// make sure there is at least one line break
459459
// if the previous comment was a line comment
460-
if n == 0 && prev != nil && prev.Text[1] == '/' {
460+
if n == 0 && prev != nil && len(prev.Text) > 1 && prev.Text[1] == '/' {
461461
n = 1
462462
}
463463

@@ -567,7 +567,7 @@ func stripCommonPrefix(lines []string) {
567567
// for the opening /*, assume up to 3 blanks or a tab. This
568568
// whitespace may be found as suffix in the common prefix.
569569
first := lines[0]
570-
if isBlank(first[2:]) {
570+
if len(first) > 2 && isBlank(first[2:]) {
571571
// no comment text on the first line:
572572
// reduce prefix by up to 3 blanks or a tab
573573
// if present - this keeps comment text indented
@@ -594,8 +594,12 @@ func stripCommonPrefix(lines []string) {
594594
suffix = suffix[2:n]
595595
} else {
596596
// otherwise assume two blanks
597-
suffix[0], suffix[1] = ' ', ' '
598-
suffix = suffix[0:n]
597+
if len(suffix) > 1 {
598+
suffix[0], suffix[1] = ' ', ' '
599+
}
600+
if len(suffix) > n {
601+
suffix = suffix[0:n]
602+
}
599603
}
600604
// Shorten the computed common prefix by the length of
601605
// suffix, if it is found as suffix of the prefix.
@@ -643,7 +647,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
643647
}
644648

645649
// shortcut common case of //-style comments
646-
if text[1] == '/' {
650+
if len(text) > 1 && text[1] == '/' {
647651
p.writeString(pos, trimRight(text), true)
648652
return
649653
}
@@ -756,7 +760,7 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
756760
// to track whether we're inside an expression or statement and
757761
// use that information to decide more directly.
758762
needsLinebreak := false
759-
if p.mode&noExtraBlank == 0 &&
763+
if p.mode&noExtraBlank == 0 && len(last.Text) > 1 &&
760764
last.Text[1] == '*' {
761765
if line := p.lineFor(last.Pos()); (line == 0 || line == next.Line) &&
762766
tok != token.COMMA &&
@@ -771,7 +775,7 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
771775
}
772776
// Ensure that there is a line break after a //-style comment,
773777
// before EOF, and before a closing '}' unless explicitly disabled.
774-
if last.Text[1] == '/' ||
778+
if len(last.Text) > 1 && last.Text[1] == '/' ||
775779
tok == token.EOF ||
776780
tok == token.RBRACE && p.mode&noExtraLinebreak == 0 {
777781
needsLinebreak = true

0 commit comments

Comments
 (0)