Skip to content

Commit 6a9f973

Browse files
authored
Merge pull request #5 from ksss/support-option
Fix to support `@option` tag
2 parents 7dfcc6b + af881bf commit 6a9f973

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

lib/rubocop/cop/yard/tag_type.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ def on_new_investigation
4848

4949
def check(comment)
5050
docstring = comment.text.gsub(/\A#\s*/, '')
51-
check_syntax_error(comment) do
52-
::YARD::DocstringParser.new.parse(docstring).tags.each do |tag|
53-
next unless tag.types
51+
::YARD::DocstringParser.new.parse(docstring).tags.each do |tag|
52+
types = extract_tag_type(tag)
5453

55-
::YARD::Tags::TypesExplainer::Parser.parse(tag.types.join(', ')).each do |types_explainer|
54+
check_syntax_error(comment) do
55+
types_explainers = ::YARD::Tags::TypesExplainer::Parser.parse(types.join(', '))
56+
types_explainers.each do |types_explainer|
5657
check_mismatch_collection_type(comment, types_explainer)
5758
end
5859
end
@@ -95,12 +96,21 @@ def check_mismatch_collection_type(comment, types_explainer)
9596
end
9697
end
9798

99+
def extract_tag_type(tag)
100+
case tag
101+
when ::YARD::Tags::OptionTag
102+
tag.pair.types
103+
else
104+
tag.types
105+
end
106+
end
107+
98108
def inline_comment?(comment)
99109
!comment_line?(comment.source_range.source_line)
100110
end
101111

102112
def include_yard_tag?(comment)
103-
comment.source.match?(/@(?:param|return|option|raise|yieldparam|yieldreturn)\s+\[.*\]/)
113+
comment.source.match?(/@(?:param|return|option|raise|yieldparam|yieldreturn)\s+.*\[.*\]/)
104114
end
105115

106116
def tag_range_for_comment(comment)

smoke/tag_type.json

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,44 @@
2525
"line": 2,
2626
"column": 13
2727
}
28+
},
29+
{
30+
"severity": "convention",
31+
"message": "(SyntaxError) expecting END, got name 'bbb'",
32+
"cop_name": "YARD/TagType",
33+
"corrected": false,
34+
"correctable": false,
35+
"location": {
36+
"start_line": 3,
37+
"start_column": 22,
38+
"last_line": 3,
39+
"last_column": 29,
40+
"length": 8,
41+
"line": 3,
42+
"column": 22
43+
}
44+
},
45+
{
46+
"severity": "convention",
47+
"message": "(SyntaxError) invalid character at -",
48+
"cop_name": "YARD/TagType",
49+
"corrected": false,
50+
"correctable": false,
51+
"location": {
52+
"start_line": 4,
53+
"start_column": 14,
54+
"last_line": 4,
55+
"last_column": 21,
56+
"length": 8,
57+
"line": 4,
58+
"column": 14
59+
}
2860
}
2961
]
3062
}
3163
],
3264
"summary": {
33-
"offense_count": 1,
65+
"offense_count": 3,
3466
"target_file_count": 1,
3567
"inspected_file_count": 1
3668
}

smoke/tag_type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Foo
22
# @param [aaa|bbb] foo
33
# @option bar baz [aaa bbb]
4+
# @return [aaa-bbb]
45
def foo
56
end
67
end

0 commit comments

Comments
 (0)