Sha256: 54aeb94d13d9bdfc30544618d3ab7adc176103a4b93277bb0827262b65417e1c

Contents?: true

Size: 1.41 KB

Versions: 26

Compression:

Stored size: 1.41 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::NodePattern

      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 && member.loc.line == let.loc.line
          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

26 entries across 26 versions & 1 rubygems

Version Path
rubocop-rspec-2.0.0.pre lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.44.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.44.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.43.2 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.43.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.43.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.42.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.41.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.40.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.39.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.38.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.38.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.37.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.37.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.36.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.35.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.34.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.34.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.33.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.32.0 lib/rubocop/rspec/align_let_brace.rb