eslintrb 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fad2cfbd4d08f35e745eaf7eebbad1231d97a689
4
+ data.tar.gz: 587a569c8f4e2efa7b92e0f344e505e21a004245
5
+ SHA512:
6
+ metadata.gz: 82edb0a6d9b3f77d9a6e591af3a2bdc7633e12dd2b29930ea54d0415833f3dbedb7b842f56eba2f2b1a5d2cf9e18867d4e651cd3d234483afd8d63d94162ce66
7
+ data.tar.gz: eb0ab434ae303d2083efbbd5f14206cb58e58f852e2763d542212f4ae5662ee0f63cae52c2a5e4c288ea14873ef10e471312e631faedfa23bbd905028beedfa1
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ /.project
@@ -0,0 +1,3 @@
1
+ [submodule "vendor/eslint"]
2
+ path = vendor/eslint
3
+ url = https://github.com/eslint/eslint.git
@@ -0,0 +1,28 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ - 2.2
8
+ - jruby
9
+ before_script: "git submodule update --init --recursive"
10
+ env:
11
+ - EXECJS_RUNTIME=RubyRacer
12
+ - EXECJS_RUNTIME=RubyRhino
13
+ matrix:
14
+ exclude:
15
+ - rvm: 1.8.7
16
+ env: EXECJS_RUNTIME=RubyRhino
17
+ - rvm: 1.9.2
18
+ env: EXECJS_RUNTIME=RubyRhino
19
+ - rvm: 1.9.3
20
+ env: EXECJS_RUNTIME=RubyRhino
21
+ - rvm: 2.0.0
22
+ env: EXECJS_RUNTIME=RubyRhino
23
+ - rvm: 2.1
24
+ env: EXECJS_RUNTIME=RubyRhino
25
+ - rvm: 2.2
26
+ env: EXECJS_RUNTIME=RubyRhino
27
+ - rvm: jruby
28
+ env: EXECJS_RUNTIME=RubyRacer
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in eslintrb.gemspec
4
+ gemspec
5
+
6
+ # Depend on defined ExecJS runtime
7
+ execjs_runtimes = {
8
+ "RubyRacer" => "therubyracer",
9
+ "RubyRhino" => "therubyrhino",
10
+ "Mustang" => "mustang"
11
+ }
12
+
13
+ if ENV["EXECJS_RUNTIME"] && execjs_runtimes[ENV["EXECJS_RUNTIME"]]
14
+ gem execjs_runtimes[ENV["EXECJS_RUNTIME"]], :group => :development
15
+ end
16
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Stereobooster
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # eslintrb
2
+ [![Build Status](https://secure.travis-ci.org/ocke/eslintrb.png?branch=master)](http://travis-ci.org/ocke/eslintrb)
3
+
4
+ Forked from [jshintrb](https://github.com/stereobooster/jshintrb) who did all the hard work.
5
+
6
+ Ruby wrapper for [ESLint](https://github.com/eslint/eslint/). The main difference from [eslint](https://github.com/liquid/eslint_on_rails) it does not depend on Java. Instead it uses [ExecJS](https://github.com/sstephenson/execjs).
7
+
8
+ ## Installation
9
+
10
+ `eslintrb` is available as ruby gem.
11
+
12
+ $ gem install eslintrb
13
+
14
+ Ensure that your environment has a JavaScript interpreter supported by [ExecJS](https://github.com/sstephenson/execjs). Usually, installing `therubyracer` gem is the best alternative.
15
+
16
+ ## Usage
17
+
18
+ ```ruby
19
+ require 'eslintrb'
20
+
21
+ eslintrb.lint(File.read("source.js"))
22
+ # => array of warnings
23
+
24
+ eslintrb.report(File.read("source.js"))
25
+ # => string
26
+ ```
27
+
28
+ Or you can use it with rake
29
+
30
+ ```ruby
31
+ require "eslintrb/eslinttask"
32
+ eslintrb::eslintTask.new :eslint do |t|
33
+ t.pattern = 'javascript/**/*.js'
34
+ t.options = :defaults
35
+ end
36
+ ```
37
+
38
+ When initializing `eslintrb`, you can pass options
39
+
40
+ ```ruby
41
+ eslintrb::Lint.new(:undef => true).lint(source)
42
+ # Or
43
+ eslintrb.lint(source, :undef => true)
44
+ ```
45
+
46
+ [List of all available options](http://www.eslint.com/docs/options/)
47
+
48
+ If you pass `:defaults` as option, it is the same as if you pass following
49
+
50
+ ```
51
+ {
52
+ :bitwise => true,
53
+ :curly => true,
54
+ :eqeqeq => true,
55
+ :forin => true,
56
+ :immed => true,
57
+ :latedef => true,
58
+ :newcap => true,
59
+ :noarg => true,
60
+ :noempty => true,
61
+ :nonew => true,
62
+ :plusplus => true,
63
+ :regexp => true,
64
+ :undef => true,
65
+ :strict => true,
66
+ :trailing => true,
67
+ :browser => true
68
+ }
69
+ ```
70
+
71
+ If you pass `:eslintrc` as option, `.eslintrc` file is loaded as option.
72
+
73
+ ## TODO
74
+
75
+ - add more tests
76
+ - add color reporter. Maybe [colorize](https://github.com/fazibear/colorize)
77
+ - add cli. Support same options as [eslint/node-eslint](https://github.com/eslint/node-eslint/blob/master/lib/cli.js)
@@ -0,0 +1,41 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ [:build, :install, :release].each do |task_name|
4
+ Rake::Task[task_name].prerequisites << :spec
5
+ end
6
+
7
+ require "multi_json"
8
+
9
+ def eslint_version
10
+ package = File.expand_path("../vendor/eslint/package.json", __FILE__)
11
+ MultiJson.load(File.open(package, "r:UTF-8").read)["version"]
12
+ end
13
+
14
+ task :eslint_version do
15
+ p eslint_version
16
+ end
17
+
18
+ require 'submodule'
19
+ Submodule::Task.new do |t|
20
+ t.test do
21
+ sh "npm i"
22
+ sh "npm test"
23
+ # sh "node bin/build"
24
+ end
25
+
26
+ t.after_pull do
27
+ cp "vendor/eslint/build/eslint.js", "lib/js/eslint.js"
28
+ sh "git add lib/js/eslint.js"
29
+ end
30
+ end
31
+
32
+ require "rspec/core/rake_task"
33
+ RSpec::Core::RakeTask.new
34
+
35
+ task :default => :spec
36
+
37
+ #desc "Generate code coverage"
38
+ # RSpec::Core::RakeTask.new(:coverage) do |t|
39
+ # t.rcov = true
40
+ # t.rcov_opts = ["--exclude", "spec"]
41
+ # end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "eslintrb/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "eslintrb"
7
+ s.version = Eslintrb::VERSION
8
+ s.authors = ["stereobooster", "ocke"]
9
+ s.email = ["stereobooster@gmail.com", "ocke@exploder.nl"]
10
+ s.homepage = "https://github.com/ocke/eslintrb"
11
+ s.summary = %q{Ruby wrapper for JSHint}
12
+ s.description = %q{Ruby wrapper for JSHint. The main difference from jshint gem it does not depend on Java. Instead, it uses ExecJS}
13
+ s.license = "MIT"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency "submodule", ">=0.0.3"
23
+ s.add_runtime_dependency "rake"
24
+
25
+ s.add_dependency "multi_json", ">= 1.3"
26
+ s.add_dependency "execjs"
27
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: UTF-8
2
+
3
+ require "eslintrb/version"
4
+ require "eslintrb/lint"
5
+ require "eslintrb/reporter/default"
6
+
7
+ module Eslintrb
8
+
9
+ def self.lint(source, options = nil, globals = nil)
10
+ Lint.new(options, globals).lint(source)
11
+ end
12
+
13
+ def self.report(source, options = nil, globals = nil, out = nil)
14
+ reporter = Reporter::Default.new
15
+ linter = Lint.new(options, globals)
16
+ report = ''
17
+ if source.is_a?(Array) then
18
+ source.each do |src|
19
+ if !src.is_a?(String) then
20
+ p src.to_s
21
+ raise ArgumentError, 'Expected array of strings. Instead get ' + src.class.to_s
22
+ end
23
+ errors = linter.lint(File.read(src))
24
+ rep = reporter.format errors, src
25
+ if out && rep.size > 0 then
26
+ out.puts rep
27
+ end
28
+ report += rep
29
+ end
30
+ else
31
+ errors = linter.lint(source)
32
+ report = reporter.format errors
33
+ if out then
34
+ out.puts report
35
+ end
36
+ end
37
+ report
38
+ end
39
+
40
+ end
@@ -0,0 +1,87 @@
1
+ # Define a task library for running JSHint contexts.
2
+
3
+ require 'rake'
4
+ require 'rake/tasklib'
5
+
6
+ require 'eslintrb'
7
+
8
+ module Eslintrb
9
+
10
+ class EslintTask < ::Rake::TaskLib
11
+ # Name of JSHint task. (default is :jshint)
12
+ attr_accessor :name
13
+
14
+ # Glob pattern to match JavaScript files. (default is './**/*.js')
15
+ attr_accessor :pattern
16
+
17
+ # options
18
+ attr_accessor :options
19
+
20
+ attr_accessor :globals
21
+
22
+ # Whether or not to fail Rake when an error occurs (typically when Jshint check fail).
23
+ # Defaults to true.
24
+ attr_accessor :fail_on_error
25
+
26
+ # Explicitly define the list of JavaScript files to be linted.
27
+ # +js_files+ is expected to be an array of file names (a
28
+ # FileList is acceptable). If both +pattern+ and +js_files+ are
29
+ # used, then the list of JavaScritp files is the union of the two.
30
+ attr_accessor :js_files
31
+
32
+ attr_accessor :exclude_pattern
33
+
34
+ attr_accessor :exclude_js_files
35
+
36
+ # Defines a new task, using the name +name+.
37
+ def initialize(name=:jshint)
38
+ @name = name
39
+ @pattern = nil
40
+ @js_files = nil
41
+ @exclude_pattern = nil
42
+ @exclude_js_files = nil
43
+ @options = nil
44
+ @globals = nil
45
+ @fail_on_error = true
46
+
47
+ yield self if block_given?
48
+ @pattern = './**/*.js' if pattern.nil? && js_files.nil?
49
+ define
50
+ end
51
+
52
+ def define # :nodoc:
53
+
54
+ actual_name = Hash === name ? name.keys.first : name
55
+ unless ::Rake.application.last_comment
56
+ desc "Run JShint"
57
+ end
58
+ task name do
59
+ unless js_file_list.empty?
60
+ result = Eslintrb::report(js_file_list, @options, @globals, STDERR)
61
+ if result.size > 0
62
+ abort("JSHint check failed") if fail_on_error
63
+ end
64
+ end
65
+ end
66
+
67
+ self
68
+ end
69
+
70
+ def evaluate(o) # :nodoc:
71
+ case o
72
+ when Proc then o.call
73
+ else o
74
+ end
75
+ end
76
+
77
+ def js_file_list # :nodoc:
78
+ result = []
79
+ result += js_files.to_a if js_files
80
+ result += FileList[ pattern ].to_a if pattern
81
+ result -= exclude_js_files.to_a if exclude_js_files
82
+ result -= FileList[ exclude_pattern ].to_a if exclude_pattern
83
+ FileList[result]
84
+ end
85
+ end
86
+
87
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: UTF-8
2
+
3
+ require "execjs"
4
+ require "multi_json"
5
+
6
+ module Eslintrb
7
+
8
+ class Lint
9
+ Error = ExecJS::Error
10
+
11
+ # Default options for compilation
12
+ DEFAULTS = {
13
+ rules: {
14
+ 'no-bitwise' => 2,
15
+ 'no-undef' => 2,
16
+ :curly => 2,
17
+ :eqeqeq => 2,
18
+ :strict => 2
19
+ }
20
+ }
21
+
22
+ SourcePath = File.expand_path("../../js/eslint.js", __FILE__)
23
+
24
+ def initialize(options = nil, globals = nil)
25
+
26
+ if options == :defaults then
27
+ @options = DEFAULTS.dup
28
+ elsif options == :jshintrc then
29
+ raise '`.eslintrc` is not exist on current working directory.' unless File.exist?('./.eslintrc')
30
+ @options = MultiJson.load(File.read('./.eslintrc'))
31
+ elsif options.instance_of? Hash then
32
+ @options = options.dup
33
+ # @options = DEFAULTS.merge(options)
34
+ elsif options.nil?
35
+ @options = nil
36
+ else
37
+ raise 'Unsupported option for Eslintrb: ' + options.to_s
38
+ end
39
+
40
+ @globals = globals
41
+
42
+ @context = ExecJS.compile("var window = {}; \n" + File.open(SourcePath, "r:UTF-8").read)
43
+ end
44
+
45
+ def lint(source)
46
+ source = source.respond_to?(:read) ? source.read : source.to_s
47
+
48
+ js = ["var errors;"]
49
+ if @options.nil? and @globals.nil? then
50
+ js << "errors = window.eslint.verify(#{MultiJson.dump(source)}, {});"
51
+ elsif @globals.nil? then
52
+ js << "errors = window.eslint.verify(#{MultiJson.dump(source)}, #{MultiJson.dump(@options)});"
53
+ else
54
+ globals_hash = Hash[*@globals.product([false]).flatten]
55
+ @options = @options.merge({globals: globals_hash})
56
+ js << "errors = window.eslint.verify(#{MultiJson.dump(source)}, #{MultiJson.dump(@options)});"
57
+ end
58
+ js << "return errors;"
59
+
60
+ @context.exec js.join("\n")
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,30 @@
1
+ module Eslintrb
2
+ module Reporter
3
+ class Default
4
+
5
+ def format(errors, file = nil)
6
+ result = ''
7
+ indent = ''
8
+ if file then
9
+ indent = ' '
10
+ end
11
+
12
+ errors.each do |error|
13
+ if error.nil? then
14
+ result += indent + 'fatal error'
15
+ else
16
+ result += indent + 'line ' + error["line"].to_s + ', col ' +
17
+ error["character"].to_s + ', ' + error["reason"].to_s + "\n"
18
+ end
19
+ end
20
+
21
+ if file && result.size > 0 then
22
+ result = 'file: ' + file + "\n" + result
23
+ end
24
+
25
+ result
26
+ end
27
+
28
+ end
29
+ end
30
+ end