Module: RuboCop::Cop::PrecedingFollowingAlignment

Included in:
Style::ExtraSpacing, Style::SpaceAroundOperators, Style::SpaceBeforeFirstArg
Defined in:
lib/rubocop/cop/mixin/preceding_following_alignment.rb

Overview

Common functionality for checking whether an AST node/token is aligned with something on a preceding or following line

Instance Method Summary collapse

Instance Method Details

#aligned_assignment?(range, line) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 80

def aligned_assignment?(range, line)
  range.source[-1] == '=' && line[range.last_column - 1] == '='
end

#aligned_char?(range, line) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 76

def aligned_char?(range, line)
  line[range.column] == range.source[0]
end

#aligned_identical?(range, line) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 84

def aligned_identical?(range, line)
  range.source == line[range.column, range.size]
end

#aligned_operator?(range, line) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 68

def aligned_operator?(range, line)
  (aligned_identical?(range, line) || aligned_assignment?(range, line))
end

#aligned_token?(range, line) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 62

def aligned_token?(range, line)
  aligned_words?(range, line) ||
    aligned_char?(range, line) ||
    aligned_assignment?(range, line)
end

#aligned_with_adjacent_line?(range, predicate) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 20

def aligned_with_adjacent_line?(range, predicate)
  # minus 2 because node.loc.line is zero-based
  pre  = (range.line - 2).downto(0)
  post = range.line.upto(processed_source.lines.size - 1)

  aligned_with_any_line_range?([pre, post], range, &predicate)
end

#aligned_with_any_line?(line_ranges, range, indent = nil, &predicate) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 38

def aligned_with_any_line?(line_ranges, range, indent = nil, &predicate)
  line_ranges.any? do |line_nos|
    aligned_with_line?(line_nos, range, indent, &predicate)
  end
end

#aligned_with_any_line_range?(line_ranges, range, &predicate) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 28

def aligned_with_any_line_range?(line_ranges, range, &predicate)
  return true if aligned_with_any_line?(line_ranges, range, &predicate)

  # If no aligned token was found, search for an aligned token on the
  # nearest line with the same indentation as the checked line.
  base_indentation = processed_source.lines[range.line - 1] =~ /\S/

  aligned_with_any_line?(line_ranges, range, base_indentation, &predicate)
end

#aligned_with_line?(line_nos, range, indent = nil) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 44

def aligned_with_line?(line_nos, range, indent = nil)
  line_nos.each do |lineno|
    next if comment_lines.include?(lineno + 1)
    line = processed_source.lines[lineno]
    index = line =~ /\S/
    next unless index
    next if indent && indent != index
    return yield(range, line)
  end
  false
end

#aligned_with_operator?(range) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 16

def aligned_with_operator?(range)
  aligned_with_adjacent_line?(range, method(:aligned_operator?))
end

#aligned_with_something?(range) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 12

def aligned_with_something?(range)
  aligned_with_adjacent_line?(range, method(:aligned_token?))
end

#aligned_words?(range, line) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 72

def aligned_words?(range, line)
  line[range.column - 1, 2] =~ /\s\S/
end

#allow_for_alignment?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 8

def allow_for_alignment?
  cop_config['AllowForAlignment']
end

#comment_linesObject



56
57
58
59
60
# File 'lib/rubocop/cop/mixin/preceding_following_alignment.rb', line 56

def comment_lines
  @comment_lines ||= processed_source.comments.map(&:loc).select do |r|
    begins_its_line?(r.expression)
  end.map(&:line)
end