Class: Eslintrb::Lint
- Inherits:
-
Object
- Object
- Eslintrb::Lint
- 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.("../../js/eslint.js", __FILE__)
Instance Method Summary collapse
-
#initialize(options = nil, globals = nil) ⇒ Lint
constructor
A new instance of Lint.
- #lint(source) ⇒ Object
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( = nil, globals = nil) if == :defaults then @options = DEFAULTS.dup elsif == :eslintrc then raise '`.eslintrc` is not exist on current working directory.' unless File.exist?('./.eslintrc') @options = MultiJson.load(File.read('./.eslintrc')) elsif .instance_of? Hash then @options = .dup # @options = DEFAULTS.merge(options) elsif .nil? @options = nil else raise 'Unsupported option for Eslintrb: ' + .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 |