class SlimLint::Linter

Base implementation for all lint checks.

@abstract

Attributes

config[R]
document[R]
lints[R]

List of lints reported by this linter.

@todo Remove once spec/support/shared_linter_context returns an array of

lints for the subject instead of the linter itself.

Public Class Methods

new(config) click to toggle source

Initializes a linter with the specified configuration.

@param config [Hash] configuration for this linter

# File lib/slim_lint/linter.rb, line 21
def initialize(config)
  @config = config
  @lints = []
end

Public Instance Methods

name() click to toggle source

Returns the simple name for this linter.

@return [String]

# File lib/slim_lint/linter.rb, line 40
def name
  self.class.name.split('::').last
end
run(document) click to toggle source

Runs the linter against the given Slim document.

@param document [SlimLint::Document]

# File lib/slim_lint/linter.rb, line 29
def run(document)
  @document = document
  @lints = []
  @disabled_lines = nil
  trigger_pattern_callbacks(document.sexp)
  @lints
end

Private Instance Methods

disabled_for_line?(line) click to toggle source
# File lib/slim_lint/linter.rb, line 67
def disabled_for_line?(line)
  disabled_lines.include?(line)
end
disabled_lines() click to toggle source
# File lib/slim_lint/linter.rb, line 71
def disabled_lines
  @disabled_lines ||= begin
    currently_disabled = false
    @document.source_lines.each_with_index.reduce([]) do |lines, pair|
      line = pair[0]
      line_number = pair[1] + 1

      if line =~ %r{/ slim-lint:disable #{linter_name}}
        currently_disabled = true
      elsif line =~ %r{/ slim-lint:enable #{linter_name}}
        currently_disabled = false
      elsif currently_disabled
        lines << line_number
      end
      lines
    end
  end
end
linter_name() click to toggle source
# File lib/slim_lint/linter.rb, line 90
def linter_name
  @linter_name ||= self.class.name.split('::').last
end
parse_ruby(source) click to toggle source

Parse Ruby code into an abstract syntax tree.

@param source [String] Ruby code to parse @return [AST::Node]

# File lib/slim_lint/linter.rb, line 62
def parse_ruby(source)
  @ruby_parser ||= SlimLint::RubyParser.new
  @ruby_parser.parse(source)
end
report_lint(node, message) click to toggle source

Record a lint for reporting back to the user.

@param node [#line] node to extract the line number from @param message [String] error/warning to display to the user

# File lib/slim_lint/linter.rb, line 52
def report_lint(node, message)
  return if disabled_for_line?(node.line)

  @lints << SlimLint::Lint.new(self, @document.file, node.line, message)
end