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

CodeLength incorrectly calculated when there are kwargs that should be counted as one #10469

Closed
Darhazer opened this issue Mar 24, 2022 · 5 comments · Fixed by #10520
Closed
Labels

Comments

@Darhazer
Copy link
Member

When there are keyword arguments, and the configuration says that hashes should be counted as one line, the code length module calculates the length incorrectly, by always subtracting 2.

I tracked the issue down to the Metrics::Utils::CodeLengthCalculator which includes this line

# Subtract 2 length of opening and closing brace if method argument omits hash braces.
length -= 2 if descendant.hash_type? && !descendant.braces?

This is valid in this example:

foo(
              foo: :bar,
              baz: :quux
            )

But not this one:

foo(foo: :bar, baz: :quux)

As a reproduction spec:

it 'folds hashes as method kwargs if asked' do
        source = parse_source(<<~RUBY)
          def test
            foo(foo: :bar, baz: :quux)
          end
        RUBY

        length = described_class.new(source.ast, source, foldable_types: %i[hash]).calculate
        expect(length).to eq(1)
      end

This gives a result of -1

RuboCop version

Tested on master

$ rubocop -V 
1.26.1 (using Parser 3.1.1.0, rubocop-ast 1.16.0, running on ruby 2.6.8 universal.arm64e-darwin21)
  - rubocop-performance 1.13.3
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.9.0
@Drenmi Drenmi added the bug label Mar 25, 2022
nobuyo added a commit to nobuyo/rubocop that referenced this issue Apr 13, 2022
koic added a commit that referenced this issue Apr 14, 2022
[Fix #10469] Fix code length calculation when kwargs written in single line
@Darhazer
Copy link
Member Author

#10520 fixes just a specific case. If the node is multiline, but still starts at the line of the parent node, it still gives the wrong result

it 'folds hashes as method args if asked' do
        source = parse_source(<<~RUBY)
          def test
            foo(foo: :bar,
                baz: :quux)
          end
        RUBY

        length = described_class.new(source.ast, source, foldable_types: %i[hash]).calculate
        expect(length).to eq(1)
      end

The actual of the above is -1

@Darhazer Darhazer reopened this Apr 14, 2022
@nobuyo
Copy link
Contributor

nobuyo commented Apr 14, 2022

I'm sorry for I didn't enough consider about coverage of cases 🙏
I'll attempt some additional fix on new branch.

Do the following examples cover all cases for this issue?

# pattern 1
def test
  foo(
    foo: :bar,
    baz: :quux
  )
end

# pattern 2
def test
  foo(foo: :bar, baz: :quux)
end

# pattern 3
def test
  foo(foo: :bar,
      baz: :quux)
end

# pattern 4
def test
  foo(
    foo: :bar, baz: :quux
  )
end

@Darhazer
Copy link
Member Author

I'm sorry for I didn't enough consider about coverage of cases pray

That's alright - you have improved the calculation nevertheless. Just haven't fully resolved the issue

I'll attempt some additional fix on new branch.

Thank you 🙇

Do the following examples cover all cases for this issue?

Seems exhaustive enough. Well, if you want to go to extremes:

# pattern 5
def test
  foo(
    # those are keyword arguments
    foo: :bar, baz: :quux

  )
end

Though it's not clear whether we should count the comment as part of the hash or not 🤷‍♂️

@nobuyo
Copy link
Contributor

nobuyo commented Apr 16, 2022

I created PR (#10543) for this. I'd be happy if you take a look at it :)

@Darhazer
Copy link
Member Author

Fixed in #10543

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants