Class: Mdlint::Linter::Rules::NoMultipleBlanks

Inherits:
Mdlint::Linter::Rule show all
Defined in:
lib/mdlint/linter/rules/no_multiple_blanks.rb,
sig/internal.rbs

Instance Attribute Summary

Attributes inherited from Mdlint::Linter::Rule

#violations

Instance Method Summary collapse

Methods inherited from Mdlint::Linter::Rule

#add_violation, inherited, #initialize

Constructor Details

This class inherits a constructor from Mdlint::Linter::Rule

Instance Method Details

#check(_tokens, source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mdlint/linter/rules/no_multiple_blanks.rb', line 11

def check(_tokens, source)
  blank_count = 0

  source.each_line.with_index(1) do |line, line_num|
    if line.strip.empty?
      blank_count += 1
      if blank_count > 1
        add_violation(
          message: "Multiple consecutive blank lines",
          line: line_num,
          fixable: true
        )
      end
    else
      blank_count = 0
    end
  end

  @violations
end

#fix(_tokens, source) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mdlint/linter/rules/no_multiple_blanks.rb', line 32

def fix(_tokens, source)
  result = []
  blank_count = 0

  source.each_line do |line|
    if line.strip.empty?
      blank_count += 1
      result << line if blank_count <= 1
    else
      blank_count = 0
      result << line
    end
  end

  result.join
end