Skip to content

Commit

Permalink
change LegalCommentsBeforeToken into ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 4, 2023
1 parent 56f4ef9 commit c169006
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 76 deletions.
3 changes: 1 addition & 2 deletions internal/css_lexer/css_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"
"unicode/utf8"

"github.com/evanw/esbuild/internal/helpers"
"github.com/evanw/esbuild/internal/logger"
)

Expand Down Expand Up @@ -505,7 +504,7 @@ func (lexer *lexer) consumeToEndOfMultiLineComment(startRange logger.Range) {

// Record legal comments
if text := lexer.source.Contents[startRange.Loc.Start:commentEnd]; isLegalComment || containsAtPreserveOrAtLicense(text) {
text = helpers.RemoveMultiLineCommentIndent(lexer.source.Contents[:startRange.Loc.Start], text)
text = lexer.source.CommentTextWithoutIndent(logger.Range{Loc: startRange.Loc, Len: int32(commentEnd) - startRange.Loc.Start})
lexer.legalCommentsBefore = append(lexer.legalCommentsBefore, Comment{Loc: startRange.Loc, Text: text})
}
return
Expand Down
63 changes: 0 additions & 63 deletions internal/helpers/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,8 @@ package helpers

import (
"strings"
"unicode/utf8"
)

func RemoveMultiLineCommentIndent(prefix string, text string) string {
// Figure out the initial indent
indent := 0
seekBackwardToNewline:
for len(prefix) > 0 {
c, size := utf8.DecodeLastRuneInString(prefix)
switch c {
case '\r', '\n', '\u2028', '\u2029':
break seekBackwardToNewline
}
prefix = prefix[:len(prefix)-size]
indent++
}

// Split the comment into lines
var lines []string
start := 0
for i, c := range text {
switch c {
case '\r', '\n':
// Don't double-append for Windows style "\r\n" newlines
if start <= i {
lines = append(lines, text[start:i])
}

start = i + 1

// Ignore the second part of Windows style "\r\n" newlines
if c == '\r' && start < len(text) && text[start] == '\n' {
start++
}

case '\u2028', '\u2029':
lines = append(lines, text[start:i])
start = i + 3
}
}
lines = append(lines, text[start:])

// Find the minimum indent over all lines after the first line
for _, line := range lines[1:] {
lineIndent := 0
for _, c := range line {
if c != ' ' && c != '\t' {
break
}
lineIndent++
}
if indent > lineIndent {
indent = lineIndent
}
}

// Trim the indent off of all lines after the first line
for i, line := range lines {
if i > 0 {
lines[i] = line[indent:]
}
}
return strings.Join(lines, "\n")
}

func EscapeClosingTag(text string, slashTag string) string {
if slashTag == "" {
return text
Expand Down
13 changes: 3 additions & 10 deletions internal/js_lexer/js_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ type MaybeSubstring struct {
}

type Lexer struct {
LegalCommentsBeforeToken []js_ast.Comment
LegalCommentsBeforeToken []logger.Range
WebpackComments *[]js_ast.Comment
AllOriginalComments []logger.Range
Identifier MaybeSubstring
Expand Down Expand Up @@ -2648,21 +2648,14 @@ func (lexer *Lexer) scanCommentText() {
}
}

if isMultiLineComment && (hasLegalAnnotation || isWebpackComment) {
text = helpers.RemoveMultiLineCommentIndent(lexer.source.Contents[:lexer.start], text)
}

if hasLegalAnnotation {
lexer.LegalCommentsBeforeToken = append(lexer.LegalCommentsBeforeToken, js_ast.Comment{
Loc: logger.Loc{Start: int32(lexer.start)},
Text: text,
})
lexer.LegalCommentsBeforeToken = append(lexer.LegalCommentsBeforeToken, lexer.Range())
}

if isWebpackComment {
*lexer.WebpackComments = append(*lexer.WebpackComments, js_ast.Comment{
Loc: logger.Loc{Start: int32(lexer.start)},
Text: text,
Text: lexer.source.CommentTextWithoutIndent(lexer.Range()),
})
}
}
2 changes: 1 addition & 1 deletion internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7368,7 +7368,7 @@ func (p *parser) parseStmtsUpTo(end js_lexer.T, opts parseStmtOpts) []js_ast.Stm
stmts = append(stmts, js_ast.Stmt{
Loc: comment.Loc,
Data: &js_ast.SComment{
Text: comment.Text,
Text: p.source.CommentTextWithoutIndent(comment),
IsLegalComment: true,
},
})
Expand Down
68 changes: 68 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,74 @@ func (s *Source) RangeOfLegacyOctalEscape(loc Loc) (r Range) {
return
}

func (s *Source) CommentTextWithoutIndent(r Range) string {
text := s.Contents[r.Loc.Start:r.End()]
if len(text) < 2 || !strings.HasPrefix(text, "/*") {
return text
}
prefix := s.Contents[:r.Loc.Start]

// Figure out the initial indent
indent := 0
seekBackwardToNewline:
for len(prefix) > 0 {
c, size := utf8.DecodeLastRuneInString(prefix)
switch c {
case '\r', '\n', '\u2028', '\u2029':
break seekBackwardToNewline
}
prefix = prefix[:len(prefix)-size]
indent++
}

// Split the comment into lines
var lines []string
start := 0
for i, c := range text {
switch c {
case '\r', '\n':
// Don't double-append for Windows style "\r\n" newlines
if start <= i {
lines = append(lines, text[start:i])
}

start = i + 1

// Ignore the second part of Windows style "\r\n" newlines
if c == '\r' && start < len(text) && text[start] == '\n' {
start++
}

case '\u2028', '\u2029':
lines = append(lines, text[start:i])
start = i + 3
}
}
lines = append(lines, text[start:])

// Find the minimum indent over all lines after the first line
for _, line := range lines[1:] {
lineIndent := 0
for _, c := range line {
if c != ' ' && c != '\t' {
break
}
lineIndent++
}
if indent > lineIndent {
indent = lineIndent
}
}

// Trim the indent off of all lines after the first line
for i, line := range lines {
if i > 0 {
lines[i] = line[indent:]
}
}
return strings.Join(lines, "\n")
}

func plural(prefix string, count int, shown int, someAreMissing bool) string {
var text string
if count == 1 {
Expand Down

0 comments on commit c169006

Please sign in to comment.