-
Notifications
You must be signed in to change notification settings - Fork 27
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
Always correct conditional to multiline in CallbackConditionalsBinding #74
Conversation
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 dig it
@@ -52,13 +52,38 @@ def autocorrect(node) | |||
_, _, block = conditional.child_nodes | |||
expected_class = node.parent_module_name | |||
|
|||
if block.source.include?("T.unsafe(self).") |
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.
Can't it be as problem with this?
something: -> { T.unsafe(self).passing_splat(*args) }
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.
Ugh, yes, that would be indeed be an issue. Should we just ignore T.unsafe
s then?
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.
If we're going to change working code into non-working code yes.
Another solution is to move the T.unsafe
part in another cop that is optional and just used to auto-correct the T.unsafe
in T.bind
…e same line as the class
da11ad5
to
026d835
Compare
I made two corrections based on recent feedback.
module Something
class PrivateClass
# Here if we auto correct to the fully qualified name, it will give a typing error of non-private reference to a private constant
before_create :do_it, if: -> { T.bind(self, Something::PrivateClass)... }
end
private_constant(:PrivateClass)
end However, if we never use the fully qualified name, then these scenarios will also fail: class Something::MyClass
# If we bind to MyClass, Sorbet will error saying it cannot find the definition of MyClass
before_create :do_it, if: -> { T.bind(self, MyClass)... }
end Therefore, the cop will now auto-correct using the fully qualified name for constants defined in this pattern |
Gemfile.lock
Outdated
@@ -63,4 +63,4 @@ DEPENDENCIES | |||
yard (~> 0.9) | |||
|
|||
BUNDLED WITH | |||
2.2.16 | |||
2.2.21 |
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.
Is this change expected?
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 assume it happened because my bundler version is newer. Would you prefer to roll this back? IMO, I'd just keep it
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.
Let's remove it 🔥
Added another test case for when two classes are defined inside the same namespace. module Something
class Namespace::Klass
# Properly corrects to Namespace::Klass because it's defined on the same line
validates :it, if: -> { T.bind(self, Namespace::Klass).should? }
end
class AnotherOne
# Properly corrects to AnotherOne because using the fully qualified name here is not necessary
validates :it, if: -> { T.bind(self, AnotherOne).should? }
end
end |
Gemfile.lock
Outdated
@@ -63,4 +63,4 @@ DEPENDENCIES | |||
yard (~> 0.9) | |||
|
|||
BUNDLED WITH | |||
2.2.16 | |||
2.2.21 |
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.
Let's remove it 🔥
corrector.remove_preceding(block, 1) | ||
end | ||
|
||
# Remove the last space and `}` and re-add it with a line break |
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.
Shouldn't the block be changed in a do
.. end
if it's going to be multiline? In our style guide we enforce BlockDelimiters.
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 there's already a cop for fixing {}
into do, end
, which would pick that up. Also, it can get quite complex when the lambda has parameters or whether you use ->
or lambda
and this cop is complex enough as it is.
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.
Fair 👍
a62d498
to
474dc3b
Compare
corrector.remove_preceding(block, 1) | ||
end | ||
|
||
# Remove the last space and `}` and re-add it with a line break |
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.
Fair 👍
This PR applies a few fixes to the
CallbackConditionalsBinding
cop. It also adds support for usingproc
in the conditional instead of a lambda.Always breaks lines for multi statement procs
Substitute T.unsafe usage for bind
Array conditionals are not considered offenses anymore
Procs that accept arguments are not considered offenses
Conditionals where the recursive receiver is not self are now corrected to multiline
Chained send statements are properly corrected in a single line