Skip to content

Commit

Permalink
fix: starting position of comment (#507)
Browse files Browse the repository at this point in the history
Fix Begin of Comment to point to the position of the opening slash.
  • Loading branch information
tyamagu2 authored Aug 16, 2023
1 parent 11288b7 commit 9f9bfb9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ func (p *parser) scan() (tkn token.Token, literal string, idx file.Idx) { //noli
case '/':
if p.mode&StoreComments != 0 {
literal := string(p.readSingleLineComment())
p.comments.AddComment(ast.NewComment(literal, p.idx))
p.comments.AddComment(ast.NewComment(literal, idx))
continue
}
p.skipSingleLineComment()
continue
case '*':
if p.mode&StoreComments != 0 {
literal = string(p.readMultiLineComment())
p.comments.AddComment(ast.NewComment(literal, p.idx))
p.comments.AddComment(ast.NewComment(literal, idx))
continue
}
p.skipMultiLineComment()
Expand Down
18 changes: 18 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,24 @@ func TestPosition(t *testing.T) {
node = program.Body[0].(*ast.DoWhileStatement)
is(node.Idx0(), 1)
is(parser.slice(node.Idx0(), node.Idx1()), "do { i++; } while (i < 10 )")

parser = newParser("", "(function() { // single-line comment\n })", 1, nil)
parser.mode |= StoreComments
program, err = parser.parse()
is(err, nil)
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement)
comment := parser.comments.CommentMap[block][0]
is(comment.Begin, 15)
is(parser.slice(comment.Begin, file.Idx(int(comment.Begin)+len(comment.Text)+2)), "// single-line comment")

parser = newParser("", "(function() { /* multi-line comment */ })", 1, nil)
parser.mode |= StoreComments
program, err = parser.parse()
is(err, nil)
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement)
comment = parser.comments.CommentMap[block][0]
is(comment.Begin, 15)
is(parser.slice(comment.Begin, file.Idx(int(comment.Begin)+len(comment.Text)+4)), "/* multi-line comment */")
})
}

Expand Down

0 comments on commit 9f9bfb9

Please sign in to comment.