Skip to content

Commit

Permalink
Fix parser to never create doc from trailing comment (#11268)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Sep 17, 2022
1 parent 2da3efc commit d8d3a0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
23 changes: 23 additions & 0 deletions spec/compiler/crystal/tools/doc/method_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ describe Doc::Method do
method.doc_copied_from.should be_nil
end

it "trailing comment is not a doc comment" do
program = semantic(<<-CR, inject_primitives: false, wants_doc: true).program
nil # trailing comment
def foo
end
CR
generator = Doc::Generator.new program, [""]
method = generator.type(program).lookup_class_method("foo").not_nil!
method.doc.should be_nil
end

it "trailing comment is not part of a doc comment" do
program = semantic(<<-CR, inject_primitives: false, wants_doc: true).program
nil # trailing comment
# doc comment
def foo
end
CR
generator = Doc::Generator.new program, [""]
method = generator.type(program).lookup_class_method("foo").not_nil!
method.doc.should eq("doc comment")
end

it "inherits doc from ancestor (no extra comment)" do
program = semantic("
class Foo
Expand Down
13 changes: 12 additions & 1 deletion src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module Crystal
@filename = ""
@wants_regex = true
@doc_enabled = false
@comment_is_doc = true
@comments_enabled = false
@count_whitespace = false
@slash_is_regex = true
Expand Down Expand Up @@ -96,6 +97,16 @@ module Crystal
end

def next_token
# Check previous token:
if @token.type.newline? || @token.type.eof?
# 1) After a newline or at the start of the stream (:EOF), a following comment can be a doc comment
@comment_is_doc = true
elsif !@token.type.space?
# 2) Any non-space token prevents a following comment from being a doc
# comment.
@comment_is_doc = false
end

reset_token

# Skip comments
Expand All @@ -112,7 +123,7 @@ module Crystal
consume_loc_pragma
start = current_pos
else
if @doc_enabled
if @doc_enabled && @comment_is_doc
consume_doc
elsif @comments_enabled
return consume_comment(start)
Expand Down

0 comments on commit d8d3a0f

Please sign in to comment.