Class: Mdlint::Linter::Rules::LineLength

Inherits:
Mdlint::Linter::Rule show all
Defined in:
lib/mdlint/linter/rules/line_length.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, #fix, inherited, #initialize

Constructor Details

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

Instance Method Details

#check(_tokens, source) ⇒ Object



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

def check(_tokens, source)
  maximum = @options.fetch(:line_length, @options.fetch(:length, 80)).to_i
  return @violations if maximum <= 0

  in_fence = false
  source.each_line.with_index(1) do |line, line_number|
    stripped = line.chomp
    in_fence = !in_fence if stripped.match?(/\A {0,3}(`{3,}|~{3,})/)
    next if in_fence && @options.fetch(:ignore_code_blocks, false)
    next if TextWidth.measure(stripped) <= maximum

    add_violation(
      message: "Line length #{TextWidth.measure(stripped)} exceeds #{maximum}",
      line: line_number,
      column: maximum + 1,
      fixable: false
    )
  end

  @violations
end