-
Notifications
You must be signed in to change notification settings - Fork 25
add count_threshold option #142
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
add count_threshold option #142
Conversation
fc38551
to
b39d39f
Compare
@@ -51,6 +51,7 @@ def report | |||
violations = new_violations(issue) | |||
|
|||
violations.each do |violation| | |||
next if (violation.occurrences + 1) < language_strategy.count_threshold |
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.
here's where the magic happens
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.
This PR looks great to me! One minor style suggestion and I'm also wondering if there's a spec we can add that exercises the change in the Reporter
class.
Thanks!
@@ -30,6 +30,11 @@ def mass_threshold_for(language) | |||
end | |||
end | |||
|
|||
def count_threshold_for(language) | |||
threshold = fetch_language(language).fetch("count_threshold", nil) | |||
threshold ? threshold.to_i : threshold |
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.
This ternary conditional is a bit difficult to read when the switch value is also the default return.
Given the similarity to the #mass_threshold_for
method above, it might make sense to consolidate:
threshold_config_for(:mass, "ruby")
threshold_config_for(:count, "ruby")
def threshold_config_for(option, language)
threshold = fetch_language(language)["#{option}_threshold"]
if threshold
threshold.to_i
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.
I think (and my feelings on this are related to the origins of this PR), that there's no point in abstracting these two methods until there's 3-4 similar config options (and I really do think there won't be any more)
You could also even do
def count_threshold_for(language)
fetch_language(language).dig("count_threshold")&.to_i
end
if we wanted to get really crazy (well, I guess in ruby 2.3 only)
I'll switch it back to the old if threshold
syntax
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.
Good point! 👍
I tried to update the spec, but it looks pretty involved because of where I put the code. Maybe I'll try again next week if I have time |
@devvmh I'm good rolling with this as is. Going to do some local testing off of this branch before merging. |
Great, thanks! |
Hey @devvmh, I've tested this locally and it works well! One question I have now that I've thought about it a bit more is whether this option should be configured one level up instead of nested under a particular language. Do you think it's likely that users will want to specify different count thresholds per language? The I'm thinking that it might be better to support this option as a sibling to engines:
duplication:
enabled: true
config:
count_threshold: 3
languages:
- ruby What do you think? |
Yeah that's a good question. I think for my particular case I would do the same for ruby and javascript. I do think that it's more useful to be per-language than not though, don't you? Like, it's not harder to configure per-language (just 4 extra lines of YAML worst case). If you'd like me to change it, just say and I can do that |
Just took a quick poll internally, and the consensus was basically: So I think it would be ideal if we supported the configuration option at the higher position, but also allow language-specific overrides. engines:
duplication:
enabled: true
config:
count_threshold: 3
languages:
ruby:
python:
javascript:
count_threshold: 5 Does that sound good to you? |
Yeah! brilliant. I'll tty and implement that in tte jext few days On Sat, Oct 29, 2016, 1:03 AM Devon Blandin notifications@github.com
|
@@ -30,6 +30,15 @@ def mass_threshold_for(language) | |||
end | |||
end | |||
|
|||
def count_threshold_for(language) | |||
threshold = fetch_language(language).fetch("count_threshold", nil) |
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.
What do you think about pushing the default count_threshold
value into the EngineConfig class since languages don't have specific defaults?
module CC
module Engine
module Analyzers
class EngineConfig
DEFAULT_COUNT_THRESHOLD = 2
def count_threshold_for(language)
fetch_language(language)["count_threshold"] || count_threshold || DEFAULT_COUNT_THRESHOLD
end
private
def count_threshold
config["count_threshold"]
end
module CC
module Engine
module Analyzers
class Base
def count_threshold
engine_config.count_threshold_for(self.class::LANGUAGE)
end
Phew, OK, that's finally updated. |
@devvmh Awesome! The code looks great 👍 Last thing: Would you mind adding an example or two to spec/cc/engine/analyzers/engine_config_spec.rb to cover |
5289dbb
to
5adf9d3
Compare
adds the option to only check duplication if the code is found in more than just two locations.
5adf9d3
to
96cd4ef
Compare
OK, updated. Should I squash this into one commit? |
@devvmh Perfect 👌
No need. I'm going use GitHub's handy "Squash and merge" button. Thanks for the contribution! 🎉 |
adds the option to only check duplication if the code is found in more than just two locations.
fixes #131