Skip to content

Commit 3e41f95

Browse files
committed
Remove nonsense
1 parent fbacc2b commit 3e41f95

File tree

4 files changed

+33
-43
lines changed

4 files changed

+33
-43
lines changed

bin/rubocop-interactive

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,5 @@ OptionParser.new do |opts|
7777
end.parse!
7878

7979
# Start interactive session - loading screen shown before reading input
80-
if ARGV.any?
81-
RubocopInteractive.start_with_files!(ARGV, **options)
82-
else
83-
RubocopInteractive.start_with_stdin!(**options)
84-
end
80+
input = ARGV.any? ? ARGV : $stdin
81+
RubocopInteractive.start!(input, **options)

lib/rubocop_interactive.rb

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,33 @@
2020
module RubocopInteractive
2121
class Error < StandardError; end
2222

23-
def self.start_with_stdin!(confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false)
24-
ui = UI.new(confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses)
25-
ui.show_loading(source: :stdin)
26-
27-
json = $stdin.read
28-
29-
start!(json, ui: ui, confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses)
30-
end
31-
32-
def self.start_with_files!(files, confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false)
33-
ui = UI.new(confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses)
34-
ui.show_loading(source: :files, files: files)
35-
36-
json = run_rubocop_on_files(files)
37-
38-
start!(json, ui: ui, confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses)
39-
end
40-
41-
def self.start!(json, ui: nil, confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false)
23+
def self.start!(input, ui: nil, confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false)
24+
# Create UI if not provided
4225
ui ||= UI.new(confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses)
4326

44-
session = Session.new(json, ui: ui)
27+
# Convert input to JSON string based on type
28+
json_string = case input
29+
when Array
30+
# Array of file paths
31+
ui.show_loading(source: :files, files: input)
32+
run_rubocop_on_files(input)
33+
when Hash
34+
# Already parsed JSON - convert back to string
35+
JSON.generate(input)
36+
when String
37+
# Raw JSON string
38+
input
39+
else
40+
# IO-like object (IO, StringIO, File, etc) - must have #read method
41+
if input.respond_to?(:read)
42+
ui.show_loading(source: :stdin)
43+
input.read
44+
else
45+
raise ArgumentError, "input must be IO-like (with #read), Array (files), Hash (parsed JSON), or String (JSON)"
46+
end
47+
end
48+
49+
session = Session.new(json_string, ui: ui)
4550
stats = session.run
4651

4752
# Write keypress recording if enabled

test/e2e/calculator_test.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,23 +184,7 @@ def run_sample
184184
end
185185

186186
def run_rubocop_json
187-
require 'rubocop'
188-
require 'stringio'
189-
190-
output = StringIO.new
191-
old_stdout = $stdout
192-
old_stderr = $stderr
193-
$stdout = output
194-
$stderr = StringIO.new
195-
196-
begin
197-
cli = RuboCop::CLI.new
198-
cli.run(['--format', 'json', '--cache', 'false', @work_file])
199-
ensure
200-
$stdout = old_stdout
201-
$stderr = old_stderr
202-
end
203-
204-
JSON.parse(output.string)
187+
json_string = RubocopInteractive.run_rubocop_on_files([@work_file])
188+
JSON.parse(json_string)
205189
end
206190
end

test/support/fakes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def initialize(responses: [])
1010
@offenses_shown = []
1111
end
1212

13+
def show_loading(source:, files: nil)
14+
# no-op
15+
end
16+
1317
def show_summary(total:)
1418
# no-op
1519
end

0 commit comments

Comments
 (0)