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

Conversation

dantevvp
Copy link
Contributor

Related issue: #1687

Currently, Reek uses Parser::CurrentRuby to determine which Ruby version to parse when analyzing code, so it always chooses the installed Ruby version's parser. Under the Code Climate engine, this means that it will always choose the parser for Ruby version 2.6, because that's the version that is installed in the Dockerfile.

To let users choose what Ruby version they want to use reek under, expose a new config attribute "target_ruby_version". This overrides the Reek::Source::SourceCode.default_parser method to use whichever parser version the user specifies.

Example

With .codeclimate.yml looking like this:

engines:
  reek:
    enabled: true
    config:
      target_ruby_version: "2.6.8"

The logic will use the first two digits of the chosen version to determine which parser version to use. In this case, it would be Parser::Ruby26

Another example:

engines:
  reek:
    enabled: true
    config:
      target_ruby_version: "3.1"

In this case, we will choose Parser::Ruby31 as the parser, which supports newer syntax.

If the user specifies an unknown version or no version at all, it will default to Parser::CurrentRuby, which is the previous behavior.

@mvz
Copy link
Collaborator

mvz commented Jan 12, 2023

Thanks, @dantevvp, I'll try to do a review this weekend.

Copy link
Collaborator

@mvz mvz left a comment

Choose a reason for hiding this comment

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

This seems fine. I have just two suggestions for improvement.

module Reek
module Source
# Override Reek::Source::SourceCode to use a parser version specified by the user
class SourceCode
Copy link
Collaborator

Choose a reason for hiding this comment

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

The other overrides in this file are done using Module#prepend. I think it makes sense to do that here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

@mvz
Copy link
Collaborator

mvz commented Jan 15, 2023

@dantevvp I'm assuming you will test that this works in the context of Code Climate yourself 🙂

@dantevvp
Copy link
Contributor Author

@dantevvp I'm assuming you will test that this works in the context of Code Climate yourself 🙂

Correct! I've tested this with 3.1 syntax and old syntax. It defaults to Parser::CurrentRuby when an invalid/null version number is specified, and it uses the correct specified parser each time.

@dantevvp dantevvp requested a review from mvz January 17, 2023 14:11
@troessner
Copy link
Owner

Looks good to me! Can you please squash to one commit @dantevvp and then.. lets release a new version @mvz ?

@mvz
Copy link
Collaborator

mvz commented Jan 17, 2023

I'm not sure if we need to release a new version. Doesn't Code Climate run reek straight from the repository using the Dockerfile? @dantevvp would know this ...

Copy link
Collaborator

@mvz mvz left a comment

Choose a reason for hiding this comment

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

All good as soon as rubocop is happy 😉

To let users choose what Ruby version they want to use reek under,
expose a new config attribute "target_ruby_version". This overrides
the Reek::Source::SourceCode.default_parser method to use whichever
parser version the user specifies

Parse target_ruby_version with Gem::Version

Co-authored-by: Matijs van Zuijlen <[email protected]>

Use prepend to override SourceCode class methods

remove trailing whitespace
@dantevvp dantevvp force-pushed the support_ruby_3_1_code_climate_engine branch from 20ae1f2 to b74920e Compare January 18, 2023 14:02
@dantevvp
Copy link
Contributor Author

dantevvp commented Jan 18, 2023

@troessner @mvz Commits squashed and Rubucop offense resolved!

I'm not sure if we need to release a new version. Doesn't Code Climate run reek straight from the repository using the Dockerfile? @dantevvp would know this ...

This is correct! We use the commit sha of the repository so a version release would not be necessary to get the latest Reek code.

@mvz mvz merged commit d9b0fb4 into troessner:master Jan 24, 2023
@mvz
Copy link
Collaborator

mvz commented Jan 24, 2023

Thanks, @dantevvp!

bannable added a commit to bannable/paseto that referenced this pull request Apr 6, 2024
This plugin has been failing for years because Code Climate insists on running reek under 2.6 with parser 3.1.

Today, I found troessner/reek#1694, which shows an undocumented way to set a target version for reek. This commit sets a target version of 3.1 and fixes some of the "new" findings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants