Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parser to never create doc from trailing comment #11268

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [email protected]?
# 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