rubocop-publicdoc2 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f47572451602f0cb28be0997c1626c0fcd218a60dbad8c0ca5edcd97ab093500
4
+ data.tar.gz: 071c9289b76dd2a4fb9cbb1cbfd979e664c52dd6e0926943549f7d86e104c691
5
+ SHA512:
6
+ metadata.gz: 626e6be5fa81324b512397956b27659b66d8fff181d86dbaefeec6ec661ffd2779f28613124a42d9d91394508555af5ca15a27b912c634376557e58f95dbbc78
7
+ data.tar.gz: b00f1afad0dc06c87b3caa7bfc35fc253da7768aae1a2d0256b9f736233f97275eb120958efb6f3bf1ac642221a9df8ab4bba7e84308c2ad4308a2e5e360e208
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ Naming/FileName:
2
+ Exclude:
3
+ - lib/rubocop-publicdoc2.rb
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rubocop-publicdoc2
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.6.8
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.8
7
+ before_install: gem install bundler -v 2.0.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## New features
2
+ * Style/PublicMethodDocumentation
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ ruby '2.6.8'
8
+ # Specify your gem's dependencies in rubocop-publicdoc2.gemspec
9
+ gemspec
10
+ # gem 'bundler', '~> 2.0'
11
+ gem 'rake'
12
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-publicdoc2 (0.1.2)
5
+ rubocop
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.4.4)
12
+ parallel (1.20.1)
13
+ parser (3.0.2.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.0.0)
16
+ rake (13.0.6)
17
+ regexp_parser (2.1.1)
18
+ rexml (3.2.5)
19
+ rspec (3.10.0)
20
+ rspec-core (~> 3.10.0)
21
+ rspec-expectations (~> 3.10.0)
22
+ rspec-mocks (~> 3.10.0)
23
+ rspec-core (3.10.1)
24
+ rspec-support (~> 3.10.0)
25
+ rspec-expectations (3.10.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.10.0)
28
+ rspec-mocks (3.10.2)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-support (3.10.2)
32
+ rubocop (1.20.0)
33
+ parallel (~> 1.10)
34
+ parser (>= 3.0.0.0)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.8, < 3.0)
37
+ rexml
38
+ rubocop-ast (>= 1.9.1, < 2.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 3.0)
41
+ rubocop-ast (1.11.0)
42
+ parser (>= 3.0.1.1)
43
+ ruby-progressbar (1.11.0)
44
+ unicode-display_width (2.0.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.17.2)
51
+ rake
52
+ rspec
53
+ rubocop-publicdoc2!
54
+
55
+ RUBY VERSION
56
+ ruby 2.6.8p205
57
+
58
+ BUNDLED WITH
59
+ 1.17.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Paul Kristoff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Paul Kristoff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Rubocop::Publicdoc2
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubocop/publicdoc2`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rubocop-publicdoc2', require: false
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubocop-publicdoc2
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubocop-publicdoc2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Rubocop::Publicdoc2 project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rubocop-publicdoc2/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ require 'rspec/core/rake_task'
9
+
10
+ RSpec::Core::RakeTask.new(:spec) do |spec|
11
+ spec.pattern = FileList['spec/**/*_spec.rb']
12
+ end
13
+
14
+ desc 'Generate a new cop with a template'
15
+ task :new_cop, [:cop] do |_task, args|
16
+ require 'rubocop'
17
+
18
+ cop_name = args.fetch(:cop) do
19
+ warn 'usage: bundle exec rake new_cop[Department/Name]'
20
+ exit!
21
+ end
22
+
23
+ github_user = `git config github.user`.chop
24
+ github_user = 'your_id' if github_user.empty?
25
+
26
+ generator = RuboCop::Cop::Generator.new(cop_name, github_user)
27
+
28
+ generator.write_source
29
+ generator.write_spec
30
+ generator.inject_require(root_file_path: 'lib/rubocop/cop/publicdoc2_cops.rb')
31
+ generator.inject_config(config_file_path: 'config/default.yml')
32
+
33
+ puts generator.todo
34
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubocop/publicdoc2"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ # Write it!
2
+
3
+ Style/PublicMethodDocumentation:
4
+ Description: 'check public methods to have correct documentation'
5
+ Enabled: pending
6
+ VersionAdded: '0.1.0'
7
+
8
+ Style/PublicClassDocumentation:
9
+ Description: 'check classes have correct documentation'
10
+ Enabled: pending
11
+ VersionAdded: '0.1.0'
12
+
13
+ # this added to allow tests to work
14
+ Style/CommentAnnotation:
15
+ Keywords: []
16
+ Enabled: false
17
+
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'style/public_method_documentation'
3
+ require_relative 'style/public_class_documentation'
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # TODO: Write cop description and example of bad / good code. For every
7
+ # `SupportedStyle` and unique configuration, there needs to be examples.
8
+ # Examples must have valid Ruby syntax. Do not use upticks.
9
+ #
10
+ # @example EnforcedStyle: PublicClassDocumentation (default)
11
+ # # Description of the `PublicClassDocumentation` style.
12
+ #
13
+ # # bad
14
+ # class xxx
15
+ #
16
+ # # bad
17
+ # # class xxx documentation
18
+ # class xxx
19
+ #
20
+ # # bad
21
+ # # class xxx documentation
22
+ # # the end
23
+ # class xxx
24
+ #
25
+ # # good
26
+ # # class xxx documentation
27
+ # #
28
+ # class xxx
29
+ #
30
+ class PublicClassDocumentation < Documentation
31
+ # TODO: Implement the cop in here.
32
+ #
33
+ # In many cases, you can use a node matcher for matching node pattern.
34
+ # See https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
35
+ #
36
+ # For example
37
+
38
+ # overide method to add on my class doc
39
+ #
40
+ # === Parameters:
41
+ #
42
+ # * <tt>:node</tt> a class node
43
+ #
44
+ def on_class(node)
45
+ check_class_comment(node, :class)
46
+ super
47
+ end
48
+
49
+ private
50
+
51
+ def check_class_comment(node, type)
52
+ if documentation_comment?(node)
53
+ add_offense(preceding_lines(node).last, message: 'Class documentation should end with an empty line') unless class_comment_blank?(node)
54
+ else
55
+ add_offense(node, message: 'Missing class documentation')
56
+ end
57
+ end
58
+
59
+ def class_comment_blank?(node)
60
+ # assumes a comment exists
61
+ last_line = preceding_lines(node).last
62
+ /^\#$/.match?(last_line.text)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,372 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # TODO: Write cop description and example of bad / good code. For every
7
+ # `SupportedStyle` and unique configuration, there needs to be examples.
8
+ # Examples must have valid Ruby syntax. Do not use upticks.
9
+ #
10
+ # @example EnforcedStyle: PublicMethodDocumentation (default)
11
+ # # Description of the `PublicMethodDocumentation` style.
12
+ #
13
+ # # bad
14
+ # def xxx
15
+ #
16
+ # # bad
17
+ # # xxx documentation
18
+ # def xxx
19
+ #
20
+ # # bad
21
+ # # xxx documentation
22
+ # # the end
23
+ # def xxx
24
+ #
25
+ # # good
26
+ # # class xxx documentation
27
+ # #
28
+ # def xxx
29
+ #
30
+ # # bad
31
+ # # class xxx documentation
32
+ # #
33
+ # def xxx(p1)
34
+ #
35
+ class PublicMethodDocumentation < DocumentationMethod
36
+ ATTRS_DOC = '# === Attributes:'
37
+ RETURNS_DOC = '# === Returns:'
38
+ PARMS_DOC = '# === Parameters:'
39
+
40
+ MSG_ATTRIBUTES_AND_PARAMETERS_NO_COEXIST = 'Attributes and Parameters should not exist on same method.'
41
+ MSG_DESCRIPTION_SHOULD_END_WITH_BLANK_COMMENT = 'Description should end with blank comment.'
42
+ MSG_MISSING_DOCUMENTATION = 'Missing public method documentation comment for `%s`.'
43
+ MSG_MISSING_PARAMETERS = 'Parameter is missing for `%s`.'
44
+ MSG_PARAMETERS_ARG_NAME_MISMATCH = 'Parameter name `%s` does not match argument name `%s`.'
45
+ MSG_PARAMETERS_ARG_SIZE_MISMATCH = 'Parameter size `%s` does not match argument size `%s`.'
46
+ MSG_PARAMETERS_SHOULD_BE_BEFORE_RETURNS = 'Parameters should be before Returns.'
47
+ MSG_RANGE_BODY_EMPTY = '%s body is empty.'
48
+ MSG_RETURNS_SHOULD_BE_LAST = 'Returns should be last.'
49
+
50
+ # https://regex101.com/
51
+ ATTR_REGEXP = /^ *# *=== *Attributes:/.freeze
52
+ DOC_PARM_REGEXP = %r{^# \* <tt>:(\w+)</tt>}.freeze
53
+ DOC_RET_REGEXP = %r{^# \* <tt>([:\w]+)</tt>}.freeze
54
+ PARMS_REGEXP = /^ *# *=== *Parameters:/.freeze
55
+ RETURNS_REGEXP = /^ *# *=== *Returns: */.freeze
56
+
57
+ # TODO: Implement the cop in here.
58
+ #
59
+ # In many cases, you can use a node matcher for matching node pattern.
60
+ # See https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
61
+ #
62
+ # For example
63
+ include DocumentationComment
64
+ include DefNode
65
+
66
+ # checks for public methods to make sure they have proper documentation
67
+ # if not it will add an offense
68
+ #
69
+ # === Parameters:
70
+ #
71
+ # * <tt>:node</tt> a def node
72
+ #
73
+ def on_def(node)
74
+ # puts("start-#{node.children.first.to_s}")
75
+ check(node)
76
+ # puts 'end-on_def'
77
+ end
78
+
79
+ private
80
+
81
+ def add_format(message)
82
+ format(message, @method_name)
83
+ end
84
+
85
+ def add_offense(node, location: :expression, message: nil, severity: nil)
86
+ super(node, message: add_format(message), severity: severity)
87
+ end
88
+
89
+ def check(node)
90
+ return if non_public?(node)
91
+
92
+ # return if documentation_comment?(node)
93
+ prk_documentation_comment(node)
94
+ end
95
+
96
+ def prk_documentation_comment(node)
97
+ @method_name = node.children.first.to_s
98
+ # puts " processing: #{@method_name}"
99
+ preceding_lines = preceding_lines(node)
100
+
101
+ return add_offense(node, message: MSG_MISSING_DOCUMENTATION) unless preceding_comment?(node, preceding_lines.last)
102
+
103
+ description_range, parameters_range, returns_range, attrs_range = parse_documentation(preceding_lines)
104
+
105
+ add_offense(preceding_lines[0], message: MSG_MISSING_DESCRIPTION) if description_range.nil?
106
+
107
+ # order
108
+ # description_range
109
+ # parameters_range || attrs_range
110
+ # returns_range
111
+ #
112
+ grd = description_range.before?(parameters_range) &&
113
+ description_range.before?(returns_range) &&
114
+ description_range.before?(attrs_range)
115
+ add_offense(description_range.start_comment, message: MSG_DESCRIPTIION_SHOULD_BE_FIRST) unless grd
116
+ guard = parameters_range.before?(returns_range)
117
+ grd = guard && attrs_range.before?(returns_range)
118
+ add_offense(description_range.start_comment, message: MSG_RETURNS_SHOULD_BE_LAST) unless grd
119
+
120
+ grd = attrs_range.missing? || parameters_range.missing?
121
+ add_offense(attrs_range.start_comment, message: MSG_ATTRIBUTES_AND_PARAMETERS_NO_COEXIST) unless grd
122
+
123
+ special_comm = preceding_lines.any? do |comment|
124
+ !AnnotationComment.new(comment, annotation_keywords).annotation? &&
125
+ !interpreter_directive_comment?(comment) &&
126
+ !rubocop_directive_comment?(comment)
127
+ end
128
+
129
+ return add_offense(preceding_lines[index], message: MSG_INVALID_DOCUMENTATION) unless special_comm
130
+
131
+ add_offense(parameters_range.start_comment, message: MSG_PARAMETERS_SHOULD_BE_BEFORE_RETURNS) unless guard
132
+
133
+ check_blank_comments(description_range, parameters_range, returns_range, attrs_range)
134
+
135
+ args = node.arguments
136
+ guard = parameters_range.missing? && !args.empty?
137
+ return add_offense(preceding_lines[0], message: MSG_MISSING_PARAMETERS) if guard
138
+
139
+ guard = !parameters_range.missing? && args.empty?
140
+ return add_offense(parameters_range.start_comment, message: MSG_UNNECESSARY_PARAMETERS) if guard
141
+
142
+ check_body(parameters_range) unless parameters_range.missing?
143
+ check_body(attrs_range) unless attrs_range.missing?
144
+ check_body(returns_range) unless returns_range.missing?
145
+
146
+ check_parms_and_args(args, parameters_range) unless parameters_range.missing?
147
+ end
148
+
149
+ def parse_documentation(comments)
150
+ desc = MethodDocRange.new(comments, 'Description')
151
+ returns = MethodDocRange.new(comments, 'Return')
152
+ parms = MethodDocRange.new(comments, 'Parameter')
153
+ attrs = MethodDocRange.new(comments, 'Attribute')
154
+ current = nil
155
+ comments.each_with_index do |comment_line, i|
156
+ text_line = comment_line.text
157
+ if RETURNS_REGEXP.match?(text_line)
158
+ current.end = i - 1 unless current.nil?
159
+ returns.start = i # [comment_line, i, 0]
160
+ current = returns
161
+ elsif PARMS_REGEXP.match?(text_line)
162
+ current.end = i - 1 unless current.nil?
163
+ parms.start = i # [comment_line, i, 0]
164
+ current = parms
165
+ elsif ATTR_REGEXP.match?(text_line)
166
+ current.end = i - 1 unless current.nil?
167
+ attrs.start = i # [comment_line, i, 0]
168
+ current = attrs
169
+ elsif i == 0
170
+ current.end = i - 1 unless current.nil?
171
+ desc.start = i # [comment_line, i, 0]
172
+ current = desc
173
+ end
174
+ current.end = comments.size - 1
175
+ end
176
+ # !parms.first_comment? && !returns.first_comment?
177
+ add_offense(comments[0], message: MSG_MISSING_DESCRIPTION) if desc.missing?
178
+ unless parms.missing?
179
+ guard = parms.first_comment_equal?(PARMS_DOC)
180
+ add_offense(parms.start_comment, message: MSG_PARAMETERS_DOES_MATCH_MATCH) unless guard
181
+ end
182
+ unless returns.missing?
183
+ guard = returns.first_comment_equal?(RETURNS_DOC)
184
+ add_offense(returns.start_comment, message: MSG_RETURNS_DOES_NOT_MATCH) unless guard
185
+ end
186
+ unless attrs.missing?
187
+ add_offense(attrs.start_comment, message: MSG_RETURNS_DOES_NOT_MATCH) unless attrs.first_comment_equal?(ATTRS_DOC)
188
+ end
189
+ # puts 'parse_document result'
190
+ # puts(" desc =#{desc.to_s}")
191
+ # puts(" parms=#{parms.to_s}")
192
+ # puts(" returns=#{returns.to_s}")
193
+ # puts(" attrs=#{attrs.to_s}")
194
+ [desc, parms, returns, attrs]
195
+ end
196
+
197
+ def check_blank_comments(description_range, parameters_range, returns_range, attrs_range)
198
+ unless description_range.missing?
199
+ # rubocop:disable Layout/LineLength
200
+ add_offense(description_range.start_comment, message: MSG_DESCRIPTION_SHOULD_NOT_BEGIN_WITH_BLANK_COMMENT) if description_range.starts_with_empty_comment?
201
+ add_offense(description_range.end_comment, message: MSG_DESCRIPTION_SHOULD_END_WITH_BLANK_COMMENT) unless description_range.ends_with_empty_comment?
202
+ end
203
+
204
+ add_offense(parameters_range.start_comment, message: MSG_PARAMETERS_IS_MISSING_FIRST_BLANK_COMMENT) unless parameters_range.first_empty_comment?
205
+ add_offense(parameters_range.end_comment, message: MSG_PARAMETERS_SHOULD_END_WITH_BLANK_COMMENT) unless parameters_range.ends_with_empty_comment?
206
+
207
+ add_offense(returns_range.start_comment, message: MSG_RETURNS_IS_MISSING_FIRST_BLANK_COMMENT) unless returns_range.first_empty_comment?
208
+ add_offense(returns_range.end_comment, message: MSG_RETURNS_SHOULD_END_WITH_BLANK_COMMENT) unless returns_range.ends_with_empty_comment?
209
+
210
+ add_offense(attrs_range.start_comment, message: MSG_ATTRIBUTES_IS_MISSING_FIRST_BLANK_COMMENT) unless attrs_range.first_empty_comment?
211
+ ends_with_empty_comment_ = attrs_range.ends_with_empty_comment?
212
+ add_offense(attrs_range.end_comment, message: MSG_ATTRIBUTES_SHOULD_END_WITH_BLANK_COMMENT) unless ends_with_empty_comment_
213
+ # rubocop:enable Layout/LineLength
214
+ end
215
+
216
+ def check_body(range)
217
+ # puts "check_body=#{range.type} for #{@method_name}"
218
+ # puts range.start_comment.text
219
+ body = range.range_body
220
+ found = false
221
+ body.each_with_index do |line, _i|
222
+ # puts "check_body loop text=#{line.text}"
223
+ next if range.empty_comm?(line)
224
+
225
+ found = true
226
+ text = line.text.to_s
227
+ if range.returns?
228
+ # puts "check_body ret DOC_RET_REGEXP) text=#{DOC_RET_REGEXP.match(text)}"
229
+ # puts "check_body (DOC_SUB_PARM_REGEXP) text=#{DOC_SUB_PARM_REGEXP.match(text)}"
230
+ unless DOC_RET_REGEXP.match(text) || DOC_SUB_PARM_REGEXP.match(text)
231
+ add_offense(line, message: format(MSG_ILLEGAL_RANGE_RET_BODY_FORMAT, range.type)) unless text.start_with?('# **')
232
+ add_offense(line, message: format(MSG_ILLEGAL_RANGE_BODY_FORMAT_SUB, range.type)) if text.start_with?('# **')
233
+ end
234
+ else
235
+ # puts "check_body DOC_PARM_REGEXP) text=#{DOC_PARM_REGEXP.match(text)}"
236
+ # puts "check_body (DOC_SUB_PARM_REGEXP) text=#{DOC_SUB_PARM_REGEXP.match(text)}"
237
+ unless DOC_PARM_REGEXP.match(text) || DOC_SUB_PARM_REGEXP.match(text)
238
+ add_offense(line, message: format(MSG_ILLEGAL_RANGE_BODY_FORMAT, range.type)) unless text.start_with?('# **')
239
+ add_offense(line, message: format(MSG_ILLEGAL_RANGE_BODY_FORMAT_SUB, range.type)) if text.start_with?('# **')
240
+ end
241
+ end
242
+ end
243
+ # puts "check_body found=#{found}"
244
+ # puts "check_body adding_offense=#{found}" unless found
245
+ add_offense(range.start_comment, message: format(MSG_RANGE_BODY_EMPTY, range.type)) unless found
246
+ end
247
+
248
+ def check_parms_and_args(args, parameters_range)
249
+ # pns = parm_names(range_lines(preceding_lines, parameters_range))
250
+ pns = parameters_range.parm_names
251
+
252
+ # rubocop:disable Layout/LineLength
253
+ add_offense(pns[args.size][0], message: format(MSG_PARAMETERS_ARG_SIZE_MISMATCH, pns.size, args.size)) if pns.size > args.size
254
+ add_offense(args[pns.size], message: format(MSG_PARAMETERS_ARG_SIZE_MISMATCH, pns.size, args.size)) if args.size > pns.size
255
+ # rubocop:enable Layout/LineLength
256
+
257
+ match_parms_to_args(args, pns)
258
+ end
259
+
260
+ def match_parms_to_args(args, pns)
261
+ pns.each_with_index do |param_pair, i|
262
+ break if args[i].nil?
263
+
264
+ arg_name = get_arg_name(args[i])
265
+ param_line = param_pair[0]
266
+ param_name = param_pair[1]
267
+ # puts "Comparing arg name #{arg_name} with parm name #{param_name} ans=#{param_name == arg_name}"
268
+ next if param_name == arg_name
269
+
270
+ add_offense(
271
+ param_line,
272
+ message: format(MSG_PARAMETERS_ARG_NAME_MISMATCH, param_name, arg_name)
273
+ )
274
+ end
275
+ end
276
+
277
+ def get_arg_name(arg)
278
+ name = arg.node_parts[0].to_s
279
+ # handle unused arguments, which begin with _
280
+ return name[1...name.size] if name[0] == '_'
281
+
282
+ name
283
+ end
284
+ end
285
+
286
+ # method doc range checks parts of the comment for correctness
287
+ #
288
+ class MethodDocRange
289
+ attr_accessor(:end, :start, :type)
290
+
291
+ DOC_PARM_REGEXP = %r{^# \* <tt>:(\w+)</tt>}.freeze
292
+ PARM_START = '# * <tt>:'
293
+ PARM_END = '</tt>'
294
+
295
+ def initialize(comments, type)
296
+ @comments = comments
297
+ @type = type
298
+ end
299
+
300
+ def before?(method_doc_range)
301
+ return true if missing? || method_doc_range.missing?
302
+
303
+ @start < method_doc_range.start
304
+ end
305
+
306
+ def empty_comm?(comment)
307
+ txt = comment.text
308
+ txt.size <= 2
309
+ end
310
+
311
+ def end_comment
312
+ @comments[@end]
313
+ end
314
+
315
+ def ends_with_empty_comment?
316
+ missing? || empty_comm?(end_comment)
317
+ end
318
+
319
+ def first_comment?
320
+ @start == 0
321
+ end
322
+
323
+ def first_empty_comment?
324
+ missing? || empty_comm?(@comments[@start + 1])
325
+ end
326
+
327
+ def first_comment_equal?(text)
328
+ !missing? && start_comment.text == text
329
+ end
330
+
331
+ def missing?
332
+ @start.nil?
333
+ end
334
+
335
+ def parm_names
336
+ names = []
337
+ range_body.each do |parm_line|
338
+ # puts "parm_line.text=#{parm_line}"
339
+ # puts "match=#{DOC_PARM_REGEXP.match(parm_line.text).to_s}"
340
+ DOC_PARM_REGEXP.match(parm_line.text) do |m|
341
+ parm_name = m.to_s[PARM_START.size...m.to_s.index(PARM_END)]
342
+ names.push([parm_line, parm_name])
343
+ # puts "parm_name=#{parm_name}"
344
+ end
345
+ end
346
+ # puts "names=#{names}"
347
+ names
348
+ end
349
+
350
+ def range_body
351
+ @comments[@start + (first_empty_comment? ? 2 : 1)...@end + (ends_with_empty_comment? ? 0 : 1)]
352
+ end
353
+
354
+ def returns?
355
+ @type == 'Return'
356
+ end
357
+
358
+ def start_comment
359
+ @comments[@start]
360
+ end
361
+
362
+ def starts_with_empty_comment?
363
+ empty_comm?(@comments[@start])
364
+ end
365
+
366
+ def to_s
367
+ [missing? ? nil : start_comment, @start, @end]
368
+ end
369
+ end
370
+ end
371
+ end
372
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The original code is from https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
4
+ # See https://github.com/rubocop-hq/rubocop-rspec/blob/master/MIT-LICENSE.md
5
+ module RuboCop
6
+ module Publicdoc2
7
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
8
+ # bit of our configuration.
9
+ module Inject
10
+ def self.defaults!
11
+ path = CONFIG_DEFAULT.to_s
12
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
13
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
14
+ puts "configuration from #{path}" if ConfigLoader.debug?
15
+ config = ConfigLoader.merge_with_default(config, path)
16
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module RuboCop
2
+ module Publicdoc2
3
+ VERSION = '0.1.2'
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require "rubocop/publicdoc2/version"
2
+
3
+ module RuboCop
4
+ module Publicdoc2
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
8
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
9
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
10
+
11
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
12
+ end
13
+ end
14
+
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ require_relative 'rubocop/publicdoc2'
6
+ require_relative 'rubocop/publicdoc2/version'
7
+ require_relative 'rubocop/publicdoc2/inject'
8
+
9
+ RuboCop::Publicdoc2::Inject.defaults!
10
+
11
+ require_relative 'rubocop/cop/publicdoc2_cops'
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "rubocop/publicdoc2/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rubocop-publicdoc2"
7
+ spec.version = RuboCop::Publicdoc2::VERSION
8
+ spec.authors = ["Paul Kristoff"]
9
+ spec.email = ["paul.kristoff@kristoffs.com"]
10
+
11
+ spec.summary = %q{All public methods have the correct documentation}
12
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
13
+ spec.homepage = "https://github.com/pkristoff/rubocop-publicdoc2"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/pkristoff/rubocop-publicdoc2"
20
+ spec.metadata["changelog_uri"] = "https://github.com/pkristoff/rubocop-publicdoc2"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.17.2"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+
35
+ spec.add_runtime_dependency 'rubocop'
36
+ end
37
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-publicdoc2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Paul Kristoff
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.17.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.17.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - paul.kristoff@kristoffs.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".ruby-gemset"
80
+ - ".ruby-version"
81
+ - ".travis.yml"
82
+ - CHANGELOG.md
83
+ - CODE_OF_CONDUCT.md
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - LICENSE
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - bin/console
91
+ - bin/setup
92
+ - config/default.yml
93
+ - lib/rubocop-publicdoc2.rb
94
+ - lib/rubocop/cop/publicdoc2_cops.rb
95
+ - lib/rubocop/cop/style/public_class_documentation.rb
96
+ - lib/rubocop/cop/style/public_method_documentation.rb
97
+ - lib/rubocop/publicdoc2.rb
98
+ - lib/rubocop/publicdoc2/inject.rb
99
+ - lib/rubocop/publicdoc2/version.rb
100
+ - rubocop-publicdoc2.gemspec
101
+ homepage: https://github.com/pkristoff/rubocop-publicdoc2
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ allowed_push_host: https://rubygems.org
106
+ homepage_uri: https://github.com/pkristoff/rubocop-publicdoc2
107
+ source_code_uri: https://github.com/pkristoff/rubocop-publicdoc2
108
+ changelog_uri: https://github.com/pkristoff/rubocop-publicdoc2
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.0.3.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: All public methods have the correct documentation
128
+ test_files: []