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
Layout/LineLength auto-correction produces invalid Ruby syntax in specific situations #9749
Comments
I'd like to report a similar problem with method calls and heredocs. In my case, the following method definition: def test_unindexed_foreign_keys
assert_equal(<<~OUTPUT, unindexed_foreign_keys({ "users" => ["profile_id", "account_id"], "account" => ["group_id"] }))
account group_id
users account_id profile_id
OUTPUT
end gets transformed into def test_unindexed_foreign_keys
assert_equal(<<~OUTPUT,
unindexed_foreign_keys({ "users" => ["profile_id", "account_id"], "account" => ["group_id"] }))
account group_id
users account_id profile_id
OUTPUT
end You can see the original method invoked |
@gregnavis I extracted your bug to #9799. |
…move the first argument of an unparenthesized `send` node to the next line, which changes behaviour. Previously in rubocop#9382, `Layout/LineLength` was fixed for unparenthesized `send` nodes with a hash, but now it is applied to all argument types.
[Fix #9749] Fix autocorrection for `Layout/LineLength` to not move the first argument of an unparenthesized `send` node to the next line
I have come across certain situations where the
Layout/LineLength
cop moves things down to a new line in such a way it produces invalid Ruby syntax. Specially for method calls which do not surround the arguments with parentheses.Expected behavior
Reformat method calls arguments in a sensible way that does not yield a syntax error.
Actual behavior
Method arguments are all moved to a new line, without any indentation, and because the method call does not wrap its arguments in parentheses, it becomes invalid syntax.
(the
.rubocop.yml
file referenced above is blank)Steps to reproduce the problem
The key is to have a method call without parentheses, at least two arguments, and the
,
after the first argument is on column 120 or later (assuming default max line length of 120). If max line length is customized to something other than the default 120, the column position that triggers the error will be the set max value.Create
file.rb
:Run rubocop against it:
file.rb
now has invalid syntax:RuboCop version
I've tested this with latest stable release (1.13.0), and from latest commit in master (74377b0) at time of writing.
Stable version details
.rubocop.yml
:# empty to override ~/.rubocop.yml
Gemfile
:Gemfile.lock
:Latest commit details
.rubocop.yml
:# empty to override ~/.rubocop.yml
Gemfile
:Gemfile.lock
:Extra Examples
This seems to happen whenever the
,
between the first and second argument is in colum 120 or higher with defaultLayout/LineLengh
max value of120
. if the max value is set to80
, the issue happens when the,
is on column 80 or higher.Example 1 (from above)
Yields invalid syntax when
,
between arguments is on column 120 or later, with no parentheses around arguments.Before:
After:
Expected:
Example 2
Formats the code properly when the
,
between arguments falls on column 119 or less, with no parentheses around arguments.Before:
After:
Example 3
Formats the code properly when arguments are wrapped in parentheses
Before:
After:
Example 4
Does not quite format/fix line length complaints when arguments are wrapped in parentheses, combined the arguments do not fit on a single line, but separated onto their own lines there would be no lines too long.
In the example below, the
fozbaz
argument gets placed so it ends beyond the 120th column triggering aLayout/LineLength
complaint. The argument can very reasonably be moved down to the next line as illustrated.Before:
After:
Expected:
The text was updated successfully, but these errors were encountered: