Class: Laser::GenericLineLengthWarning

Inherits:
LineWarning show all
Defined in:
lib/laser/warnings/line_length.rb

Instance Attribute Summary

Attributes inherited from Warning

#body, #file, #line_number, #name, #severity

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from LineWarning

options

Class Method Details

+ (Object) inspect



8
9
10
# File 'lib/laser/warnings/line_length.rb', line 8

def self.inspect
  "Laser::GenericLineLengthWarning<#{line_length_limit}>"
end

Instance Method Details

- (Object) fix(content_stack = nil)



16
17
18
19
20
21
22
# File 'lib/laser/warnings/line_length.rb', line 16

def fix(content_stack = nil)
  result = handle_long_comments(self.line)
  return result if result
  result = try_to_fix_guarded_lines(self.line)
  return result if result
  self.line
end

- (Object) fix_long_comment(text)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/laser/warnings/line_length.rb', line 61

def fix_long_comment(text)
  # Must have no leading text
  return nil unless text =~ /^(\s*)(#+\s*)(.*)\Z/
  indent, hashes, comment = $1, $2, $3
  indent_size = indent.size
  # The "+ 2" is (indent)#(single space)
  space_for_text_per_line = self.class.line_length_limit - (indent_size + hashes.size)
  lines = ['']
  words = comment.split(/\s/)
  quota = space_for_text_per_line
  current_line = 0
  while words.any?
    word = words.shift
    # break on word big enough to make a new line, unless its the first word
    if quota - (word.size + 1) < 0 && quota < space_for_text_per_line
      current_line += 1
      lines << ''
      quota = space_for_text_per_line
    end
    unless lines[current_line].empty?
      lines[current_line] << ' '
      quota -= 1
    end
    lines[current_line] << word
    quota -= word.size
  end
  lines.map { |line| indent + hashes + line }.join("\n")
end

- (Object) handle_long_comments(line)



51
52
53
54
55
56
57
58
59
# File 'lib/laser/warnings/line_length.rb', line 51

def handle_long_comments(line)
  code, comment = split_on_token(line, :on_comment)
  return nil if comment.empty?
  indent, code = code.match(/^(\s*)(.*)$/)[1..2]
  hashes, comment = comment.match(/^(#+\s*)(.*)$/)[1..2]
  comment_cleaned = fix_long_comment(indent + hashes + comment)
  code_cleaned = !code.strip.empty? ? "\n" + indent + code.rstrip : ''
  comment_cleaned + code_cleaned
end

- (Boolean) match?(body = self.body)



12
13
14
# File 'lib/laser/warnings/line_length.rb', line 12

def match?(body = self.body)
  !!(line.rstrip.size > self.class.line_length_limit)
end

- (Object) try_to_fix_guarded_lines(line)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/laser/warnings/line_length.rb', line 24

def try_to_fix_guarded_lines(line)
  return nil if line !~ /\b(if|unless)\s/  # quick fast check
  code, guard = split_on_keyword(:if, :unless)
  code.rstrip!
  return nil if code.empty? || guard.empty? || code.strip == 'end'
  # check guard for closing braces
  return nil if count_occurrences(guard, '}') != count_occurrences(guard, '{')
  indent = get_indent(line)
  indent_unit = ' ' * @settings[:indent_size]

  result = code
  until guard.empty?
    condition = indent + guard.strip
    body = result.split(/\n/).map { |line| indent_unit + line}.join("\n")
    new_condition, guard = split_on_keyword(condition[indent.size+1..-1], :if, :unless)
    if new_condition.empty?
      new_condition, guard = guard.rstrip, ''
    else
      new_condition = "#{condition[indent.size,1]}#{new_condition.rstrip}"
    end
    condition = indent + new_condition unless guard.empty?
    result = condition + "\n" + body + "\n" + indent + 'end'
  end

  result
end