-
Notifications
You must be signed in to change notification settings - Fork 121
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 RuboCop runner composable and capture any stdout message from it #275
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||||||||||
# typed: strict | ||||||||||||||
# frozen_string_literal: true | ||||||||||||||
|
||||||||||||||
begin | ||||||||||||||
require "rubocop" | ||||||||||||||
rescue LoadError | ||||||||||||||
return | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
require "cgi" | ||||||||||||||
require "singleton" | ||||||||||||||
|
||||||||||||||
module RubyLsp | ||||||||||||||
module Requests | ||||||||||||||
module Support | ||||||||||||||
# :nodoc: | ||||||||||||||
class RuboCopRunner < RuboCop::Runner | ||||||||||||||
extend T::Sig | ||||||||||||||
|
||||||||||||||
sig { returns(T::Array[RuboCop::Cop::Offense]) } | ||||||||||||||
attr_reader :offenses | ||||||||||||||
|
||||||||||||||
DEFAULT_ARGS = T.let([ | ||||||||||||||
"--force-exclusion", | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume we dropped the stderr option because we're now capturing stdout, but should we keep the formatter to reduce the amount of prints from RuboCop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we can still keep things less chatty overall. I'll reinstate. |
||||||||||||||
].freeze, T::Array[String]) | ||||||||||||||
|
||||||||||||||
sig { params(args: String).void } | ||||||||||||||
def initialize(*args) | ||||||||||||||
@options = T.let({}, T::Hash[Symbol, T.untyped]) | ||||||||||||||
@offenses = T.let([], T::Array[RuboCop::Cop::Offense]) | ||||||||||||||
@errors = T.let([], T::Array[String]) | ||||||||||||||
@warnings = T.let([], T::Array[String]) | ||||||||||||||
|
||||||||||||||
args += DEFAULT_ARGS | ||||||||||||||
rubocop_options = ::RuboCop::Options.new.parse(args).first | ||||||||||||||
super(rubocop_options, ::RuboCop::ConfigStore.new) | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
sig { params(path: String, contents: String).void } | ||||||||||||||
def run(path, contents) | ||||||||||||||
@errors = [] | ||||||||||||||
@warnings = [] | ||||||||||||||
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't recycle runners, but only use a single instance, errors and warnings need to cleared at the top of every run so that they don't keep building up run over run. |
||||||||||||||
@offenses = [] | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was what I tried first, but it didn't work since it is a frozen array. |
||||||||||||||
@options[:stdin] = contents | ||||||||||||||
capture_output { super([path]) } | ||||||||||||||
display_handled_errors | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
sig { returns(String) } | ||||||||||||||
def formatted_source | ||||||||||||||
@options[:stdin] | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
private | ||||||||||||||
|
||||||||||||||
sig { void } | ||||||||||||||
def display_handled_errors | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? What type of error is this trying to display? If we don't pop up a dialogue, I doubt anyone will actually see it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so that people can see and/or report problems that any formatting/diagnostic operation has encountered. I feel like this is needed more for troubleshooting than making people aware. After all, there is no action to be taken on any of these, since they are already handled. This is what the output looks like:
|
||||||||||||||
return if errors.empty? | ||||||||||||||
|
||||||||||||||
$stderr.puts "[RuboCop] Encountered and handled errors:" | ||||||||||||||
errors.uniq.each do |error| | ||||||||||||||
$stderr.puts "[RuboCop] - #{error}" | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
sig { params(_file: String, offenses: T::Array[RuboCop::Cop::Offense]).void } | ||||||||||||||
def file_finished(_file, offenses) | ||||||||||||||
@offenses = offenses | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
sig { params(block: T.proc.void).void } | ||||||||||||||
def capture_output(&block) | ||||||||||||||
original_verbosity = $VERBOSE | ||||||||||||||
orig_stdout = $stdout | ||||||||||||||
orig_stderr = $stderr | ||||||||||||||
|
||||||||||||||
$VERBOSE = nil | ||||||||||||||
$stderr = StringIO.new | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How hard/bad would it be to monkey patch the classes from Rubocop that still print so we noop |
||||||||||||||
$stdout = StringIO.new | ||||||||||||||
|
||||||||||||||
block.call | ||||||||||||||
ensure | ||||||||||||||
$stdout = orig_stdout | ||||||||||||||
$stderr = orig_stderr | ||||||||||||||
$VERBOSE = original_verbosity | ||||||||||||||
Comment on lines
+83
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit,
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was explicitly reverting changes in the reverse order :) |
||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure there's a lot of value in keeping this method around after the refactor. What do you think about moving this to the
run
body?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can do that!