You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was reading through this issue, and wanted to note that the resulting expression may need to have parenthesis in order to keep order of operations good. I'm not sure if it would be possible to calculate that it's not needed. Some examples counter to the example in the issue:
# Subtraction before multiplication
# BEFORE
def foo
a = 5 - 10
puts a # => -5
b = a * 5 # => -25
end
# Without ()
def foo
puts 5 - 10 # => -5
b = 5 - 10 * 5 # => **-45**
end
# With ()
def foo
puts (5 - 10) # => -5
b = (5 - 10) * 5 # => **-25**
end
I can't think of an example for this other thought quick enough, but I suspect there would be issues with certain method calling instances as well, akin to what you see with 1..10.map vs (1..10).map... you know, all those classic cases where you end up needing parens afterall.
I just wanted to highlight this concern as I feel like this refactoring tool could create some easy-to-miss bugs if it doesn't wrap the expression in parens.
Maybe there should be a check for "does the replacement candidate, or replacement targets include any method calls" in the ast? And if so, we should use parens? This may be naïve, but it seems like that's where things can get sticky.
That's a really good point. Maybe the implementation should check if the target of the inlining is a unary/math expression and then enforce parenthesis in those cases.
I'd like to pick this up. The plan is an "Inline variable" code action, offered when the cursor is on a local variable's assignment or on one of its reads. It replaces every read in the same scope with the assigned expression and removes the assignment, wrapping the expression in parentheses when it's a binary or unary expression, as you suggested above.
I'd leave the action out when the variable is reassigned in the scope, and when the value contains a method call and is read more than once, since inlining would change how often it runs. Does that match what you had in mind?
(I use AI assistance when working on patches, and I check and test everything before submitting.)
Activity
ttilberg commentedon Sep 20, 2024
I was reading through this issue, and wanted to note that the resulting expression may need to have parenthesis in order to keep order of operations good. I'm not sure if it would be possible to calculate that it's not needed. Some examples counter to the example in the issue:
I can't think of an example for this other thought quick enough, but I suspect there would be issues with certain method calling instances as well, akin to what you see with
1..10.mapvs(1..10).map... you know, all those classic cases where you end up needing parens afterall.I just wanted to highlight this concern as I feel like this refactoring tool could create some easy-to-miss bugs if it doesn't wrap the expression in parens.
ttilberg commentedon Sep 20, 2024
Maybe there should be a check for "does the replacement candidate, or replacement targets include any method calls" in the ast? And if so, we should use parens? This may be naïve, but it seems like that's where things can get sticky.
vinistock commentedon Sep 20, 2024
That's a really good point. Maybe the implementation should check if the target of the inlining is a unary/math expression and then enforce parenthesis in those cases.
IslamElsayed commentedon Oct 28, 2026
I'd like to pick this up. The plan is an "Inline variable" code action, offered when the cursor is on a local variable's assignment or on one of its reads. It replaces every read in the same scope with the assigned expression and removes the assignment, wrapping the expression in parentheses when it's a binary or unary expression, as you suggested above.
I'd leave the action out when the variable is reassigned in the scope, and when the value contains a method call and is read more than once, since inlining would change how often it runs. Does that match what you had in mind?
(I use AI assistance when working on patches, and I check and test everything before submitting.)