Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to set StyleGuideBaseURL per department #7295

Merged
merged 2 commits into from
Aug 19, 2019

Conversation

koic
Copy link
Member

@koic koic commented Aug 19, 2019

Summary

Resolves rubocop/rubocop-rails#107 and follow up rubocop/rubocop-rails#109 (comment).

This PR makes it possible to set a style guide URL (StyleGuideBaseURL) for each department.

For example, RuboCop Rails gem (Rails department) supports The Rails Style Guide. In that case, set .rubocop.yml as follows.

# .rubocop.yml
require:
  - rubocop-rails

AllCops:
  StyleGuideBaseURL:  https://rubystyle.guide

Rails:
  StyleGuideBaseURL:  https://rails.rubystyle.guide

In practice Rails: StyleGuideBaseURL: https://rails.rubystyle.guide will be set in config/default.yml of rubocop-rails.

% cat example.rb
# frozen_string_literal: true

time = Time.new
% bundle exec rubocop --display-style-guide example.rb
Inspecting 1 file
W

Offenses:

example.rb:3:1: W: Lint/UselessAssignment: Useless assignment to
variable - time. (https://rubystyle.guide#underscore-unused-vars)
time = Time.new
^^^^
example.rb:3:13: C: Rails/TimeZone: Do not use Time.new without
zone. Use one of Time.zone.now, Time.current, Time.new.in_time_zone,
Time.new.utc, Time.new.getlocal, Time.new.xmlschema, Time.new.iso8601,
Time.new.jisx0301, Time.new.rfc3339, Time.new.httpdate, Time.new.to_i,
Time.new.to_f instead. (https://rails.rubystyle.guide#time,
http://danilenko.org/2012/7/6/rails_timezones)
time = Time.new
            ^^^

1 file inspected, 2 offenses detected

The style guide URLs set for each department is displayed.

So AllCops style guide URL is used when there is no style guide URL for a specific department.

Other Information

Note that tasks/cops_documentation.rake will require the following patch:

diff --git a/tasks/cops_documentation.rake
b/tasks/cops_documentation.rake
index bd93ffe31..751222fcb 100644
--- a/tasks/cops_documentation.rake
+++ b/tasks/cops_documentation.rake
@@ -155,7 +155,9 @@ task generate_cops_documentation:
:yard_for_generate_documentation do

   def references(config, cop)
     cop_config = config.for_cop(cop)
-    urls = RuboCop::Cop::MessageAnnotator.new(config, cop_config, {}).urls
+    urls = RuboCop::Cop::MessageAnnotator.new(
+      config, cop.name, cop_config, {}
+    ).urls
     return '' if urls.empty?

     content = h3('References')

AFAIK, rubocop-performance, rubocop-rails, rubocop-rspec, and rubocop-minitest repositories are targeted.

/cc @bquorning


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic force-pushed the add_style_guide_base_url_plugin branch from 06ec0b4 to 9bf9e21 Compare August 19, 2019 05:34
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 19, 2019

I like that approach, but I think we should also mention it somewhere in the documentation.

CHANGELOG.md Outdated
@@ -5,6 +5,7 @@
### New features

* [#7274](https://github.com/rubocop-hq/rubocop/issues/7274): Add new `Lint/SendWithMixinArgument` cop. ([@koic][])
* [#7295](https://github.com/rubocop-hq/rubocop/pull/7295): Add a plugin for `StyleGuideBaseURL`. ([@koic][])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling this a plugin might be a bit misleading - something like "Make it possible to set StyleGuideBaseURL per department." sounds better to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review. I update it.

@koic koic changed the title Add a plugin for StyleGuideBaseURL Make it possible to set StyleGuideBaseURL per department Aug 19, 2019
@koic koic force-pushed the add_style_guide_base_url_plugin branch from 9bf9e21 to d5f1483 Compare August 19, 2019 07:07
@koic
Copy link
Member Author

koic commented Aug 19, 2019

I tried adding a document about this.
d5f1483

### Summary

Resolves rubocop/rubocop-rails#107
and follow up rubocop/rubocop-rails#109 (comment).

This PR makes it possible to set a style guide URL (`StyleGuideBaseURL`)
for each department.

For example, RuboCop Rails gem (Rails department) supports
The Rails Style Guide. In that case, set .rubocop.yml as follows.

```yaml
# .rubocop.yml
require:
  - rubocop-rails

AllCops:
  StyleGuideBaseURL:  https://rubystyle.guide

Rails:
  StyleGuideBaseURL:  https://rails.rubystyle.guide
```

In practice `Rails: StyleGuideBaseURL: https://rails.rubystyle.guide`
will be set in config/default.yml of rubocop-rails.

```console
% cat example.rb
# frozen_string_literal: true

time = Time.new
```

```console
% bundle exec rubocop --display-style-guide example.rb
Inspecting 1 file
W

Offenses:

example.rb:3:1: W: Lint/UselessAssignment: Useless assignment to
variable - time. (https://rubystyle.guide#underscore-unused-vars)
time = Time.new
^^^^
example.rb:3:13: C: Rails/TimeZone: Do not use Time.new without
zone. Use one of Time.zone.now, Time.current, Time.new.in_time_zone,
Time.new.utc, Time.new.getlocal, Time.new.xmlschema, Time.new.iso8601,
Time.new.jisx0301, Time.new.rfc3339, Time.new.httpdate, Time.new.to_i,
Time.new.to_f instead. (https://rails.rubystyle.guide#time,
http://danilenko.org/2012/7/6/rails_timezones)
time = Time.new
            ^^^

1 file inspected, 2 offenses detected
```

The style guide URLs set for each department is displayed.

- https://rubystyle.guide#underscore-unused-vars for `Lint/UselessAssignment` cop
- https://rails.rubystyle.guide#time for for `Rails/TimeZone` cop

So `AllCops` style guide URL is used when there is no style guide URL
for a specific department.

### Other Information

Note that tasks/cops_documentation.rake will require the following patch:

```diff
diff --git a/tasks/cops_documentation.rake
b/tasks/cops_documentation.rake
index bd93ffe31..751222fcb 100644
--- a/tasks/cops_documentation.rake
+++ b/tasks/cops_documentation.rake
@@ -155,7 +155,9 @@ task generate_cops_documentation:
:yard_for_generate_documentation do

   def references(config, cop)
     cop_config = config.for_cop(cop)
-    urls = RuboCop::Cop::MessageAnnotator.new(config, cop_config, {}).urls
+    urls = RuboCop::Cop::MessageAnnotator.new(
+      config, cop.name, cop_config, {}
+    ).urls
     return '' if urls.empty?

     content = h3('References')
```

AFAIK, rubocop-performance, rubocop-rails, rubocop-rspec, and
rubocop-minitest repositories are targeted.
@koic koic force-pushed the add_style_guide_base_url_plugin branch from d5f1483 to bf67a9e Compare August 19, 2019 07:23
@bbatsov bbatsov merged commit baece78 into rubocop:master Aug 19, 2019
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 19, 2019

Great work! Thanks!

@koic koic deleted the add_style_guide_base_url_plugin branch August 19, 2019 09:34
koic added a commit to rubocop/rubocop-rails that referenced this pull request Aug 19, 2019
koic added a commit to rubocop/rubocop-performance that referenced this pull request Aug 19, 2019
Follow up rubocop/rubocop#7295.

This commit fixes the following error.

```console
% bundle exec rake
The `SafeMode` option will be removed in `RuboCop` 0.76. Please update
`rubocop-performance` to 1.15.0 or higher.
Files:          25
Modules:         3 (    3 undocumented)
Classes:        26 (    0 undocumented)
Constants:      50 (   45 undocumented)
Attributes:      0 (    0 undocumented)
Methods:        53 (   47 undocumented)
 28.03% documented
rake aborted!
ArgumentError: wrong number of arguments (given 3, expected 4)
tasks/cops_documentation.rake:154:in `new'
tasks/cops_documentation.rake:154:in `references'
tasks/cops_documentation.rake:24:in `cops_body'
tasks/cops_documentation.rake:197:in `print_cop_with_doc'
tasks/cops_documentation.rake:173:in `block in print_cops_of_department'
tasks/cops_documentation.rake:172:in `each'
tasks/cops_documentation.rake:172:in `print_cops_of_department'
tasks/cops_documentation.rake:268:in `block in main'
tasks/cops_documentation.rake:267:in `each'
tasks/cops_documentation.rake:267:in `main'
tasks/cops_documentation.rake:278:in `block in <top (required)>'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
Tasks: TOP => default => generate_cops_documentation
(See full trace by running task with --trace)
```
koic added a commit to rubocop/rubocop-minitest that referenced this pull request Aug 22, 2019
Follow rubocop/rubocop#7295

This commit fixes the following error.

```console
% bundle exec rake
Files:           1
Modules:         3 (    3 undocumented)
Classes:         1 (    0 undocumented)
Constants:       1 (    1 undocumented)
Attributes:      0 (    0 undocumented)
Methods:         2 (    2 undocumented)
 14.29% documented
 rake aborted!
 ArgumentError: wrong number of arguments (given 3, expected 4)
 tasks/cops_documentation.rake:156:in `new'
 tasks/cops_documentation.rake:156:in `references'
 tasks/cops_documentation.rake:24:in `cops_body'
 tasks/cops_documentation.rake:197:in `print_cop_with_doc'
 tasks/cops_documentation.rake:173:in `block in
 print_cops_of_department'
 tasks/cops_documentation.rake:172:in `each'
 tasks/cops_documentation.rake:172:in `print_cops_of_department'
 tasks/cops_documentation.rake:268:in `block in main'
 tasks/cops_documentation.rake:267:in `each'
 tasks/cops_documentation.rake:267:in `main'
 tasks/cops_documentation.rake:278:in `block in <top (required)>'
 /Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
 /Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
 Tasks: TOP => default => generate_cops_documentation
 (See full trace by running task with --trace)
 ```
abhaynikam pushed a commit to abhaynikam/rubocop-minitest that referenced this pull request Sep 4, 2019
Follow rubocop/rubocop#7295

This commit fixes the following error.

```console
% bundle exec rake
Files:           1
Modules:         3 (    3 undocumented)
Classes:         1 (    0 undocumented)
Constants:       1 (    1 undocumented)
Attributes:      0 (    0 undocumented)
Methods:         2 (    2 undocumented)
 14.29% documented
 rake aborted!
 ArgumentError: wrong number of arguments (given 3, expected 4)
 tasks/cops_documentation.rake:156:in `new'
 tasks/cops_documentation.rake:156:in `references'
 tasks/cops_documentation.rake:24:in `cops_body'
 tasks/cops_documentation.rake:197:in `print_cop_with_doc'
 tasks/cops_documentation.rake:173:in `block in
 print_cops_of_department'
 tasks/cops_documentation.rake:172:in `each'
 tasks/cops_documentation.rake:172:in `print_cops_of_department'
 tasks/cops_documentation.rake:268:in `block in main'
 tasks/cops_documentation.rake:267:in `each'
 tasks/cops_documentation.rake:267:in `main'
 tasks/cops_documentation.rake:278:in `block in <top (required)>'
 /Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
 /Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
 Tasks: TOP => default => generate_cops_documentation
 (See full trace by running task with --trace)
 ```
renawatson68 added a commit to renawatson68/performance-develop-rubyonrails that referenced this pull request Sep 23, 2022
Follow up rubocop/rubocop#7295.

This commit fixes the following error.

```console
% bundle exec rake
The `SafeMode` option will be removed in `RuboCop` 0.76. Please update
`rubocop-performance` to 1.15.0 or higher.
Files:          25
Modules:         3 (    3 undocumented)
Classes:        26 (    0 undocumented)
Constants:      50 (   45 undocumented)
Attributes:      0 (    0 undocumented)
Methods:        53 (   47 undocumented)
 28.03% documented
rake aborted!
ArgumentError: wrong number of arguments (given 3, expected 4)
tasks/cops_documentation.rake:154:in `new'
tasks/cops_documentation.rake:154:in `references'
tasks/cops_documentation.rake:24:in `cops_body'
tasks/cops_documentation.rake:197:in `print_cop_with_doc'
tasks/cops_documentation.rake:173:in `block in print_cops_of_department'
tasks/cops_documentation.rake:172:in `each'
tasks/cops_documentation.rake:172:in `print_cops_of_department'
tasks/cops_documentation.rake:268:in `block in main'
tasks/cops_documentation.rake:267:in `each'
tasks/cops_documentation.rake:267:in `main'
tasks/cops_documentation.rake:278:in `block in <top (required)>'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
Tasks: TOP => default => generate_cops_documentation
(See full trace by running task with --trace)
```
richardstewart0213 added a commit to richardstewart0213/performance-build-Performance-optimization-analysis- that referenced this pull request Nov 4, 2022
Follow up rubocop/rubocop#7295.

This commit fixes the following error.

```console
% bundle exec rake
The `SafeMode` option will be removed in `RuboCop` 0.76. Please update
`rubocop-performance` to 1.15.0 or higher.
Files:          25
Modules:         3 (    3 undocumented)
Classes:        26 (    0 undocumented)
Constants:      50 (   45 undocumented)
Attributes:      0 (    0 undocumented)
Methods:        53 (   47 undocumented)
 28.03% documented
rake aborted!
ArgumentError: wrong number of arguments (given 3, expected 4)
tasks/cops_documentation.rake:154:in `new'
tasks/cops_documentation.rake:154:in `references'
tasks/cops_documentation.rake:24:in `cops_body'
tasks/cops_documentation.rake:197:in `print_cop_with_doc'
tasks/cops_documentation.rake:173:in `block in print_cops_of_department'
tasks/cops_documentation.rake:172:in `each'
tasks/cops_documentation.rake:172:in `print_cops_of_department'
tasks/cops_documentation.rake:268:in `block in main'
tasks/cops_documentation.rake:267:in `each'
tasks/cops_documentation.rake:267:in `main'
tasks/cops_documentation.rake:278:in `block in <top (required)>'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
Tasks: TOP => default => generate_cops_documentation
(See full trace by running task with --trace)
```
Cute0110 added a commit to Cute0110/Rubocop-Performance that referenced this pull request Sep 28, 2023
Follow up rubocop/rubocop#7295.

This commit fixes the following error.

```console
% bundle exec rake
The `SafeMode` option will be removed in `RuboCop` 0.76. Please update
`rubocop-performance` to 1.15.0 or higher.
Files:          25
Modules:         3 (    3 undocumented)
Classes:        26 (    0 undocumented)
Constants:      50 (   45 undocumented)
Attributes:      0 (    0 undocumented)
Methods:        53 (   47 undocumented)
 28.03% documented
rake aborted!
ArgumentError: wrong number of arguments (given 3, expected 4)
tasks/cops_documentation.rake:154:in `new'
tasks/cops_documentation.rake:154:in `references'
tasks/cops_documentation.rake:24:in `cops_body'
tasks/cops_documentation.rake:197:in `print_cop_with_doc'
tasks/cops_documentation.rake:173:in `block in print_cops_of_department'
tasks/cops_documentation.rake:172:in `each'
tasks/cops_documentation.rake:172:in `print_cops_of_department'
tasks/cops_documentation.rake:268:in `block in main'
tasks/cops_documentation.rake:267:in `each'
tasks/cops_documentation.rake:267:in `main'
tasks/cops_documentation.rake:278:in `block in <top (required)>'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
Tasks: TOP => default => generate_cops_documentation
(See full trace by running task with --trace)
```
SerhiiMisiura added a commit to SerhiiMisiura/Rubocop-Performance that referenced this pull request Oct 5, 2023
Follow up rubocop/rubocop#7295.

This commit fixes the following error.

```console
% bundle exec rake
The `SafeMode` option will be removed in `RuboCop` 0.76. Please update
`rubocop-performance` to 1.15.0 or higher.
Files:          25
Modules:         3 (    3 undocumented)
Classes:        26 (    0 undocumented)
Constants:      50 (   45 undocumented)
Attributes:      0 (    0 undocumented)
Methods:        53 (   47 undocumented)
 28.03% documented
rake aborted!
ArgumentError: wrong number of arguments (given 3, expected 4)
tasks/cops_documentation.rake:154:in `new'
tasks/cops_documentation.rake:154:in `references'
tasks/cops_documentation.rake:24:in `cops_body'
tasks/cops_documentation.rake:197:in `print_cop_with_doc'
tasks/cops_documentation.rake:173:in `block in print_cops_of_department'
tasks/cops_documentation.rake:172:in `each'
tasks/cops_documentation.rake:172:in `print_cops_of_department'
tasks/cops_documentation.rake:268:in `block in main'
tasks/cops_documentation.rake:267:in `each'
tasks/cops_documentation.rake:267:in `main'
tasks/cops_documentation.rake:278:in `block in <top (required)>'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/koic/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
Tasks: TOP => default => generate_cops_documentation
(See full trace by running task with --trace)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

StyleGuideBaseURL is broken for rubocop cops
2 participants