Skip to content

Commit

Permalink
Add CLI switches for debug mode
Browse files Browse the repository at this point in the history
When a cop raises an exception, RuboCop will swallow it and tell
you to use debug mode if you want to see the backtrace.

I needed to add this in order to be able to debug the problems
which lead to PR #5.
  • Loading branch information
leoarnold authored and r7kamura committed Jan 9, 2023
1 parent a95f7ad commit bf6de34
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/templatecop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def call

offenses = Runner.new(
auto_correct: options[:auto_correct],
debug: options[:debug],
file_paths: file_paths,
formatter: formatter,
rubocop_config: rubocop_config,
Expand All @@ -96,6 +97,9 @@ def parse_options!
parser.on('--[no-]color', 'Force color output on or off.') do |value|
options[:color] = value
end
parser.on('-d', '--debug', 'Display debug info.') do |value|
options[:debug] = value
end
parser.parse!(@argv)
options
end
Expand Down
11 changes: 10 additions & 1 deletion lib/templatecop/ruby_offense_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ module Templatecop
# Collect RuboCop offenses from Ruby code.
class RubyOffenseCollector
# @param [Boolean] auto_correct
# @param [Boolean] debug
# @param [String] file_path
# @param [RuboCop::Config] rubocop_config
# @param [String] source
def initialize(auto_correct:, file_path:, rubocop_config:, source:)
def initialize(
auto_correct:,
debug: false,
file_path:,
rubocop_config:,
source:
)
@auto_correct = auto_correct
@debug = debug
@file_path = file_path
@rubocop_config = rubocop_config
@source = source
Expand Down Expand Up @@ -57,6 +65,7 @@ def rubocop_team
registry,
@rubocop_config,
auto_correct: @auto_correct,
debug: @debug,
display_cop_names: true,
extra_details: true,
stdin: ''
Expand Down
15 changes: 13 additions & 2 deletions lib/templatecop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
require 'stringio'

module Templatecop
# Run investigation and auto-correcttion.
# Run investigation and auto-correction.
class Runner
# @param [Boolean] auto_correct
# @param [Boolean] debug
# @param [Array<String>] file_paths
# @param [Object] formatter
# @param [RuboCop::Config] rubocop_config
# @param [#call] ruby_extractor
def initialize(
auto_correct:,
debug: false,
file_paths:,
formatter:,
rubocop_config:,
ruby_extractor:
)
@auto_correct = auto_correct
@debug = debug
@file_paths = file_paths
@formatter = formatter
@rubocop_config = rubocop_config
Expand Down Expand Up @@ -54,9 +57,16 @@ def correct(file_path:, offenses:, source:)
# @param [String] rubocop_config
# @param [String] source
# @return [Array<Templatecop::Offense>]
def investigate(auto_correct:, file_path:, rubocop_config:, source:)
def investigate(
auto_correct:,
debug:,
file_path:,
rubocop_config:,
source:
)
TemplateOffenseCollector.new(
auto_correct: auto_correct,
debug: debug,
file_path: file_path,
rubocop_config: rubocop_config,
ruby_extractor: @ruby_extractor,
Expand All @@ -73,6 +83,7 @@ def run_in_parallel
source = ::File.read(file_path)
offenses = investigate(
auto_correct: @auto_correct,
debug: @debug,
file_path: file_path,
rubocop_config: @rubocop_config,
source: source
Expand Down
4 changes: 4 additions & 0 deletions lib/templatecop/template_offense_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ module Templatecop
# Collect RuboCop offenses from Template code.
class TemplateOffenseCollector
# @param [Boolean] auto_correct
# @param [Boolean] debug
# @param [String] file_path Template file path
# @param [RuboCop::Config] rubocop_config
# @param [#call] ruby_extractor
# @param [String] source Template code
def initialize(
auto_correct:,
debug:,
file_path:,
rubocop_config:,
ruby_extractor:,
source:
)
@auto_correct = auto_correct
@debug = debug
@file_path = file_path
@rubocop_config = rubocop_config
@ruby_extractor = ruby_extractor
Expand All @@ -27,6 +30,7 @@ def call
snippets.flat_map do |snippet|
RubyOffenseCollector.new(
auto_correct: @auto_correct,
debug: @debug,
file_path: @file_path,
rubocop_config: @rubocop_config,
source: snippet[:code]
Expand Down

0 comments on commit bf6de34

Please sign in to comment.