Class: RogerEslint::Lint

Inherits:
Object
  • Object
show all
Defined in:
lib/roger_eslint/lint.rb

Overview

JS linter plugin for Roger

Constant Summary collapse

ESLINT_SEVERITIES =

ESLint severities translated into a human readable format

{
  1 => "Warning",
  2 => "Error"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Lint

Returns a new instance of Lint.

Parameters:

  • options (Hash) (defaults to: {})

    The options

Options Hash (options):

  • :match (Array)

    Files to match

  • :skip (Array[Regexp])

    Array of regular expressions to skip files

  • (false) (Boolean)

    :fail_on_warning Wether or not to fail test on warnings

  • :eslint (String, nil)

    eslint command, if nil will search for the command Preferring the local node_modules path.

  • :eslint_options (Array)

    An array of eslint options; make sure you have the commandline flag and the value in separate elments, so: ‘[“–global”, “$”]`



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/roger_eslint/lint.rb', line 23

def initialize(options = {})
  @options = {
    match: ["html/**/*.js"],
    skip: [%r{vendor\/.*\.js\Z}],
    fail_on_warning: false,
    eslint: nil,
    eslint_options: []
  }

  @options.update(options) if options
end

Instance Method Details

#call(test, options) ⇒ Object

Parameters:

  • options (Hash)

    The options

Options Hash (options):

  • :match (Array)

    Files to match

  • :skip (Array[Regexp])

    Array of regular expressions to skip files



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/roger_eslint/lint.rb', line 46

def call(test, options)
  @_call_options = {}.update(@options).update(options)

  detect_eslint(test)

  test.log(self, "ESLinting files")

  files = test.get_files(@_call_options[:match], @_call_options[:skip])

  lint(test, files).empty?
ensure
  @_call_options = {}
end

#lint(test, file_paths) ⇒ Array

Returns failed files.

Returns:

  • (Array)

    failed files



36
37
38
39
40
41
# File 'lib/roger_eslint/lint.rb', line 36

def lint(test, file_paths)
  output = `#{eslint_command(file_paths)}`
  file_lints = JSON.parse(output)

  process_lint_results(test, file_lints)
end