From 7d8c7a4cd9c35e48107f78a08927eda2d01c7833 Mon Sep 17 00:00:00 2001 From: tsingbx Date: Tue, 29 Oct 2024 15:44:41 +0800 Subject: [PATCH 1/2] fix index out of bound when write comment --- internal/go/printer/printer.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/go/printer/printer.go b/internal/go/printer/printer.go index a2b9582..20e41e0 100644 --- a/internal/go/printer/printer.go +++ b/internal/go/printer/printer.go @@ -359,7 +359,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment return } - if pos.Line == p.last.Line && (prev == nil || prev.Text[1] != '/') { + if pos.Line == p.last.Line && (prev == nil || (len(prev.Text) > 1 && prev.Text[1] != '/')) { // comment on the same line as last item: // separate with at least one separator hasSep := false @@ -457,7 +457,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment // make sure there is at least one line break // if the previous comment was a line comment - if n == 0 && prev != nil && prev.Text[1] == '/' { + if n == 0 && prev != nil && len(prev.Text) > 1 && prev.Text[1] == '/' { n = 1 } @@ -567,7 +567,7 @@ func stripCommonPrefix(lines []string) { // for the opening /*, assume up to 3 blanks or a tab. This // whitespace may be found as suffix in the common prefix. first := lines[0] - if isBlank(first[2:]) { + if len(first) > 2 && isBlank(first[2:]) { // no comment text on the first line: // reduce prefix by up to 3 blanks or a tab // if present - this keeps comment text indented @@ -594,8 +594,12 @@ func stripCommonPrefix(lines []string) { suffix = suffix[2:n] } else { // otherwise assume two blanks - suffix[0], suffix[1] = ' ', ' ' - suffix = suffix[0:n] + if len(suffix) > 1 { + suffix[0], suffix[1] = ' ', ' ' + } + if len(suffix) > n { + suffix = suffix[0:n] + } } // Shorten the computed common prefix by the length of // suffix, if it is found as suffix of the prefix. @@ -643,7 +647,7 @@ func (p *printer) writeComment(comment *ast.Comment) { } // shortcut common case of //-style comments - if text[1] == '/' { + if len(text) > 1 && text[1] == '/' { p.writeString(pos, trimRight(text), true) return } @@ -756,7 +760,7 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro // to track whether we're inside an expression or statement and // use that information to decide more directly. needsLinebreak := false - if p.mode&noExtraBlank == 0 && + if p.mode&noExtraBlank == 0 && len(last.Text) > 1 && last.Text[1] == '*' { if line := p.lineFor(last.Pos()); (line == 0 || line == next.Line) && tok != token.COMMA && @@ -771,7 +775,7 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro } // Ensure that there is a line break after a //-style comment, // before EOF, and before a closing '}' unless explicitly disabled. - if last.Text[1] == '/' || + if len(last.Text) > 1 && last.Text[1] == '/' || tok == token.EOF || tok == token.RBRACE && p.mode&noExtraLinebreak == 0 { needsLinebreak = true From 4e18907914820f2c281d4a42e30fbb7baefaaa41 Mon Sep 17 00:00:00 2001 From: tsingbx Date: Tue, 29 Oct 2024 16:40:50 +0800 Subject: [PATCH 2/2] fix lint error --- internal/go/printer/printer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/go/printer/printer.go b/internal/go/printer/printer.go index 20e41e0..43a0119 100644 --- a/internal/go/printer/printer.go +++ b/internal/go/printer/printer.go @@ -359,7 +359,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment return } - if pos.Line == p.last.Line && (prev == nil || (len(prev.Text) > 1 && prev.Text[1] != '/')) { + if pos.Line == p.last.Line && (prev == nil || len(prev.Text) > 1 && prev.Text[1] != '/') { // comment on the same line as last item: // separate with at least one separator hasSep := false