Class: Pronto::ESLint

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/eslint.rb

Instance Method Summary collapse

Instance Method Details

#inspect(patch) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pronto/eslint.rb', line 15

def inspect(patch)
  offences = Eslintrb.lint(patch.new_file_full_path, options).compact

  fatals = offences.select { |offence| offence['fatal'] }
    .map { |offence| new_message(offence, nil) }

  return fatals if fatals && !fatals.empty?

  offences.map do |offence|
    patch.added_lines.select { |line| line.new_lineno == offence['line'] }
      .map { |line| new_message(offence, line) }
  end
end

#js_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/pronto/eslint.rb', line 44

def js_file?(path)
  %w[.js .es6 .js.es6 .jsx].include?(File.extname(path))
end

#new_message(offence, line) ⇒ Object



37
38
39
40
41
42
# File 'lib/pronto/eslint.rb', line 37

def new_message(offence, line)
  path = line ? line.patch.delta.new_file[:path] : '.eslintrc'
  level = line ? :warning : :fatal

  Message.new(path, line, level, offence['message'], nil, self.class)
end

#optionsObject



29
30
31
32
33
34
35
# File 'lib/pronto/eslint.rb', line 29

def options
  if ENV['ESLINT_CONFIG']
    JSON.parse(IO.read(ENV['ESLINT_CONFIG']))
  else
    File.exist?('.eslintrc') ? :eslintrc : :defaults
  end
end

#runObject



6
7
8
9
10
11
12
13
# File 'lib/pronto/eslint.rb', line 6

def run
  return [] unless @patches

  @patches.select { |patch| patch.additions > 0 }
    .select { |patch| js_file?(patch.new_file_full_path) }
    .map { |patch| inspect(patch) }
    .flatten.compact
end