Cop/LineBreakAroundConditionalBlock breaks when there's a comment on the block opening line

Our cop LineBreakAroundConditionalBlock will fail with such example:

    def status
      strong_memoize(:status) do # test comment
        if Feature.enabled?(:ci_composite_status, default_enabled: false)
          Gitlab::Ci::Status::Composite
            .new(@jobs)
            .status
        else
          CommitStatus
            .where(id: @jobs)
            .legacy_status
        end
      end
    end

(just added a #test comment here, everything else as it is from app/models/ci/group.rb which had no offences)
=>

app/models/ci/group.rb:26:9: C: Cop/LineBreakAroundConditionalBlock: Add a line break around conditional blocks
        if Feature.enabled?(:ci_composite_status, default_enabled: false) ...
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

This is a valid code and the failure description is very misleading, so it should be fixed.

Another failing example:

record.in_batches(of: @batch_size) do |batch| # rubocop:disable Cop/InBatches
  if Feature.enabled?(:export_fast_serialize_with_raw_json, default_enabled: true)
    ...

To start a fix with adding new failing specs, here's a suggested new example to add into spec/rubocop/cop/line_break_around_conditional_block_spec.rb, ~line:135:

    it "doesn't flag violation for #{conditional} preceded by a block definition with a comment" do
      source = <<~RUBY
          on_block(param_a) do |item| # a short comment
            #{conditional} condition
              do_something
            end
          end
      RUBY
      inspect_source(source)

      expect(cop.offenses).to be_empty
    end
Edited May 31, 2022 by 🤖 GitLab Bot 🤖
Assignee Loading
Time tracking Loading