Class: Mdlint::Linter::Rules::FencedCodeBlankLines

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

Constant Summary

Constants included from SourceStyleSupport

SourceStyleSupport::FENCE_REGEXP, SourceStyleSupport::LIST_MARKER_REGEXP

Instance Attribute Summary

Attributes inherited from Mdlint::Linter::Rule

#violations

Instance Method Summary collapse

Methods included from SourceStyleSupport

#fenced_lines, #inline_tokens, #line_is_blank?, #lines, #outside_fence?

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mdlint/linter/rules/source_style.rb', line 148

def check(_tokens, source)
  source_lines = lines(source)
  in_fence = false
  source_lines.each_with_index do |line, index|
    next unless line.chomp.match?(FENCE_REGEXP)

    if !in_fence && index.positive? && !line_is_blank?(source_lines[index - 1])
      add_violation(message: "Add a blank line before the fenced code block", line: index + 1)
    elsif in_fence && index < source_lines.length - 1 && !line_is_blank?(source_lines[index + 1])
      add_violation(message: "Add a blank line after the fenced code block", line: index + 1)
    end
    in_fence = !in_fence
  end
  @violations
end