Class: Eslintrb::Lint

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

Constant Summary collapse

Error =
ExecJS::Error
DEFAULTS =

Default options for compilation

{
  rules: {
    'no-bitwise' => 2,
    'curly' => 2,
    'eqeqeq' => 2,
    'guard-for-in' => 2,
    'no-use-before-define' => 2,
    'no-caller' => 2,
    'no-new-func' => 2,
    'no-plusplus' => 2,
    'no-undef' => 2,
    'strict' => 2
  },
  env: {
    'browser' => true
  }
}
SourcePath =
File.expand_path("../../js/eslint.js", __FILE__)

Instance Method Summary collapse

Constructor Details

#initialize(options = nil, globals = nil) ⇒ Lint

Returns a new instance of Lint.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eslintrb/lint.rb', line 32

def initialize(options = nil, globals = nil)

  if options == :defaults then
    @options = DEFAULTS.dup
  elsif options == :eslintrc then
    raise '`.eslintrc` is not exist on current working directory.' unless File.exist?('./.eslintrc')
    @options = MultiJson.load(File.read('./.eslintrc'))
  elsif options.instance_of? Hash then
    @options = options.dup
    # @options = DEFAULTS.merge(options)
  elsif options.nil?
    @options = nil
  else
    raise 'Unsupported option for Eslintrb: ' + options.to_s
  end

  @globals = globals

  @context = ExecJS.compile("var window = {}; \n" + File.open(SourcePath, "r:UTF-8").read)
end

Instance Method Details

#lint(source) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eslintrb/lint.rb', line 53

def lint(source)
  source = source.respond_to?(:read) ? source.read : source.to_s

  js = ["var errors;"]
  if @options.nil? and @globals.nil? then
    js << "errors = window.eslint.verify(#{MultiJson.dump(source)}, {});"
  elsif @globals.nil? then
    js << "errors = window.eslint.verify(#{MultiJson.dump(source)}, #{MultiJson.dump(@options)});"
  else
    globals_hash = Hash[*@globals.product([false]).flatten]
    @options = @options.merge({globals: globals_hash})
    js << "errors = window.eslint.verify(#{MultiJson.dump(source)}, #{MultiJson.dump(@options)});"
  end
  js << "return errors;"

  @context.exec js.join("\n")
end