Class: HamlLint::Linter::EmptyScript

Inherits:
HamlLint::Linter show all
Includes:
HamlLint::LinterRegistry
Defined in:
lib/haml_lint/linter/empty_script.rb

Overview

Checks for empty scripts.

Instance Attribute Summary

Attributes inherited from HamlLint::Linter

#lints

Instance Method Summary collapse

Methods included from HamlLint::LinterRegistry

extract_linters_from, included

Methods inherited from HamlLint::Linter

autocorrect_priority, autocorrect_safe?, #initialize, #name, ruby_parser, #run, #run_or_raise, supports_autocorrect?, #supports_autocorrect?

Methods included from HamlVisitor

#visit, #visit_children

Constructor Details

This class inherits a constructor from HamlLint::Linter

Instance Method Details

#after_visit_root(node) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/haml_lint/linter/empty_script.rb', line 22

def after_visit_root(node)
  super
  return if deleted_lines.empty?

  kept = document.source_lines.reject.with_index do |_, index|
    deleted_lines.include?(index)
  end
  apply_autocorrect(kept.join("\n"))
end

#visit_silent_script(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/haml_lint/linter/empty_script.rb', line 11

def visit_silent_script(node)
  return unless /\A\s*\Z/.match?(node.script)

  # Only a childless `-` can be deleted; a `-` with children is degenerate
  # but is still reported.
  deletable = node.children.empty?
  deleted_lines << (node.line - 1) if autocorrect? && deletable
  record_lint(node, 'Empty script should be removed',
              corrected: autocorrect? && deletable)
end