Sha256: 7c8f6aa765edff06e8984fee083f7454eda2cd67684312f7ac3ae638f352b9b8

Contents?: true

Size: 1.42 KB

Versions: 66

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # Shared behavior for aligning braces for single line lets
    class AlignLetBrace
      include RuboCop::RSpec::Language
      include RuboCop::Cop::Util

      def initialize(root, token)
        @root  = root
        @token = token
      end

      def offending_tokens
        single_line_lets.reject do |let|
          target_column_for(let) == let_token(let).column
        end
      end

      def indent_for(node)
        ' ' * (target_column_for(node) - let_token(node).column)
      end

      private

      def let_token(node)
        node.loc.public_send(token)
      end

      def target_column_for(let)
        let_group_for(let).map { |member| let_token(member).column }.max
      end

      def let_group_for(let)
        adjacent_let_chunks.detect do |chunk|
          chunk.any? do |member|
            member == let && same_line?(member, let)
          end
        end
      end

      def adjacent_let_chunks
        last_line = nil

        single_line_lets.chunk do |node|
          line      = node.loc.line
          last_line = (line if last_line.nil? || last_line + 1 == line)
          last_line.nil?
        end.map(&:last)
      end

      def single_line_lets
        @single_line_lets ||=
          root.each_node(:block).select do |node|
            let?(node) && node.single_line?
          end
      end

      attr_reader :root, :token
    end
  end
end

Version data entries

66 entries across 60 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.3.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.2.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.1.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.0.5 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.0.4 lib/rubocop/rspec/align_let_brace.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.0.3 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.0.2 lib/rubocop/rspec/align_let_brace.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.0.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.0.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-3.0.0.pre lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-2.31.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-2.30.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-2.29.2 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-2.29.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-2.29.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-2.28.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-2.27.1 lib/rubocop/rspec/align_let_brace.rb