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

Fix rubocop offenses #1350

Merged
merged 11 commits into from
Nov 3, 2020
Merged

Conversation

KapilSachdev
Copy link
Collaborator

@KapilSachdev KapilSachdev commented Sep 4, 2020

Helps move forward with #1345
Cop fixes are broken into multiple commits to ease reviewing.

Checklist of Fixed Cops:
  • Bundler/OrderedGems
  • Layout/ArgumentAlignment
  • Layout/CaseIndentation
  • Layout/ClosingHeredocIndentation
  • Layout/ClosingParenthesisIndentation
  • Layout/DotPosition
  • Layout/ElseAlignment
  • Layout/EmptyLineAfterGuardClause
  • Layout/EmptyLineAfterMagicComment
  • Layout/EmptyLines
  • Layout/EmptyLinesAroundBlockBody
  • Layout/EmptyLinesAroundModuleBody
  • Layout/EndAlignment
  • Layout/HashAlignment
  • Layout/IndentationConsistency
  • Layout/IndentationWidth
  • Layout/LeadingCommentSpace
  • Layout/LineLength
  • Layout/MultilineBlockLayout
  • Layout/MultilineMethodCallBraceLayout
  • Layout/MultilineOperationIndentation
  • Layout/SpaceAfterComma
  • Layout/SpaceAroundEqualsInParameterDefault
  • Layout/SpaceAroundOperators
  • Layout/SpaceBeforeComment
  • Layout/SpaceInLambdaLiteral
  • Layout/SpaceInsideArrayLiteralBrackets
  • Layout/SpaceInsideHashLiteralBraces
  • Layout/SpaceInsidePercentLiteralDelimiters
  • Layout/SpaceInsideRangeLiteral
  • Layout/SpaceInsideReferenceBrackets
  • Layout/TrailingEmptyLines
  • Layout/TrailingWhitespace
  • Lint/AmbiguousBlockAssociation
  • Lint/MissingCopEnableDirective
  • Lint/NestedMethodDefinition
  • Lint/RedundantCopDisableDirective
  • Lint/RedundantRequireStatement
  • Lint/UnusedBlockArgument
  • Lint/UnusedMethodArgument
  • Metrics/ModuleLength
  • Naming/MemoizedInstanceVariableName
  • Naming/RescuedExceptionsVariableName
  • Rails/Output
  • Rails/Presence
  • Security/Eval
  • Security/Open
  • Style/ClassCheck
  • Style/CollectionMethods
  • Style/ConditionalAssignment
  • Style/EmptyMethod
  • Style/EvalWithLocation
  • Style/ExpandPathArguments
  • Style/FormatStringToken
  • Style/HashSyntax
  • Style/IfInsideElse
  • Style/InverseMethods
  • Style/MutableConstant
  • Style/ParallelAssignment
  • Style/RedundantBegin
  • Style/RedundantCondition
  • Style/RedundantInterpolation
  • Style/RedundantSelf
  • Style/RedundantSort
  • Style/RescueStandardError
  • Style/SafeNavigation
  • Style/StringLiterals
  • Style/StringLiteralsInInterpolation
  • Style/SymbolProc
  • Style/TrailingCommaInArguments (questionable)
  • Style/TrailingCommaInArrayLiteral
  • Style/TrailingCommaInHashLiteral

- Sort cops alphabetically in .rubocop.yml
- Fix Style/HashSyntax Offenses
- Satisfies Style/TrailingCommaInArguments, 
Style/TrailingCommaInArrayLiteral and Style/TrailingCommaInHashLiteral
- Layout/EmptyLines
- Layout/EmptyLineAfterGuardClause
- Layout/TrailingEmptyLines
- Layout/EmptyLinesAroundBlockBody
- Layout/EmptyLinesAroundModuleBody
- Layout/EmptyLineAfterMagicComment
- Layout/SpaceInsidePercentLiteralDelimiters
- Layout/SpaceAroundEqualsInParameterDefault
- Layout/SpaceInsideArrayLiteralBrackets
- Layout/LeadingCommentSpace
- Layout/SpaceBeforeComment
- Layout/SpaceAroundOperators
- Layout/SpaceInsideRangeLiteral
- Layout/SpaceInsideReferenceBrackets
- Layout/SpaceInLambdaLiteral
- Layout/SpaceAfterComma
- Layout/SpaceInsideHashLiteralBraces
- Layout/TrailingWhitespace
- Layout/ArgumentAlignment
- Layout/HashAlignment
- Layout/DotPosition
- Layout/IndentationWidth
- Layout/EndAlignment
- Layout/MultilineOperationIndentation
- Layout/IndentationConsistency
- Layout/ClosingHeredocIndentation
- Layout/MultilineMethodCallBrace- Layout
- Layout/ClosingParenthesisIndentation
@KapilSachdev
Copy link
Collaborator Author

@mcmire do you prefer multiple PR's or a single one for all?

@KapilSachdev KapilSachdev changed the title Fix rubocop offenses [ci skip] Fix rubocop offenses Sep 6, 2020
Updated Layout/LineLength cop to Max 120 lenght and ignore everything 
inside specs/**/*

Added rules for below cops in .rubocop.yml
Lint/AmbiguousBlockAssociation
Naming/HeredocDelimiterNaming
Rails/SkipsModelValidations
Style/FormatStringToken


Fixed below mentioned cops:
- Layout/CaseIndentation
- Layout/DotPosition
- Layout/ElseAlignment
- Layout/IndentationWidth
- Layout/LineLength
- Layout/MultilineBlockLayout
- Layout/MultilineOperationIndentation
- Lint/AmbiguousBlockAssociation
- Lint/MissingCopEnableDirective
- Lint/NestedMethodDefinition
- Lint/RedundantCopDisableDirective
- Lint/RedundantRequireStatement
- Lint/UnusedBlockArgument
- Lint/UnusedMethodArgument
- Metrics/ModuleLength
- Naming/MemoizedInstanceVariableName
- Naming/RescuedExceptionsVariableName
- Rails/Output
- Rails/Presence
- Security/Eval
- Security/Open
- Style/ClassCheck
- Style/CollectionMethods
- Style/ConditionalAssignment
- Style/EvalWithLocation
- Style/FormatStringToken
- Style/InverseMethods
- Style/MutableConstant
- Style/ParallelAssignment
- Style/RedundantBegin
- Style/RedundantCondition
- Style/RedundantInterpolation
- Style/RedundantSelf
- Style/RedundantSort
- Style/RescueStandardError
- Style/SafeNavigation
- Style/StringLiteralsInInterpolation
- Style/SymbolProc
@KapilSachdev
Copy link
Collaborator Author

KapilSachdev commented Sep 6, 2020

Pending:

Offense Count Cop
9 Metrics/AbcSize
8 Metrics/CyclomaticComplexity
7 Metrics/MethodLength
4 Metrics/BlockNesting
3 Metrics/PerceivedComplexity

@mcmire How do you want to approach these? Should they be disabled?

Copy link
Contributor

@utkarsh2102 utkarsh2102 left a comment

Choose a reason for hiding this comment

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

Hey,

Nice work!
Maybe just have:

Style/StringLiterals:
  Enabled: true
  EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
  Enabled: true
  EnforcedStyle: double_quotes

It's better to use double_quotes and also this codebase uses it anyway!

Copy link
Collaborator

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Did a pass-through of the whole PR. In hindsight perhaps it would have been better as a succession of PRs, but we've already come this far, so it's not a big deal. I know I left a lot of comments, but let me see if I can summarize them:

  • Can we stick to an 80-character line length? I know that seems small in the days of large screens, but that's what was being followed before (and on a laptop it's still not so small). :) Also, can we re-enable line length in spec files?
  • When it comes to indentation, in general I (and to my knowledge this was a rule at thoughtbot, so it doesn't come from nowhere) try not to add more spaces than is necessary; instead the number of spaces should be an even number. What this means is that in cases where you have an assignment or are using an operator, and the right hand side needs to be spread across multiple lines, it's more maintainable, and an easier rule to remember IMO, to write:
foo <<
  if bar
    "baz"
  else
    "qux"
  end

rather than:

foo << if bar
         "baz"
       else
         "qux"
       end
  • In a similar vein, there are some changes around indentation of arguments to methods that span multiple lines. It has also been an unofficial rule that if any argument to a method is on its own line, then all arguments should be on their own lines. Is there any way to enforce this in Rubocop?
  • There seem to be a lot of changes around trailing commas. This was a change I made to the Rubocop config a while back but which I now regret (it creates more of a pain point than it fixes). Considering there are a lot of files that now violate this rule, I'm wondering if that would be better to revert this cop (not sure how much trouble it is).
  • There are a few cases where a long string was reformatted to fit across multiple lines but now contain line breaks that weren't there before, so make sure to change those back.
  • There are a few false negatives that I ran across, so make sure to look out for those and change those back too.

gemfiles/rails_4_2.gemfile Outdated Show resolved Hide resolved
.rubocop.yml Show resolved Hide resolved
.rubocop.yml Outdated Show resolved Hide resolved
custom_plan.rb Show resolved Hide resolved
@mcmire
Copy link
Collaborator

mcmire commented Sep 8, 2020

@KapilSachdev To answer your earlier questions:

  • In case you missed it in the review above, it's fine to have this be a big PR, just as long as it's limited to upgrading .rubocop.yml to 0.80.0 and fixing offenses, and not introducing any new major style changes that weren't there before.
  • Let's disable AbcSize, CyclomaticComplexity, and PerceivedComplexity. I don't find these to be particular helpful metrics. It is easy enough to judge whether a method is complex or difficult to read.
  • As for MethodLength and BlockNesting, since there are not hundreds of violations I wonder if it would be effective in this case to use # rubocop:disable. But first, I'd like to know where these are happening?

.rubocop.yml Show resolved Hide resolved
@mcmire
Copy link
Collaborator

mcmire commented Sep 8, 2020

@utkarsh2102 Thank you for the suggestions. I did notice a lot of changes around quotes and I agree that ideally double quotes make more sense and lead to more consistency. I'm not sure what the ratio of single quote to double quote usage is, though, so it might be a wash. Also, I would rather keep this PR focused to fixing existing violations as it relates to the current state of .rubocop.yml (or at least a version of .rubocop.yml that works with 0.80.0`) rather than introduce new cops.

@KapilSachdev
Copy link
Collaborator Author

Thanks for reviewing. I will check those one by one. (Also I just noticed your comment that was made 4 days ago which i missed somehow)

Below i'm responding against the comments made in general.

It's better to use double_quotes and also this codebase uses it anyway!

The code base has enforced style of single_quotes.

I'm not sure what the ratio of single quote to double quote usage is, though, so it might be a wash.

In master there are 4195 offences if we go towards double_quotes, as we have used single quotes in all our specs. and 438 for single_quotes which this PR has made changes against.

Can we stick to an 80-character line length?

Sure, I changed it to 120 thinking that rubocop had 80 before and we might have been using 80 because of that and now rubocop has changed to use 120 so i updated against that. If you want it to be 80, I will change it back, should I?

When it comes to indentation, in general I (and to my knowledge this was a rule at thoughtbot, so it doesn't come from nowhere) try not to add more spaces than is necessary

I will look what could be done in that regards as indentation sometimes conflicts with other cops

There seem to be a lot of changes around trailing commas.
I too didn't liked it but the there was enforced style for it already, so i thought this might have been discussed before and had been followed. should I revert that as well?

There are a few cases where a long string was reformatted to fit across multiple lines but now contain line breaks that weren't there before, so make sure to change those back.
There are a few false negatives that I ran across, so make sure to look out for those and change those back too.

Sure, will do so.
I should not have marked the last commit as [ci skip] otherwise we could have known the test report as well. Though i ran tests against non-cosmetic changes, but not all as bundle exec appraisal rails_6_0 rpsec was failing but i didn't debugged further and executed single test as they are running fine.

Let's disable AbcSize, CyclomaticComplexity, and PerceivedComplexity
👍

All 4 BlockNesting offenses are under this block.

Metrics/MethodLength: 7 offenses


Below one can be converted into a constant?

def store_translations(options = {without: []})

Add a Exclude for tasks directory?

The below two locations can be fixed by just removing empty line!


def define_model_validating_uniqueness(options = {}, &block)

@utkarsh2102
Copy link
Contributor

Hello @KapilSachdev 👋🏻

The code base has enforced style of single_quotes.
In master there are 4195 offences if we go towards double_quotes, as we have used single quotes in all our specs. and 438 for single_quotes which this PR has made changes against.

Eeks, I am sorry for not looking more closely! In that case, this looks good, thanks for your work! 💪🏻


Hello @mcmire 👋🏻

I did notice a lot of changes around quotes and I agree that ideally double quotes make more sense and lead to more consistency. I'm not sure what the ratio of single quote to double quote usage is, though, so it might be a wash.

Yep, why I say double_quotes are better because in cases of string interpolation, you have to use double quotes anyway, there's no escape from that. And if you use single quotes otherwise, it causes inconsistencies (double quotes for interpolation and single_quotes for other things!). So the best way out is using double_quotes bu default!

About the ratio, here's a good research study: rubocop/rubocop#5306 (comment)
Over a span of 2.5 years, more people have migrated to using double_quotes because of the inconsistency (as mentioned above!).
Furthermore, bundler now (already!) ships a default .rubocop.yml so the gems by default will now use double_quotes. PR here: rubygems/rubygems#3731

Let me know if you think it's worth migrating to double_quotes here as well?
If so, I'll be happy to take this after this PR is merged! 😄

Also, I would rather keep this PR focused to fixing existing violations as it relates to the current state of .rubocop.yml (or at least a version of .rubocop.yml that works with 0.80.0`) rather than introduce new cops.

You're right! Let's tackle this later as a different PR (as proposed above^)! 👍🏻

@mcmire
Copy link
Collaborator

mcmire commented Sep 10, 2020

@utkarsh2102 Hi just wanted to respond to your comment. I realize that most people probably use double quotes now but I'd rather not change the entire codebase to use double quotes. Although it may seem like there are bunch of changes to the code style in this PR, the code is really being aligned to fit rules that have already been in place (or enforce unofficial rules). It was bound to happen anyway so I treat this as a rip-the-bandaid-off kind of situation. But I'd rather not change the existing style at this point and force a widespread code change. I appreciate your suggestion though :)

@utkarsh2102
Copy link
Contributor

Hi @mcmire,

No problem!
Thanks for all your work :)

@mcmire
Copy link
Collaborator

mcmire commented Sep 15, 2020

@KapilSachdev Sorry for the delay in responding to your last comment. Here are my responses for the warnings you mentioned, first giving the link to the code, then the commentary:

The warnings for these methods make sense — they are super long. I think refactoring them is out of scope, but it's probably worth noting. Maybe for now we want to disable BlockNesting and MethodLength for them and make a new issue to refactor them later?

This one seems okay to me. There's no real way to make a constant inside of a block, because that constant will get hoisted outside that block, so I think our only choice is to have a method. I think disabling MethodLength is fine for this.

def store_translations(options = {without: []})

I feel like there might be a way to refactor this slightly, but even so it will likely be over the limit still. Perhaps we disable MethodLength for this one as well.

Maybe we ought to refactor this into different methods: add_generate_task, add_autogenerate_task, etc. That would probably be more bite-size. If you want to do this now that's fine, otherwise I would disable MethodLength for this method. (Again I'd rather avoid Excludeing an entire directory unless we know there are 1000 violations or something.)

This method is super long as well, so ideally it could be refactored later, but if removing the empty lines at the end keep it under the limit I'm fine with that.

def define_model_validating_uniqueness(options = {}, &block)

There might be a way to refactor this as well although it's not obvious, so removing the empty lines here makes sense.

@KapilSachdev
Copy link
Collaborator Author

Thanks for the feedback @mcmire and no worries. I didn't get the time to work on it further and might not get this week also.

Maybe for now we want to disable BlockNesting and MethodLength for them and make a new issue to refactor them later?

👍 I actually wanted to get hound or whatever we choose to get back up and running so that it will help cut your review cost a bit, and meanwhile we can refactor the remaining pieces.

Again I'd rather avoid Excluding an entire directory unless we know there are 1000 violations or something.

I actually do prefer the code to be consistent with rules defined, around all areas of source code, be it either script/tasks, specs etc... but i think i wanted to get rid of those at that point. Its better we ignore them and add to TODO refactoring list.

@mcmire
Copy link
Collaborator

mcmire commented Oct 2, 2020

@KapilSachdev Reading back over your comment, I realize this PR is pretty large so I'm fine with Excluding things for now if it helps to get this across the finish line.

@KapilSachdev
Copy link
Collaborator Author

@mcmire Apologies for late reply. I will try to complete the suggestions you made soon and we can decide on next steps.

@mcmire
Copy link
Collaborator

mcmire commented Oct 12, 2020

@KapilSachdev No worries! Sounds good.

@KapilSachdev KapilSachdev marked this pull request as ready for review October 15, 2020 18:09
@vsppedro
Copy link
Collaborator

vsppedro commented Oct 15, 2020

Sorry. I saw the commit Bump rubocop from 0.89.1 to 0.90 and thought that the gem was not with the latest version. I should have looked at Gemfile.lock. My bad.

@mcmire
Copy link
Collaborator

mcmire commented Oct 15, 2020

Just checked CI and it looks like there are a couple of failures. Perhaps one of the .freeze changes caused this? Kind of a weird error.

@KapilSachdev
Copy link
Collaborator Author

KapilSachdev commented Oct 16, 2020

@vsppedro I should have added this in the commit message. This was not done in the commit you are mentioning, but in the changes pushed yesterday.

@mcmire Yes, it is weird. The failing test should have failed in all the test suites, but they didn't and seems to be affected in postgresql adapter.

      class ValidateNumericalityOfMatcher
        NUMERIC_NAME = 'number'.freeze
        NON_NUMERIC_VALUE = 'abcd'.freeze # This seems to be the issue
        DEFAULT_DIFF_TO_COMPARE = 1
...

I will look into it.
Do you have any thoughts on it as to why this is happening?

@mcmire
Copy link
Collaborator

mcmire commented Oct 16, 2020

@KapilSachdev It looks like the test that's failing involves setting a value on an attribute which is a Postgres money column type. It looks like the money type, in the process of type-casting the given value, directly modifies the incoming value using sub!. Since the incoming value is frozen in this case, that fails. This appears to be a defect in Rails that was fixed in Rails 5.2. Because we can't really do anything about that, perhaps the fix here is to move NON_NUMERIC_VALUE in validate_numericality_of_matcher.rb so that it's a class or instance method instead of a constant, and then take off .freeze?

@KapilSachdev
Copy link
Collaborator Author

@mcmire As gemfiles are auto-generated I have excluded them from rubocop.
All green now

@KapilSachdev
Copy link
Collaborator Author

Upgrading to v1.0 in current PR state will add the following offences.

Count Cop
54 Style/StringConcatenation
10 Style/OptionalBooleanParameter
4 Layout/EmptyLinesAroundAttributeAccessor
3 Style/HashLikeCase
2 Lint/MissingSuper
2 Style/RedundantRegexpCharacterClass
1 Layout/SpaceAroundMethodCallOperator
1 Lint/EmptyExpression
1 Lint/EmptyInterpolation
1 Style/CaseLikeIf
1 Style/ClassEqualityComparison
1 Style/ExplicitBlockArgument
1 Style/GlobalStdStream

@vsppedro
Copy link
Collaborator

IMO, I think it would be best to let this upgrade for another PR.

@KapilSachdev
Copy link
Collaborator Author

Yes, I'm working on it. Just mentioned the cops in case we want any cop to be disabled or need some non-default enforcement preference.

@KapilSachdev
Copy link
Collaborator Author

KapilSachdev commented Oct 27, 2020

@mcmire Please confirm the expectation for Style/OptionalBooleanParameter (currentl i have disabled it).
And also for Lint/MissingSuper. Do we want to disable it, or ignore for the instances mentioned below.

def respond_to_missing?(_name, _include_all)
true
end
def method_missing(method_name, *args, &block)
call = MethodCall.new(

For Style/HashLikeCase following changes were done

class ComparisonMatcher < ValidationMatcher
ERROR_MESSAGES = {
:> => :greater_than,
:>= => :greater_than_or_equal_to,
:< => :less_than,
:<= => :less_than_or_equal_to,
:== => :equal_to,
:!= => :other_than,
}

ERROR_MESSAGES = {
            :> => { label: :greater_than,
                    assertions: [false, false, true], },
            :>= => { label: :greater_than_or_equal_to,
                     assertions: [false, true, true], },
            :< => { label: :less_than,
                    assertions: [true, false, false], },
            :<= => { label: :less_than_or_equal_to,
                     assertions: [true, true, false], },
            :== => { label: :equal_to,
                     assertions: [false, true, false], },
            :!= => { label: :other_than,
                     assertions: [true, false, true], },
          }.freeze

and

def macro_description
case macro.to_s
when 'belongs_to'
'belong to'
when 'has_many'
'have many'
when 'has_one'
'have one'
when 'has_and_belongs_to_many'
'have and belong to many'
end
end

        MACROS = {
          'belongs_to' => 'belong to',
          'has_many' => 'have many',
          'has_one' => 'have one',
          'has_and_belongs_to_many' => 'have and belong to many',
        }.freeze

@KapilSachdev KapilSachdev force-pushed the fix/rubocop_offenses branch 2 times, most recently from 5399f48 to b18f576 Compare October 30, 2020 18:15
Copy link
Collaborator

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Adding some more comments. We're getting close. One thing I would say is that I feel like in some cases we are making code changes just to appease Rubocop but they actually detract from the readability or make things look objectively awkward. For instance, needing to add \\ to appease the line length. For now it's probably okay just so we can get this over the finish line, but maybe it's something we should look to relax in the future.

@@ -4,12 +4,16 @@
require 'shoulda/matchers/active_model/validation_matcher/build_description'
require 'shoulda/matchers/active_model/validator'
require 'shoulda/matchers/active_model/allow_value_matcher'
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_changed_value_error'
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_does_not_exist_error'
require 'shoulda/matchers/active_model/allow_value_matcher/'\
Copy link
Collaborator

@mcmire mcmire Nov 2, 2020

Choose a reason for hiding this comment

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

I think this is one of those things that actually detracts from the readability of code, can we leave these alone or disable the cops on these?

Copy link
Collaborator Author

@KapilSachdev KapilSachdev Nov 3, 2020

Choose a reason for hiding this comment

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

I will check if LineLength can be disabled for require. It should work i think.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay perfect :)

:<= => :less_than_or_equal_to,
:== => :equal_to,
:!= => :other_than,
:> => { label: :greater_than,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you format this as the following:

{
  label: :greater_than,
  assertions: [false, false, true]
},
...

Again, abiding by the rule guideline that you shouldn't need to add extra spaces to align items.

matcher.with_message(@message, values: { count: @value })
matcher
end
@_submatchers ||= comparison_combos.map do |
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like another change to please Rubocop but actually detracts from the readability, would you mind changing this back?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change might have been caught up in the process.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Haha, no worries!

@@ -77,7 +83,7 @@ def all_bounds_correct?

def failing_submatchers
submatchers_and_results.
reject { |x| x[:matched] }.
select { |x| !x[:matched] }.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know I've asked about this before but which cop controls this? This seems like an unnecessary check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was reverted to its original state.
Style/InverseMethods controls this behaviour.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, sorry for the bluntness. I just think it may lead to churn.

@@ -558,7 +558,8 @@ def add_submatcher(submatcher)
end

if submatcher.respond_to?(:diff_to_compare)
@diff_to_compare = [@diff_to_compare, submatcher.diff_to_compare].max
@diff_to_compare = [@diff_to_compare,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe:

@diff_to_compare = [
  @diff_to_compare,
  submatcher.diff_to_compare,
].max

Again, the whole "keep indentation to multiple of 2 and don't add more than is necessary" sort of thing

@@ -1,6 +1,7 @@
require 'shoulda/matchers/active_record/association_matcher'
require 'shoulda/matchers/active_record/association_matchers'
require 'shoulda/matchers/active_record/association_matchers/counter_cache_matcher'
require 'shoulda/matchers/active_record/association_matchers/'\
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another thing I don't think is necessary :)

@@ -990,6 +990,13 @@ class AssociationMatcher

attr_reader :name, :options

MACROS = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you mind putting this at the top of the class? I believe that we have been pretty good about putting them first.

@@ -1292,7 +1299,8 @@ def class_name_correct?
end

def join_table_correct?
if macro != :has_and_belongs_to_many || join_table_matcher.matches?(@subject)
if macro != :has_and_belongs_to_many || \
Copy link
Collaborator

Choose a reason for hiding this comment

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

When it comes to stretching if statements across multiple lines, what do you think about formatting this as:

if (
  macro != :has_and_belongs_to_many ||
  join_table_matcher.matches?(@subject)
)

@@ -33,9 +33,9 @@ def matches?(subject)
end

missing_option << (
'fail validation if ' +
Copy link
Collaborator

@mcmire mcmire Nov 2, 2020

Choose a reason for hiding this comment

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

Is this enforced by a cop? I notice you doing this a lot and I'm wondering if it's your style or something that Rubocop is enforcing. If not no worries just curious.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change is enforced by 1.0, which requires string concatenation to be enforced with interpolation by default.
So to counter LineLength and Style/StringConcatenation cops, i had to use \.
I was thinking of heredoc, but that would be too much of a change, so updated it with \ for your feedback.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, gotcha 👍

@mcmire
Copy link
Collaborator

mcmire commented Nov 2, 2020

@KapilSachdev Let's leave both Style/OptionalBooleanParameter and Lint/MissingSuper disabled for now.

@@ -67,7 +69,7 @@ def has_attribute?
end

def has_expected_action_text?
@subject.send(rich_text_attribute).class.name == 'ActionText::RichText'
@subject.send(rich_text_attribute).instance_of?(ActionText::RichText)
Copy link
Collaborator

@vsppedro vsppedro Nov 2, 2020

Choose a reason for hiding this comment

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

Instead of using the class, can you try using a string?

@subject.send(rich_text_attribute).instance_of?('ActionText::RichText')

Let's see if this change will fix the problem on CI. I almost sure. I'll make some tests.

EDIT 1:

instance_of does not work passing a string. I'll look other way.

EDIT 2:

defined?(ActionText::RichText) && @subject.send(rich_text_attribute).instance_of?(ActionText::RichText)

But I think we can do better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

class name evaluation with string poses an issue of being failed silently when the class is not even defined.
But this current scenario is not one of those cases so we may revert to string class comparison.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I did not know that. Thank you!

- bump rubocop to v1.0
- Fix Layout/MultilineAssignmentLayout and other remaining offences
- Exculde appraisal generated gemfiles in rubocop
- Replace NON_NUMERIC_VALUE constant with instance method against 
failing test case in  rails <= 5.1 and postgres adapter.
This is a defect in rails where a frozen string is modified in 
https://github.com/rails/rails/blob/v5.1.7/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb#L25
@mcmire
Copy link
Collaborator

mcmire commented Nov 3, 2020

@KapilSachdev Anything else you noticed? I'm good with this PR if you are.

@KapilSachdev
Copy link
Collaborator Author

@mcmire I think the current state of the PR is fine.

@mcmire
Copy link
Collaborator

mcmire commented Nov 3, 2020

@KapilSachdev Okay cool. I see you keep force-pushing, so did you want to keep your commits separate or put them together?

@KapilSachdev
Copy link
Collaborator Author

@mcmire I had wanted to keep them separate, but if you think the change should go in one commit altogether, thats fine with me too.
Should I squash them all in one?

@mcmire
Copy link
Collaborator

mcmire commented Nov 3, 2020

@KapilSachdev No, it's fine with me to keep them separate! Just wanted to know your preference.

Merging now!

@mcmire mcmire merged commit b7e0218 into thoughtbot:master Nov 3, 2020
@KapilSachdev
Copy link
Collaborator Author

Thank you.

@KapilSachdev KapilSachdev deleted the fix/rubocop_offenses branch November 3, 2020 17:06
@KapilSachdev KapilSachdev added this to the 4.5.0 milestone Nov 11, 2020
Earlopain added a commit to Earlopain/shoulda-matchers that referenced this pull request Jun 5, 2024
Ruby 3.4 will warn when `logger` is required without it being part of the gemspec: ruby/ruby@d7e558e

The usage of a logger was added and removed in thoughtbot#1350
matsales28 pushed a commit that referenced this pull request Jul 5, 2024
Ruby 3.4 will warn when `logger` is required without it being part of the gemspec: ruby/ruby@d7e558e

The usage of a logger was added and removed in #1350
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.

4 participants