Skip to content

Commit 8645ed4

Browse files
authored
Merge pull request #22 from ksss/fix-mismatchname
Fix mismatch name if includes new line
2 parents 61c636f + 36b1c44 commit 8645ed4

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

lib/rubocop/cop/yard/helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def styled_string(types_explainer)
7575
def inline_comment?(comment)
7676
!comment_line?(comment.source_range.source_line)
7777
end
78+
79+
def build_docstring(preceding_lines)
80+
comment_texts = preceding_lines.map { |l| l.text.gsub(/\A#/, '') }
81+
minimum_space = comment_texts.map { |t| t.index(/[^\s]/) }.min
82+
yard_docstring = comment_texts.map { |t| t[minimum_space..-1] }.join("\n")
83+
begin
84+
::YARD::DocstringParser.new.parse(yard_docstring)
85+
rescue
86+
nil
87+
end
88+
end
7889
end
7990
end
8091
end

lib/rubocop/cop/yard/meaningless_tag.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module YARD
2020
# # good
2121
# CONST = 1
2222
class MeaninglessTag < Base
23+
include YARD::Helper
2324
include RangeHelp
2425
include DocumentationComment
2526
extend AutoCorrector
@@ -34,8 +35,9 @@ def check(node)
3435
preceding_lines = preceding_lines(node)
3536
return false unless preceding_comment?(node, preceding_lines.last)
3637

37-
yard_docstring = preceding_lines.map { |line| line.text.gsub(/\A#\s*/, '') }.join("\n")
38-
docstring = ::YARD::DocstringParser.new.parse(yard_docstring)
38+
docstring = build_docstring(preceding_lines)
39+
return false unless docstring
40+
3941
docstring.tags.each do |tag|
4042
next unless tag.tag_name == 'param' || tag.tag_name == 'option'
4143

lib/rubocop/cop/yard/mismatch_name.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ def on_def(node)
2828
preceding_lines = preceding_lines(node)
2929
return false unless preceding_comment?(node, preceding_lines.last)
3030

31-
yard_docstring = preceding_lines.map { |line| line.text.gsub(/\A#\s*/, '') }.join("\n")
32-
docstring = begin
33-
::YARD::DocstringParser.new.parse(yard_docstring)
34-
rescue
35-
return false
36-
end
31+
docstring = build_docstring(preceding_lines)
32+
return false unless docstring
3733

3834
return false if include_overload_tag?(docstring)
3935

smoke/generated/mismatch_name.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@
129129
"corrected": false,
130130
"correctable": false,
131131
"location": {
132-
"start_line": 47,
132+
"start_line": 52,
133133
"start_column": 18,
134-
"last_line": 47,
134+
"last_line": 52,
135135
"last_column": 25,
136136
"length": 8,
137-
"line": 47,
137+
"line": 52,
138138
"column": 18
139139
}
140140
}

smoke/generated/mismatch_name_correct.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def delegate(arg, ...)
4444

4545
def empty_doc(arg)
4646
end
47+
48+
# @param [String]
49+
# arg doc
50+
def returned(arg)
51+
end
4752
end
4853

4954
# https://github.com/ksss/rubocop-yard/issues/18

smoke/mismatch_name.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def delegate(arg, ...)
4040

4141
def empty_doc(arg)
4242
end
43+
44+
# @param [String]
45+
# arg doc
46+
def returned(arg)
47+
end
4348
end
4449

4550
# https://github.com/ksss/rubocop-yard/issues/18

0 commit comments

Comments
 (0)