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

Code Climate: add config "target_ruby_version" to support different versions of Ruby syntax #1694

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions bin/code_climate_reek
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative '../lib/reek'
require_relative '../lib/reek/cli/application'
require_relative '../lib/reek/report/code_climate'
Reek::CLI::Silencer.silently { require 'parser/current' }

# Map input coming from CodeClimate to Reek.
class CodeClimateToReek
Expand All @@ -16,6 +17,8 @@ class CodeClimateToReek
'--failure-exit-code', '0',
'--success-exit-code', '0'
].freeze
CUSTOM_CONFIG_KEY = 'config'
CUSTOM_CONFIG_TARGET_RUBY_VERSION_KEY = 'target_ruby_version'

attr_reader :configuration_file_path, :include_paths_key, :include_paths_default

Expand All @@ -31,6 +34,10 @@ class CodeClimateToReek
include_paths + ENGINE_CONFIGURATION
end

def target_ruby_version
config.dig(CUSTOM_CONFIG_KEY, CUSTOM_CONFIG_TARGET_RUBY_VERSION_KEY)
end

private

def configuration_file_exists?
Expand All @@ -45,11 +52,14 @@ class CodeClimateToReek
# ]
# }
def include_paths
config.fetch include_paths_key, include_paths_default
end

def config
if configuration_file_exists?
config = JSON.parse File.read(configuration_file_path)
config.fetch include_paths_key, include_paths_default
JSON.parse File.read(configuration_file_path)
else
include_paths_default
{}
end
end
end
Expand All @@ -61,7 +71,46 @@ module ReportClassOverride
end
end

# Override Reek::Source::SourceCode to use a parser version specified by the user
module SourceCodeOverride
# override self.default_parser method
def default_parser
parser_class.new(Reek::AST::Builder.new).tap do |parser|
diagnostics = parser.diagnostics
diagnostics.all_errors_are_fatal = true
diagnostics.ignore_warnings = true
end
end

# config.json file will look like this:
# {
# "include_paths":[
# "lib",
# "spec"
# ],
# "config": {
# "target_ruby_version": "3.1.0"
# }
# }
def parser_class
# convert an X.Y.Z version number to an XY two digit number
requested_version = CodeClimateToReek.new.target_ruby_version
return Parser::CurrentRuby if requested_version.nil?

version_number = Gem::Version.new(requested_version).segments[0..1].join

begin
Reek::CLI::Silencer.silently { require "parser/ruby#{version_number}" }
Module.const_get("Parser::Ruby#{version_number}")
rescue LoadError, NameError
# use Parser::CurrentRuby when an invalid version number is provided
Parser::CurrentRuby
end
end
end

Reek::CLI::Command::ReportCommand.prepend ReportClassOverride
Reek::Source::SourceCode.singleton_class.prepend SourceCodeOverride

application = Reek::CLI::Application.new(CodeClimateToReek.new.cli_arguments)

Expand Down