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

[Fix #107] Fix style guide URLs #109

Merged
merged 1 commit into from
Aug 26, 2019
Merged

Conversation

koic
Copy link
Member

@koic koic commented Aug 16, 2019

Fixes #107.

This PR fixes style guide URLs when specifying rubocop --display-style-guide option.

The following is an execution using RuboCop Rails.

% bundle exec rubocop --display-style-guide --only Style/StringLiterals
Inspecting 2 files
C.

Offenses:

Gemfile:3:8: C: Style/StringLiterals: Prefer single-quoted strings when
you don't need string interpolation or special
symbols. (https://rails.rubystyle.guide#consistent-string-literals)
source "https://rubygems.org"
       ^^^^^^^^^^^^^^^^^^^^^^

2 files inspected, 1 offense detected

The correct URL is https://rubystyle.guide#consistent-string-literals instead of https://rails.rubystyle.guide#consistent-string-literals.


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.

@bbatsov
Copy link
Contributor

bbatsov commented Aug 16, 2019

Hmm, maybe we should come up with a way for plugins to provide their own base url as well, so we can avoid this repetition?

@koic
Copy link
Member Author

koic commented Aug 17, 2019

My explanation was lacking. This PR is a workaround patch.
I’m developing a plugin of base URL to RuboCop core. It takes a little time.

So I prepared this PR that workaround patch because release cycle is different between RuboCop core and RuboCop Rails. This patch can be released independently of RuboCop core.

@bbatsov
Copy link
Contributor

bbatsov commented Aug 17, 2019

Understood. Thanks for the extra details!

koic added a commit to koic/rubocop that referenced this pull request 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.

```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.
Fixes rubocop#107.

This PR fixes style guide URLs when specifying
`rubocop --display-style-guide` option.

The following is an execution using RuboCop Rails.

```console
% bundle exec rubocop --display-style-guide --only Style/StringLiterals
Inspecting 2 files
C.

Offenses:

Gemfile:3:8: C: Style/StringLiterals: Prefer single-quoted strings when
you don't need string interpolation or special
symbols. (https://rails.rubystyle.guide#consistent-string-literals)
source "https://rubygems.org"
       ^^^^^^^^^^^^^^^^^^^^^^

2 files inspected, 1 offense detected
```

The correct URL is `https://rubystyle.guide#consistent-string-literals`
instead of `https://rails.rubystyle.guide#consistent-string-literals`.
@koic koic force-pushed the fix_a_style_guild_url branch from 9b802df to 0092e77 Compare August 19, 2019 07:43
bbatsov pushed a commit to rubocop/rubocop that referenced this pull request 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.

```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 merged commit e0bfc5e into rubocop:master Aug 26, 2019
@koic koic deleted the fix_a_style_guild_url branch August 26, 2019 05:10
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