rubocop-yard 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cc400612d09ab5ddf92d7361573854c7338a9601a3c1ed253fee7affb4b56de
4
- data.tar.gz: 79b066ad6cf601e2d3eac583333cda1f2f56873dad13d7cc19ee30238e2e9d16
3
+ metadata.gz: 77c23358faa88b10e005e0f455dcf16283fe9fbf64c92276e5bff6bb201de43a
4
+ data.tar.gz: a1fb8bfe577565d31294920356f176fd9bc71b856ee7db0a11031347473602c5
5
5
  SHA512:
6
- metadata.gz: 03c33163563d342329b62d989beb3dd4635cfa09a26b4ad3b13b7802ebf3c6286d3ebda9a4f28b7453fc42aa9bcfbafa4c6579703b90636e7d1a5ce608262312
7
- data.tar.gz: f82f34bf6a182f595669b9c0ef46567b4e86b22d73d17cc672baa297f3dbeb5cdc81aa544abedbf9787fece7aca0632f7e780e811a628d96112e9a095e206f49
6
+ metadata.gz: 478b94fe6b6bd5f310fc5774554888a5aa0f99245cc8a13ff8f5225668e5486213eeec4f7621f84b3a4790a9989a9b03a0ebc47c848fda1e757194353d3d1049
7
+ data.tar.gz: debd8aa21c9d08430fd666a239e6e6114fd1dc091053f76254def0f62d37b9d1698c291c3d91ac716fc80682b07593512bd512ecd143327d3995573dacf603fb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2023-09-14
4
+
5
+ - `YARD/TagTypeSyntax`
6
+ - Check collection tag type syntax
7
+
3
8
  ## [0.1.0] - 2023-09-13
4
9
 
5
10
  - Add new `YARD/TagTypeSyntax` cop
data/README.md CHANGED
@@ -1,24 +1,34 @@
1
- # Rubocop::YARD
1
+ # RuboCop::YARD
2
2
 
3
3
  You can check YARD format in Ruby code comment by RuboCop.
4
4
 
5
+ <img src="https://github.com/ksss/rubocop-yard/blob/main/demo.png?raw=true" width=700 />
6
+
5
7
  ## Features
6
8
 
7
9
  ### `YARD/TagTypeSyntax`
8
10
 
9
11
  Check tag type syntax error.
10
12
 
11
- ## Installation
13
+ ```
14
+ @param [Symbol|String]
15
+ ^^^^^^^^^^^^^ SyntaxError as YARD tag type
16
+ ```
12
17
 
13
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
18
+ ```
19
+ @param [Hash<Symbol, String>]
20
+ ^^^^^^^^^^^^^^^^^^^^ <Type> is the collection type syntax. Did you mean {KeyType => ValueType} or Hash{KeyType => ValueType}
21
+ ```
22
+
23
+ ## Installation
14
24
 
15
25
  Install the gem and add to the application's Gemfile by executing:
16
26
 
17
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG --require=false
27
+ $ bundle add rubocop-yard --require=false
18
28
 
19
29
  If bundler is not being used to manage dependencies, install the gem by executing:
20
30
 
21
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
31
+ $ gem install rubocop-yard
22
32
 
23
33
  ## Usage
24
34
 
@@ -44,4 +54,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
44
54
 
45
55
  ## Code of Conduct
46
56
 
47
- Everyone interacting in the Rubocop::YARD project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ksss/rubocop-yard/blob/main/CODE_OF_CONDUCT.md).
57
+ Everyone interacting in the RuboCop::YARD project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ksss/rubocop-yard/blob/main/CODE_OF_CONDUCT.md).
@@ -8,15 +8,30 @@ module RuboCop
8
8
  # @param [Integer String]
9
9
  #
10
10
  # # bad
11
- # @return [TrueClass|FalseClass]
11
+ # @param [Hash<Symbol, String>]
12
+ #
13
+ # # bad
14
+ # @param [Hash(String)]
15
+ #
16
+ # # bad
17
+ # @param [Array{Symbol => String}]
12
18
  #
13
19
  # # good
14
20
  # @param [Integer, String]
15
21
  #
16
22
  # # good
17
- # @return [Boolean]
23
+ # @param [<String>]
24
+ # @param [Array<String>]
25
+ #
26
+ # # good
27
+ # @param [(String)]
28
+ # @param [Array(String)]
29
+ #
30
+ # # good
31
+ # @param [{KeyType => ValueType}]
32
+ # @param [Hash{KeyType => ValueType}]
18
33
  class TagTypeSyntax < Base
19
- MSG = 'SyntaxError as YARD tag type'
34
+ MSG = ''
20
35
  include RangeHelp # @return [void,]
21
36
 
22
37
  def on_new_investigation
@@ -33,9 +48,49 @@ module RuboCop
33
48
  def check(comment)
34
49
  docstring = comment.text.gsub(/\A#\s*/, '')
35
50
  ::YARD::DocstringParser.new.parse(docstring).tags.each do |tag|
36
- ::YARD::Tags::TypesExplainer::Parser.parse(tag.types.join(', '))
51
+ ::YARD::Tags::TypesExplainer::Parser.parse(tag.types.join(', ')).each do |types_explainer|
52
+ check_mismatch_collection_type(comment, types_explainer)
53
+ end
37
54
  rescue SyntaxError
38
- add_offense(tag_range(comment))
55
+ add_offense(tag_range_for_comment(comment), message: 'SyntaxError as YARD tag type')
56
+ end
57
+ end
58
+
59
+ def check_mismatch_collection_type(comment, types_explainer)
60
+ case types_explainer
61
+ when ::YARD::Tags::TypesExplainer::HashCollectionType
62
+ if types_explainer.name == 'Hash'
63
+ types_explainer.key_types.each { |t| check_mismatch_collection_type(comment, t) }
64
+ types_explainer.value_types.each { |t| check_mismatch_collection_type(comment, t) }
65
+ else
66
+ message = "`{KeyType => ValueType}` is the hash collection type syntax. #{did_you_mean_type(types_explainer.name)}"
67
+ add_offense(tag_range_for_comment(comment), message: message)
68
+ end
69
+ when ::YARD::Tags::TypesExplainer::FixedCollectionType
70
+ if types_explainer.name == 'Array'
71
+ types_explainer.types.each { |t| check_mismatch_collection_type(comment, t) }
72
+ else
73
+ message = "`(Type)` is the fixed collection type syntax. #{did_you_mean_type(types_explainer.name)}"
74
+ add_offense(tag_range_for_comment(comment), message: message)
75
+ end
76
+ when ::YARD::Tags::TypesExplainer::CollectionType
77
+ if types_explainer.name == 'Array'
78
+ types_explainer.types.each { |t| check_mismatch_collection_type(comment, t) }
79
+ else
80
+ message = "`<Type>` is the collection type syntax. #{did_you_mean_type(types_explainer.name)}"
81
+ add_offense(tag_range_for_comment(comment), message: message)
82
+ end
83
+ end
84
+ end
85
+
86
+ def did_you_mean_type(name)
87
+ case name
88
+ when 'Hash'
89
+ 'Did you mean `{KeyType => ValueType}` or `Hash{KeyType => ValueType}`'
90
+ when 'Array'
91
+ 'Did you mean `<Type>` or `Array<Type>`'
92
+ else
93
+ ''
39
94
  end
40
95
  end
41
96
 
@@ -47,7 +102,7 @@ module RuboCop
47
102
  comment.source.match?(/@(?:param|return)\s+\[.*\]/)
48
103
  end
49
104
 
50
- def tag_range(comment)
105
+ def tag_range_for_comment(comment)
51
106
  start_column = comment.source.index(/\[/) + 1
52
107
  end_column = comment.source.index(/\]/)
53
108
  offense_start = comment.location.column + start_column
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'yard'
3
4
  require_relative 'yard/tag_type_syntax'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module YARD
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
data/sig/rubocop/yard.rbs CHANGED
@@ -1,6 +1,17 @@
1
- module Rubocop
1
+ module RuboCop
2
2
  module YARD
3
3
  VERSION: String
4
4
  # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
5
  end
6
+ module Cop
7
+ module YARD
8
+ class TagTypeSyntax
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
14
+ end
15
+ end
16
+ end
6
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-yard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
@@ -57,13 +57,13 @@ files:
57
57
  - lib/rubocop/yard/inject.rb
58
58
  - lib/rubocop/yard/version.rb
59
59
  - sig/rubocop/yard.rbs
60
- homepage: https://ksss.ink/
60
+ homepage: https://github.com/ksss/rubocop-yard
61
61
  licenses:
62
62
  - MIT
63
63
  metadata:
64
- homepage_uri: https://ksss.ink/
65
- source_code_uri: https://ksss.ink/
66
- changelog_uri: https://ksss.ink/
64
+ homepage_uri: https://github.com/ksss/rubocop-yard
65
+ source_code_uri: https://github.com/ksss/rubocop-yard
66
+ changelog_uri: https://github.com/ksss/rubocop-yard
67
67
  rubygems_mfa_required: 'true'
68
68
  post_install_message:
69
69
  rdoc_options: []