Skip to content

Commit efd208b

Browse files
committed
Consider the case where types are nil
Fix #19
1 parent 498d934 commit efd208b

File tree

9 files changed

+79
-8
lines changed

9 files changed

+79
-8
lines changed

lib/rubocop/cop/yard/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def parse_type(type)
1919

2020
def each_types_explainer(docstring, &block)
2121
docstring.tags.each do |tag|
22-
types = extract_tag_types(tag)
22+
types = extract_tag_types(tag) or next
2323

2424
begin
2525
types_explainers = parse_type(types.join(', '))

lib/rubocop/cop/yard/mismatch_name.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def on_def(node)
5757
end
5858

5959
next unless node.arguments.none? { |arg_node| tag.name.to_sym == arg_node.name }
60+
next unless types
6061

6162
begin
6263
parse_type(types.join(', '))

lib/rubocop/cop/yard/tag_type_syntax.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def on_new_investigation
2727
def check(comment)
2828
docstring = comment.text.gsub(/\A#\s*/, '')
2929
::YARD::DocstringParser.new.parse(docstring).tags.each do |tag|
30-
types = extract_tag_types(tag)
30+
types = extract_tag_types(tag) or next
3131

3232
check_syntax_error(comment) do
3333
parse_type(types.join(', '))

sig/rubocop/yard.rbs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ module RuboCop
55
end
66
module Cop
77
module YARD
8+
type tag_types = YARD::Tags::TypesExplainer::Type
9+
| ::YARD::Tags::TypesExplainer::CollectionType
10+
| ::YARD::Tags::TypesExplainer::FixedCollectionType
11+
| ::YARD::Tags::TypesExplainer::HashCollectionType
812
class CollectionType
9-
type t = YARD::Tags::TypesExplainer::Type
10-
| ::YARD::Tags::TypesExplainer::CollectionType
11-
| ::YARD::Tags::TypesExplainer::FixedCollectionType
12-
| ::YARD::Tags::TypesExplainer::HashCollectionType
13-
private def check_mismatch_collection_type: (untyped comment, t types_explainer) -> void
13+
private def check_mismatch_collection_type: (untyped comment, tag_types types_explainer) -> void
14+
end
15+
16+
module Helper
17+
def extract_tag_types: (::YARD::Tags::OptionTag | ::YARD::Tags::Tag tag) -> Array[String]?
18+
end
19+
20+
class TagTypeSyntax < ::RuboCop::Cop::Base
21+
include YARD::Helper
22+
23+
private
24+
25+
def check: (::Parser::Source::Comment comment) -> void
26+
def check_syntax_error: (::Parser::Source::Comment comment) { () -> void } -> void
27+
def inline_comment?: (::Parser::Source::Comment comment) -> bool
28+
def include_yard_tag?: (::Parser::Source::Comment comment) -> bool
29+
def tag_range_for_comment: (::Parser::Source::Comment comment) -> untyped
30+
end
31+
32+
class MismatchName < ::RuboCop::Cop::Base
33+
include YARD::Helper
1434
end
1535
end
1636
end

smoke/collection_style_long.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ class Foo
1212
def foo
1313
end
1414
end
15+
16+
# https://github.com/ksss/rubocop-yard/issues/19
17+
class Test
18+
# @param [Hash{Symbol=>Object}] options The options
19+
# @option [Integer] :op Option associated to no param
20+
def test(options)
21+
end
22+
end
23+

smoke/collection_style_short.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ class Foo
1212
def foo
1313
end
1414
end
15+
16+
# https://github.com/ksss/rubocop-yard/issues/19
17+
class Test
18+
# @param [Hash{Symbol=>Object}] options The options
19+
# @option [Integer] :op Option associated to no param
20+
def test(options)
21+
end
22+
end

smoke/generated/collection_style_long_correct.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ class Foo
1212
def foo
1313
end
1414
end
15+
16+
# https://github.com/ksss/rubocop-yard/issues/19
17+
class Test
18+
# @param [Hash{Symbol=>Object}] options The options
19+
# @option [Integer] :op Option associated to no param
20+
def test(options)
21+
end
22+
end
23+

smoke/generated/collection_style_short.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,28 @@
7373
"line": 11,
7474
"column": 3
7575
}
76+
},
77+
{
78+
"severity": "convention",
79+
"message": "`Hash{Symbol=>Object}` is using long style syntax",
80+
"cop_name": "YARD/CollectionStyle",
81+
"corrected": false,
82+
"correctable": true,
83+
"location": {
84+
"start_line": 18,
85+
"start_column": 3,
86+
"last_line": 18,
87+
"last_column": 53,
88+
"length": 51,
89+
"line": 18,
90+
"column": 3
91+
}
7692
}
7793
]
7894
}
7995
],
8096
"summary": {
81-
"offense_count": 4,
97+
"offense_count": 5,
8298
"target_file_count": 1,
8399
"inspected_file_count": 1
84100
}

smoke/generated/collection_style_short_correct.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ class Foo
1212
def foo
1313
end
1414
end
15+
16+
# https://github.com/ksss/rubocop-yard/issues/19
17+
class Test
18+
# @param [{Symbol => Object}] options The options
19+
# @option [Integer] :op Option associated to no param
20+
def test(options)
21+
end
22+
end

0 commit comments

Comments
 (0)