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

add option to show cops disabed by inline comments #900

Merged
merged 1 commit into from Mar 29, 2014
Merged

add option to show cops disabed by inline comments #900

merged 1 commit into from Mar 29, 2014

Conversation

fshowalter
Copy link
Contributor

Sometimes its tempting for developers, especially juniors who may have trouble understanding some of RuboCop's warnings, to abuse the # rubocop:disable functionality. This pull request adds an option (-x or --show-disabled) that, after a run, prints a list of all the files, line ranges and corresponding cops that were ignored during the run due to # rubocop:disable comments.

@jonas054
Copy link
Collaborator

You've added reporting outside of the formatter classes, and I see some problems with that. For example, you can' get this information in JSON format (--format json). Perhaps a better solution would be to implement this functionality in a new cop, disabled by default.

@fshowalter
Copy link
Contributor Author

Yeah, I thought about adding formatter support, but was hesitant since those seem designed for offenses. I don't think this should go in as a cop, as it's not really an offense (and how would you disable it inline?).

Agree though, that this needs to be added to the JSON formatter (think it would make sense in the summary object) and I'll append with support for it tonight.

As for the rest of the formatters, they don't seem to support summary stats so maybe this info just isn't part of those formatters directly. In the current implementation it should still be printed to the output. (IE the fuubar formatter would do the nice progress bar =======> then afterwards it would print the ignored cops). Workable compromise?

@jonas054
Copy link
Collaborator

Yeah, if that cop is disabled inline there's a risk that RuboCop implodes and erases itself from disk. I should have thought of that. 😉

I hope @bbatsov and @yujinakayama can offer their opinions on the matter. I still think a cop is a less intrusive solution.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 18, 2014

I'm not overly fond of the cop idea, because this cop should be disabled by default and currently disabled cops can't be run with --only CopName (which I think is a bug). If we change the --only behavior I might be like the cop idea better. Also this would be a cop that will have generally have to be run by itself as I cannot imagine always running it (unless you're fanatically devoted to perfect style).

Generally I like more the flag idea. I'm not sure this info belongs inside the formatters, maybe printing it by itself is OK. Of course, we have to hear @yujinakayama's point of view.

@yujinakayama
Copy link
Collaborator

An optional idea: When -x/--show-disabled is specified, RuboCop only finds and outputs the # rubocop:disabled comments without running cops.

I think this would solve the issue about mixed outputs and also allows you to just check the disabled lines without waiting for long running cop inspection.

What do you think?

@fshowalter
Copy link
Contributor Author

That could work, but I would imagine the most common use-case for this would be in a CI report in which case you'd run both of them together anyway. Since the JSON formatter is the only one really effected by this (since the JSON is presumably parsed so the extra text at the end wouldn't work) why don't I just add support for that formatter?

@fshowalter
Copy link
Contributor Author

Okay, I moved the summary into the formatters. It does feel better in there, as just dumping the text at the end would potentially choke clients consuming the JSON formatter. That said, the formatters don't lend themselves to extension and I had alter the base formatter's signature like so:

# @api public
#
# Invoked after all files are inspected, or interrupted by user.
#
# @param inspected_files [Array(String)]
#   the inspected file paths.
#   This would be same as `target_files` passed to `#started`
#   unless RuboCop is interrupted by user.
# @param summary [Hash]
#   optional meta information about the completed run.
# @return [void]
def finished(inspected_files, summary = {})
end

Since this is a breaking change to the public API, maybe we can go with my previous commit and save this for v.19? That said, maybe consider adding a splat to the end of the base formatter methods to facilitate future extensions without breaking consumers? What's everyone think?

@@ -20,6 +20,11 @@ def cop_enabled_at_line?(cop, line_number)
disabled_line_ranges.none? { |range| range.include?(line_number) }
end

def disabled_line_ranges
disabled = @cop_disabled_line_ranges || {}
disabled.select { |key, value| value.any? }
Copy link
Collaborator

Choose a reason for hiding this comment

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

key -> _key

@yujinakayama It'd be nice if we checked for unused block arguments.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 22, 2014

I'm not sure I like the addition of the summary param. Perhaps @jonas054 and @yujinakayama would suggest a better approach to handle this (assuming with go with adding the info to formatters).

jonas054 added a commit to jonas054/rubocop that referenced this pull request Mar 22, 2014
It was said in rubocop#900 that it is a bug for --only to not enable the cop.

AllCops:
Excludes:
- vendor/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh? What's this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, (I bundle locally so without it the spec's try and analyze every gem's files). Will fix.

@jonas054
Copy link
Collaborator

I think this feature is a good idea, but I don't like how its addition requires so many changes in the core logic. A new cop is the way to go. We could do a little bit of hard-coding in comment_config.rb -- the only slightly ugly part of this solution -- to make sure this cop can't itself be disabled by comments. And we can still have a new option --show-disabled that runs the cop.

@yujinakayama
Copy link
Collaborator

but I don't like how its addition requires so many changes in the core logic.

I agree, the current implementation adds too much complexity for a feature.

Here's my idea I finally figured out:

  • Add new disabled_lines (not sure if the name should include _) formatter. This formatter reports the disabled lines at the final summary and can be used like the offenses formatter. (e.g. rubocop --format progress --format disabled_lines)
  • Pass the disabled line range hash to formatter's #file_started. As I mentioned about it in my initial formatter design, the second hash argument of #file_started is intended to be extended for like this case. So, we don't need to introduce formatter API change.
  • Just need to move the source parsing handling from Cop::Team#inspect_file to FileInspector#process_file (only the several lines) so that FileInspector can pass the disabled line hash to formatters.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 23, 2014

👍 for @yujinakayama's idea

@fshowalter
Copy link
Contributor Author

I like it. I'll make the changes tonight.

@fshowalter
Copy link
Contributor Author

Ok, here's a first-pass at @yujinakayama's suggestion. What's everyone think?

@@ -13,6 +13,7 @@ def initialize(cop_classes, config, options = nil)
@config = config
@options = options || { auto_correct: false, debug: false }
@errors = []
@cops_disabled_in_comments = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Will fix tonight.

@@ -102,8 +103,7 @@ def add_flags_with_optional_args(opts)

def add_boolean_flags(opts)
option(opts, '-d', '--debug', 'Display debug info.')
option(opts,
'-D', '--display-cop-names',
option(opts, '-D', '--display-cop-names',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please do not include unrelated changes to the commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, this was a holdover from the previous take which had an added option.

@fshowalter
Copy link
Contributor Author

Think I got all of @yujinakayama's suggestions in there, but I'm sure a second set of eyes will prove otherwise. 😏

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 27, 2014

The build is broken.

@fshowalter
Copy link
Contributor Author

Heh, jruby always seems to get me. I may need to do a push disabling the global rescue in cli.rb so Travis can show me the error, since I don't have jruby locally, but regardless, I'll get this resolved.

@@ -4,6 +4,7 @@

module Rubocop
# This class handles command line options.
# rubocop:disable ClassLength
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please increment the max class length in .rubocop-todo.yml rather than disabling.

@yujinakayama
Copy link
Collaborator

Looks good to me except the few comments and the build failure.

@bbatsov, @jonas054 Any other comments?

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 27, 2014

Nope, I have nothing to add to your comments.

@@ -17,4 +17,4 @@ CyclomaticComplexity:
# Offense count: 105
# Configuration parameters: CountComments.
MethodLength:
Max: 22
Max: 22
Copy link
Collaborator

Choose a reason for hiding this comment

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

So you've removed the last newline in the file? No big deal, but it does seem a bit odd.

@jonas054
Copy link
Collaborator

I only had one tiny remark. And I'm still moping over our choice of formatter rather than cop solution. 😄 No, that's alright.

@fshowalter
Copy link
Contributor Author

Think that fixes the rbx/jruby errors, @yujinakayama's catches, and even adds back in the missing line for @jonas054. 😄

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 28, 2014

@fshowalter Please, rebase this on top of the current master and we'll finally be good to go.

@fshowalter
Copy link
Contributor Author

Rebased. Last chance @jonas054 😉

@jonas054
Copy link
Collaborator

No, I've had my chances. 😄 Godspeed, young Showalter!

bbatsov added a commit that referenced this pull request Mar 29, 2014
…ments

add option to show cops disabed by inline comments
@bbatsov bbatsov merged commit b828661 into rubocop:master Mar 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants