Class: RemLint::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/remlint/runner.rb

Overview

Walks paths, extracts Remind out of them, runs the enabled rules, and drops whatever the file itself asked to be quiet about.

Constant Summary collapse

DISABLE =

# remlint:disable UnbalancedBlocks, TrailingWhitespace, or all. Recognised after either comment character, and mid-line, so it can sit at the end of the line it applies to.

/[#;]\s*remlint:disable\s+(?<rules>[\w\s,-]+)/
ALL =
"all"
CANDIDATE =

Extensions worth opening when walking a directory. A file with no extension is opened too, because examples/astro and examples/ansitext have none and are where most of the Remind in that directory lives.

/\.(rem|sh|bash)\z|\A[^.]+\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Config.default) ⇒ Runner

Returns a new instance of Runner.



27
28
29
30
# File 'lib/remlint/runner.rb', line 27

def initialize(config = Config.default)
  @config = config
  @warnings = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



25
26
27
# File 'lib/remlint/runner.rb', line 25

def config
  @config
end

#warningsObject (readonly)

Returns the value of attribute warnings.



25
26
27
# File 'lib/remlint/runner.rb', line 25

def warnings
  @warnings
end

Instance Method Details

#files(paths) ⇒ Object

The files a set of paths expands to: a named file is taken as given, a named directory is walked.



39
40
41
# File 'lib/remlint/runner.rb', line 39

def files(paths)
  Array(paths).flat_map { |path| expand(path) }.uniq.reject { |path| config.excluded?(path) }
end

#lint_file(path) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/remlint/runner.rb', line 43

def lint_file(path)
  text = read(path)

  if text.nil?
    []
  else
    lint_text(path, text)
  end
end

#lint_text(path, text) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/remlint/runner.rb', line 53

def lint_text(path, text)
  rules = config.rules

  Extractors.extract(path, text).flat_map do |source|
    lint_source(Document.new(source), rules)
  end
end

#run(paths) ⇒ Object

Returns the offences, worst-placed first in reading order.



33
34
35
# File 'lib/remlint/runner.rb', line 33

def run(paths)
  files(paths).flat_map { |path| lint_file(path) }.sort_by(&:sort_key)
end