Feature #20278
opensyntax error, unexpected local variable or method, expecting ')' (SyntaxError) - but I think it should report that a ',' is missing, or alternatively, that suggesting ')' is the most likely cause but not always (e. g. reword it a bit)
Description
So this is not quite a bug, but I thought it may fit better here, as it is neither a feature, and also not quite fitting under misc.
Before I will explain the issue, let me copy/paste the code I use so that others can reproduce what I mean:
class Foobar
def foo1
var1 = 1
var2 = 2
Foobar.some_method(
var1
var2
)
end
def self.some_method(a, b)
puts a
end
end
Foobar.new
If it helps, the faulty lines are those between line 7 and 8, where I passed two variables var1 and var2, but I forgot a ',' between these two.
I actually had this happen just now in another (real) code, and the error message confused me.
Anyway, there are two things I want to report here. First, the smaller issue - the description show on the commandline is strange:
expected a `)` to close the argumentsexpected a newline or semicolon after the statementcannot parse the expression
Normally it should read "to close the arguments expected". It seems the ' ' space character between "arguments expected" is swallowed; same with the "statementcannot". But that is just description - I report it in the event someone else also finds it strange. I actually did not see it at first as I always look at the bottom first, so I tend to miss what appears on top. Anyway.
The second one, and reason why I wrote this issue here, is that the error shown is a bit confusing:
ack.rb:8: syntax error, unexpected local variable or method, expecting ')' (SyntaxError)
var2
So ruby indicates that there is an issue with var2 and a ')' was expecting. But I think ruby should instead have
expected a ','. I don't know whether ruby can distinguish between this; both var1 and var2 were already defined prior, so I think in theory the ruby parser could find out that there are two separate variables, and assume that a ',' could also be missing. I don't know if this is always the case, but at the least I think in the example code I showed, ruby saying that a ')' was expected, was wrong, in my opinion.
Again, perhaps ruby has no way to distinguish, but as ruby gives a specific suggestion that there is a missing ')', when in reality I was missing the ',', is in my opinion incorrect. Perhaps it is not worth to fix it, but being able to distinguish at the least between two local variables (as in var1 and var2) should be possible, and perhaps the overall message shown could be improved a bit. For instance, the line " the argumentsexpected" could perhaps not only NOT swallow the ' ' but also be displayed on two lines. I am not sure if this makes it better or worse, since people's preferences are different. Either way I wanted to bring this to attention here.
Updated by rubyFeedback (robert heiler) 6 months ago
I just noticed that it actually already reported that a semicolon is missing:
"expected a newline or semicolon"
So I would like to reword the above to perhaps consider also showing this information in the raised message below:
ack.rb:8: syntax error, unexpected local variable or method, expecting ')' (SyntaxError)
Could then be:
ack.rb:8: syntax error, unexpected local variable or method, expecting ')' or ',' (SyntaxError)
This may be context-dependent, but at the least in the example code I showed above, I think it is more correct to indicate that a ',' may be missing, as opposed to not mention it at all (but then why is it mentioned in the prior message shown above? I guess the "unexpected local variable or method, expecting ')'" part is older. May make sense to sync these two messages perhaps; right now they report slightly different things, e. g. one mentions the semicolon, the other does not.
Updated by jeremyevans0 (Jeremy Evans) 6 months ago
- Tracker changed from Bug to Feature
- ruby -v deleted (
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]) - Backport deleted (
3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN)