1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/rdoc] Normalization of comment should check language

RDoc::Text#normalize_comment that is included RDoc::Comment always
remove Ruby style comment indicator "#" and C style comment indicator
"/**/", but should check language and remove only the language's comment
indicator.

ca68ba1e73
This commit is contained in:
aycabta 2019-01-22 04:46:46 +09:00 committed by Hiroshi SHIBATA
parent f7cbbc7074
commit a86d4eef4b
11 changed files with 73 additions and 53 deletions

View file

@ -48,9 +48,10 @@ class RDoc::Comment
# Creates a new comment with +text+ that is found in the RDoc::TopLevel # Creates a new comment with +text+ that is found in the RDoc::TopLevel
# +location+. # +location+.
def initialize text = nil, location = nil def initialize text = nil, location = nil, language = nil
@location = location @location = location
@text = text.nil? ? nil : text.dup @text = text.nil? ? nil : text.dup
@language = language
@document = nil @document = nil
@format = 'rdoc' @format = 'rdoc'

View file

@ -446,7 +446,7 @@ class RDoc::Parser::C < RDoc::Parser
next unless cls = @classes[c] next unless cls = @classes[c]
m = @known_classes[m] || m m = @known_classes[m] || m
comment = RDoc::Comment.new '', @top_level comment = RDoc::Comment.new '', @top_level, :c
incl = cls.add_include RDoc::Include.new(m, comment) incl = cls.add_include RDoc::Include.new(m, comment)
incl.record_location @top_level incl.record_location @top_level
end end
@ -564,7 +564,7 @@ class RDoc::Parser::C < RDoc::Parser
\s*"#{Regexp.escape new_name}"\s*, \s*"#{Regexp.escape new_name}"\s*,
\s*"#{Regexp.escape old_name}"\s*\);%xm \s*"#{Regexp.escape old_name}"\s*\);%xm
RDoc::Comment.new($1 || '', @top_level) RDoc::Comment.new($1 || '', @top_level, :c)
end end
## ##
@ -603,7 +603,7 @@ class RDoc::Parser::C < RDoc::Parser
'' ''
end end
RDoc::Comment.new comment, @top_level RDoc::Comment.new comment, @top_level, :c
end end
## ##
@ -643,7 +643,7 @@ class RDoc::Parser::C < RDoc::Parser
case type case type
when :func_def when :func_def
comment = RDoc::Comment.new args[0], @top_level comment = RDoc::Comment.new args[0], @top_level, :c
body = args[1] body = args[1]
offset, = args[2] offset, = args[2]
@ -673,7 +673,7 @@ class RDoc::Parser::C < RDoc::Parser
body body
when :macro_def when :macro_def
comment = RDoc::Comment.new args[0], @top_level comment = RDoc::Comment.new args[0], @top_level, :c
body = args[1] body = args[1]
offset, = args[2] offset, = args[2]
@ -780,7 +780,7 @@ class RDoc::Parser::C < RDoc::Parser
comment = '' comment = ''
end end
comment = RDoc::Comment.new comment, @top_level comment = RDoc::Comment.new comment, @top_level, :c
comment.normalize comment.normalize
look_for_directives_in class_mod, comment look_for_directives_in class_mod, comment
@ -825,7 +825,7 @@ class RDoc::Parser::C < RDoc::Parser
table[const_name] || table[const_name] ||
'' ''
RDoc::Comment.new comment, @top_level RDoc::Comment.new comment, @top_level, :c
end end
## ##
@ -856,7 +856,7 @@ class RDoc::Parser::C < RDoc::Parser
return unless comment return unless comment
RDoc::Comment.new comment, @top_level RDoc::Comment.new comment, @top_level, :c
end end
## ##
@ -990,7 +990,7 @@ class RDoc::Parser::C < RDoc::Parser
new_comment = "#{$1}#{new_comment.lstrip}" new_comment = "#{$1}#{new_comment.lstrip}"
new_comment = RDoc::Comment.new new_comment, @top_level new_comment = RDoc::Comment.new new_comment, @top_level, :c
con = RDoc::Constant.new const_name, new_definition, new_comment con = RDoc::Constant.new const_name, new_definition, new_comment
else else

View file

@ -667,7 +667,7 @@ class RDoc::Parser::Ruby < RDoc::Parser
# Creates a comment with the correct format # Creates a comment with the correct format
def new_comment comment def new_comment comment
c = RDoc::Comment.new comment, @top_level c = RDoc::Comment.new comment, @top_level, :ruby
c.format = @markup c.format = @markup
c c
end end

View file

@ -10,6 +10,8 @@ require 'strscan'
module RDoc::Text module RDoc::Text
attr_accessor :language
## ##
# Maps markup formats to classes that can parse them. If the format is # Maps markup formats to classes that can parse them. If the format is
# unknown, "rdoc" format is used. # unknown, "rdoc" format is used.
@ -111,8 +113,12 @@ module RDoc::Text
def normalize_comment text def normalize_comment text
return text if text.empty? return text if text.empty?
text = strip_stars text case language
text = strip_hashes text when :ruby
text = strip_hashes text
when :c
text = strip_stars text
end
text = expand_tabs text text = expand_tabs text
text = flush_left text text = flush_left text
text = strip_newlines text text = strip_newlines text

View file

@ -97,8 +97,9 @@ class RDoc::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Un
# Creates an RDoc::Comment with +text+ which was defined on +top_level+. # Creates an RDoc::Comment with +text+ which was defined on +top_level+.
# By default the comment has the 'rdoc' format. # By default the comment has the 'rdoc' format.
def comment text, top_level = @top_level def comment text, top_level = @top_level, language = nil
RDoc::Comment.new text, top_level comment = RDoc::Comment.new text, top_level, language
comment
end end
## ##

View file

@ -9,19 +9,19 @@ class TestRDocClassModule < XrefTestCase
tl3 = @store.add_file 'three.rb' tl3 = @store.add_file 'three.rb'
cm = RDoc::ClassModule.new 'Klass' cm = RDoc::ClassModule.new 'Klass'
comment_tl1 = RDoc::Comment.new('# comment 1') comment_tl1 = RDoc::Comment.new('# comment 1', @top_level, :ruby)
cm.add_comment comment_tl1, tl1 cm.add_comment comment_tl1, tl1
assert_equal [[comment_tl1, tl1]], cm.comment_location assert_equal [[comment_tl1, tl1]], cm.comment_location
assert_equal 'comment 1', cm.comment.text assert_equal 'comment 1', cm.comment.text
comment_tl2 = RDoc::Comment.new('# comment 2') comment_tl2 = RDoc::Comment.new('# comment 2', @top_level, :ruby)
cm.add_comment comment_tl2, tl2 cm.add_comment comment_tl2, tl2
assert_equal [[comment_tl1, tl1], [comment_tl2, tl2]], cm.comment_location assert_equal [[comment_tl1, tl1], [comment_tl2, tl2]], cm.comment_location
assert_equal "comment 1\n---\ncomment 2", cm.comment assert_equal "comment 1\n---\ncomment 2", cm.comment
comment_tl3 = RDoc::Comment.new('# * comment 3') comment_tl3 = RDoc::Comment.new('# * comment 3', @top_level, :ruby)
cm.add_comment comment_tl3, tl3 cm.add_comment comment_tl3, tl3
assert_equal [[comment_tl1, tl1], assert_equal [[comment_tl1, tl1],
@ -42,11 +42,13 @@ class TestRDocClassModule < XrefTestCase
tl1 = @store.add_file 'one.rb' tl1 = @store.add_file 'one.rb'
cm = RDoc::ClassModule.new 'Klass' cm = RDoc::ClassModule.new 'Klass'
cm.add_comment '# comment 1', tl1 comment1 = RDoc::Comment.new('# comment 1', @top_level, :ruby)
cm.add_comment '# comment 2', tl1 comment2 = RDoc::Comment.new('# comment 2', @top_level, :ruby)
cm.add_comment comment1, tl1
cm.add_comment comment2, tl1
assert_equal [['comment 1', tl1], assert_equal [[comment1, tl1],
['comment 2', tl1]], cm.comment_location [comment2, tl1]], cm.comment_location
end end
def test_add_comment_stopdoc def test_add_comment_stopdoc
@ -66,17 +68,17 @@ class TestRDocClassModule < XrefTestCase
def test_comment_equals def test_comment_equals
cm = RDoc::ClassModule.new 'Klass' cm = RDoc::ClassModule.new 'Klass'
cm.comment = '# comment 1' cm.comment = RDoc::Comment.new('# comment 1', @top_level, :ruby)
assert_equal 'comment 1', cm.comment assert_equal 'comment 1', cm.comment.to_s
cm.comment = '# comment 2' cm.comment = RDoc::Comment.new('# comment 2', @top_level, :ruby)
assert_equal "comment 1\n---\ncomment 2", cm.comment assert_equal "comment 1\n---\ncomment 2", cm.comment.to_s
cm.comment = "# * comment 3" cm.comment = RDoc::Comment.new('# * comment 3', @top_level, :ruby)
assert_equal "comment 1\n---\ncomment 2\n---\n* comment 3", cm.comment assert_equal "comment 1\n---\ncomment 2\n---\n* comment 3", cm.comment.to_s
end end
def test_comment_equals_comment def test_comment_equals_comment

View file

@ -241,6 +241,7 @@ lines, one line per element. Lines are assumed to be separated by _sep_.
@comment.text = <<-TEXT @comment.text = <<-TEXT
# comment # comment
TEXT TEXT
@comment.language = :ruby
assert_same @comment, @comment.normalize assert_same @comment, @comment.normalize

View file

@ -11,7 +11,7 @@ class TestRDocContextSection < RDoc::TestCase
@klass = @top_level.add_class RDoc::NormalClass, 'Object' @klass = @top_level.add_class RDoc::NormalClass, 'Object'
@S = RDoc::Context::Section @S = RDoc::Context::Section
@s = @S.new @klass, 'section', comment('# comment', @top_level) @s = @S.new @klass, 'section', comment('# comment', @top_level, :ruby)
end end
def test_add_comment def test_add_comment

View file

@ -1381,7 +1381,7 @@ commercial() -> Date <br />
end end
def test_find_modifiers_yields def test_find_modifiers_yields
comment = RDoc::Comment.new <<-COMMENT comment = RDoc::Comment.new <<-COMMENT, @top_level, :c
/* :yields: a, b /* :yields: a, b
* *
* Blah * Blah

View file

@ -435,7 +435,7 @@ ruby
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# my attr\n", @top_level comment = RDoc::Comment.new "##\n# my attr\n", @top_level, :ruby
util_parser "attr :foo, :bar" util_parser "attr :foo, :bar"
@ -472,7 +472,7 @@ ruby
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# my attr\n", @top_level comment = RDoc::Comment.new "##\n# my attr\n", @top_level, :ruby
util_parser "attr_accessor :foo, :bar" util_parser "attr_accessor :foo, :bar"
@ -499,7 +499,7 @@ ruby
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# my attr\n", @top_level comment = RDoc::Comment.new "##\n# my attr\n", @top_level, :ruby
util_parser "attr_accessor :foo, :bar,\n :baz,\n :qux" util_parser "attr_accessor :foo, :bar,\n :baz,\n :qux"
@ -584,7 +584,7 @@ ruby
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# my attr\n", @top_level comment = RDoc::Comment.new "##\n# my attr\n", @top_level, :ruby
util_parser "attr_writer :foo, :bar" util_parser "attr_writer :foo, :bar"
@ -610,7 +610,7 @@ ruby
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# :attr: \n# my method\n", @top_level comment = RDoc::Comment.new "##\n# :attr: \n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar" util_parser "add_my_method :foo, :bar"
@ -631,7 +631,7 @@ ruby
klass.parent = @top_level klass.parent = @top_level
comment = comment =
RDoc::Comment.new "##\n# :attr_accessor: \n# my method\n", @top_level RDoc::Comment.new "##\n# :attr_accessor: \n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar" util_parser "add_my_method :foo, :bar"
@ -651,7 +651,7 @@ ruby
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# :attr: foo\n# my method\n", @top_level comment = RDoc::Comment.new "##\n# :attr: foo\n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar" util_parser "add_my_method :foo, :bar"
@ -672,7 +672,7 @@ ruby
klass.parent = @top_level klass.parent = @top_level
comment = comment =
RDoc::Comment.new "##\n# :attr_reader: \n# my method\n", @top_level RDoc::Comment.new "##\n# :attr_reader: \n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar" util_parser "add_my_method :foo, :bar"
@ -708,7 +708,7 @@ ruby
klass.parent = @top_level klass.parent = @top_level
comment = comment =
RDoc::Comment.new "##\n# :attr_writer: \n# my method\n", @top_level RDoc::Comment.new "##\n# :attr_writer: \n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar" util_parser "add_my_method :foo, :bar"
@ -724,7 +724,7 @@ ruby
end end
def test_parse_class def test_parse_class
comment = RDoc::Comment.new "##\n# my class\n", @top_level comment = RDoc::Comment.new "##\n# my class\n", @top_level, :ruby
util_parser "class Foo\nend" util_parser "class Foo\nend"
@ -1001,7 +1001,7 @@ end
end end
def test_parse_module def test_parse_module
comment = RDoc::Comment.new "##\n# my module\n", @top_level comment = RDoc::Comment.new "##\n# my module\n", @top_level, :ruby
util_parser "module Foo\nend" util_parser "module Foo\nend"
@ -1247,7 +1247,7 @@ EOF
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# :attr: foo\n# my attr\n", @top_level comment = RDoc::Comment.new "##\n# :attr: foo\n# my attr\n", @top_level, :ruby
util_parser "\n" util_parser "\n"
@ -1311,7 +1311,7 @@ EOF
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# :method: foo\n# my method\n", @top_level comment = RDoc::Comment.new "##\n# :method: foo\n# my method\n", @top_level, :ruby
util_parser "\n" util_parser "\n"
@ -1595,7 +1595,7 @@ end
klass = RDoc::NormalClass.new 'C' klass = RDoc::NormalClass.new 'C'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "# my extend\n", @top_level comment = RDoc::Comment.new "# my extend\n", @top_level, :ruby
util_parser "extend I" util_parser "extend I"
@ -1615,7 +1615,7 @@ end
klass = RDoc::NormalClass.new 'C' klass = RDoc::NormalClass.new 'C'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "# my include\n", @top_level comment = RDoc::Comment.new "# my include\n", @top_level, :ruby
util_parser "include I" util_parser "include I"
@ -1635,7 +1635,7 @@ end
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# my method\n", @top_level comment = RDoc::Comment.new "##\n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar\nadd_my_method :baz" util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
@ -1719,7 +1719,7 @@ end
def test_parse_meta_method_define_method def test_parse_meta_method_define_method
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
comment = RDoc::Comment.new "##\n# my method\n", @top_level comment = RDoc::Comment.new "##\n# my method\n", @top_level, :ruby
util_parser "define_method :foo do end" util_parser "define_method :foo do end"
@ -1738,7 +1738,7 @@ end
klass.parent = @top_level klass.parent = @top_level
comment = comment =
RDoc::Comment.new "##\n# :method: woo_hoo!\n# my method\n", @top_level RDoc::Comment.new "##\n# :method: woo_hoo!\n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar\nadd_my_method :baz" util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
@ -1757,7 +1757,7 @@ end
klass.parent = @top_level klass.parent = @top_level
comment = comment =
RDoc::Comment.new "##\n# :singleton-method:\n# my method\n", @top_level RDoc::Comment.new "##\n# :singleton-method:\n# my method\n", @top_level, :ruby
util_parser "add_my_method :foo, :bar\nadd_my_method :baz" util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
@ -1778,7 +1778,7 @@ end
comment = comment =
RDoc::Comment.new "##\n# :singleton-method: woo_hoo!\n# my method\n", RDoc::Comment.new "##\n# :singleton-method: woo_hoo!\n# my method\n",
@top_level @top_level, :ruby
util_parser "add_my_method :foo, :bar\nadd_my_method :baz" util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
@ -1795,7 +1795,7 @@ end
def test_parse_meta_method_string_name def test_parse_meta_method_string_name
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
comment = RDoc::Comment.new "##\n# my method\n", @top_level comment = RDoc::Comment.new "##\n# my method\n", @top_level, :ruby
util_parser "add_my_method 'foo'" util_parser "add_my_method 'foo'"
@ -1827,7 +1827,7 @@ end
def test_parse_meta_method_unknown def test_parse_meta_method_unknown
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
comment = RDoc::Comment.new "##\n# my method\n", @top_level comment = RDoc::Comment.new "##\n# my method\n", @top_level, :ruby
util_parser "add_my_method ('foo')" util_parser "add_my_method ('foo')"
@ -1845,7 +1845,7 @@ end
klass = RDoc::NormalClass.new 'Foo' klass = RDoc::NormalClass.new 'Foo'
klass.parent = @top_level klass.parent = @top_level
comment = RDoc::Comment.new "##\n# my method\n", @top_level comment = RDoc::Comment.new "##\n# my method\n", @top_level, :ruby
util_parser "def foo() :bar end" util_parser "def foo() :bar end"

View file

@ -13,6 +13,7 @@ class TestRDocText < RDoc::TestCase
@options = RDoc::Options.new @options = RDoc::Options.new
@top_level = @store.add_file 'file.rb' @top_level = @store.add_file 'file.rb'
@language = nil
end end
def test_self_encode_fallback def test_self_encode_fallback
@ -137,6 +138,8 @@ we don't worry too much.
The comments associated with The comments associated with
EXPECTED EXPECTED
@language = :ruby
assert_equal expected, normalize_comment(text) assert_equal expected, normalize_comment(text)
end end
@ -155,6 +158,8 @@ we don't worry too much.
The comments associated with The comments associated with
EXPECTED EXPECTED
@language = :c
assert_equal expected, normalize_comment(text) assert_equal expected, normalize_comment(text)
end end
@ -173,6 +178,8 @@ we don't worry too much.
The comments associated with The comments associated with
EXPECTED EXPECTED
@language = :c
assert_equal expected, normalize_comment(text) assert_equal expected, normalize_comment(text)
end end
@ -200,6 +207,8 @@ The comments associated with
end end
def test_parse_empty_newline def test_parse_empty_newline
@language = :ruby
assert_equal RDoc::Markup::Document.new, parse("#\n") assert_equal RDoc::Markup::Document.new, parse("#\n")
end end