Sha256: 36aede562af52d3148e360359c57b31e8143a1efc3b690667476ac6248b4552b

Contents?: true

Size: 1.46 KB

Versions: 15

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # Shared behavior for aligning braces for single line lets
    class AlignLetBrace
      extend NodePattern::Macros

      def_node_matcher :let?, Language::Helpers::ALL.block_pattern.freeze

      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

15 entries across 15 versions & 1 rubygems

Version Path
rubocop-rspec-1.25.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.25.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.24.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.23.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.22.2 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.22.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.22.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.21.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.20.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.20.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.19.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.18.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.17.1 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.17.0 lib/rubocop/rspec/align_let_brace.rb
rubocop-rspec-1.16.0 lib/rubocop/rspec/align_let_brace.rb