jslint-rb 0.1 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ #Jslint-rb
2
+ Borne out of a set of Python/Bash scripts I had lugged around for several years. Decided
3
+ to turn into an actual program instead of setting it up on yet another machine.
4
+
5
+ ###Features:
6
+ * Converted to ruby for easy portability
7
+ * Using ExecJS, so no dependency on a particular JS runtime
8
+ * You can specify a list of globals at the commandline (config file in the future)
9
+ instead of having to update your source code files with ugly /*global x,y,z */
10
+ declarations
11
+ * will also be able to configure multiple formatters easily for use in different environments
12
+ (VIM, RubyMine, Continuous Integration, etc)
13
+
14
+ ###Examples:
15
+
16
+ Install the gem and a js runtime
17
+ ```bash
18
+ gem install jslint-rb
19
+ gem install therubyracer
20
+ ```
21
+
22
+ Setup a config file in your $HOME directory, named .jslint-rb. Then put the following in there
23
+
24
+ ```yaml
25
+ global_vars:
26
+ - default:true
27
+ formatter_proc: VIM
28
+ lint_options:
29
+ - default:true
30
+ ```
31
+ You should be all set to start linting your Javascript! Output will be to STDOUT
32
+
33
+ ```bash
34
+ jslint-rb foo.js
35
+ ```
36
+
37
+ ####TODO:
38
+
39
+ 1. Make a task to pull the latest version of JSHint
40
+ 2. Test with MacVim and Windows
data/bin/jslint-rb CHANGED
@@ -8,6 +8,6 @@ config = JslintRb::OptionParser.new
8
8
  instance = JslintRb::Runner.new(config.filename, config.options)
9
9
  output = instance.execute
10
10
 
11
- formatter = JslintRb::Formatter.new(JslintRb::Formatter::VIM)
11
+ formatter = JslintRb::Formatter.new(config.options[:formatter_proc])
12
12
 
13
13
  formatter.print(output, config.filename)
@@ -7,6 +7,7 @@ module JslintRb
7
7
  @character = jsobject["character"]
8
8
  @reason = jsobject["reason"]
9
9
  @evidence = jsobject["evidence"]
10
+ @evidence.strip! unless @evidence.nil?
10
11
  end
11
12
 
12
13
  end
@@ -12,13 +12,18 @@ module JslintRb
12
12
  VIM = Proc.new do |errors, filename|
13
13
  results = []
14
14
  errors.each do |error|
15
- results << "#{filename}:#{error.line_number}:"\
16
- "#{error.character}:#{error.reason} -- #{error.evidence.strip}"
15
+ fmt_err = "#{filename}:#{error.line_number}:"\
16
+ "#{error.character}:#{error.reason}"
17
+ fmt_err << " | #{error.evidence}" unless error.evidence.nil?
18
+ results << fmt_err
17
19
  end
18
20
  results
19
21
  end
20
22
 
21
23
  def initialize(formatter)
24
+ if formatter.nil?
25
+ puts 'Please enter a formatter class'
26
+ end
22
27
  @command = formatter
23
28
  end
24
29
 
@@ -5,6 +5,7 @@ module JslintRb
5
5
  #see a list of valid command line opts
6
6
  class OptionParser
7
7
  require 'optparse'
8
+ require 'yaml'
8
9
 
9
10
  attr_reader :options
10
11
  attr_reader :filename
@@ -27,6 +28,14 @@ module JslintRb
27
28
  eqnull: true
28
29
  }
29
30
 
31
+ DEFAULT_GLOBALS = {
32
+ Ext: true,
33
+ console: true,
34
+ Compass: true,
35
+ currentUser: true
36
+ }
37
+
38
+
30
39
  def initialize
31
40
  @filename = ARGV[0]
32
41
 
@@ -37,12 +46,55 @@ module JslintRb
37
46
  temp_file: 'jslint_wrap.tmp',
38
47
  #this is passed to JSLint so that it will not falsely call
39
48
  #an undefined error on them
40
- global_vars: "Ext,console,Compass,currentUser",
49
+ global_vars: {},
41
50
  #A hash of JSHINT options
42
- lint_options: {}
43
- #default_options: ['white: false', 'nomen: false', 'undef: true']
51
+ lint_options: {},
52
+ #Formatter proc to use
53
+ formatter_proc: nil
44
54
  }
45
55
 
56
+ parse_config_file
57
+ parse_cmdline_args
58
+ end
59
+
60
+ def load_globals globals
61
+ globals.each do |global|
62
+ key,value = global.split(':')
63
+ if key == 'default'
64
+ @options[:global_vars].merge!(DEFAULT_GLOBALS)
65
+ else
66
+ @options[:global_vars][key] = value
67
+ end
68
+ end
69
+ end
70
+
71
+ def load_lint_configs options
72
+ options.each do |lint_opt|
73
+ key,value = lint_opt.split(':')
74
+ if key == 'default'
75
+ @options[:lint_options].merge!(DEFAULT_OPT)
76
+ else
77
+ @options[:lint_options][key] = value
78
+ end
79
+ end
80
+ end
81
+
82
+ def get_formatter_constant constant
83
+ @options[:formatter_proc] = eval('JslintRb::Formatter::'<< constant)
84
+ end
85
+
86
+ def parse_config_file
87
+ config_file = File.read(File.join(ENV['HOME'],'.jslint-rb')) rescue nil
88
+ return if config_file.nil?
89
+ config = YAML::load(config_file)
90
+ return if config == false
91
+ load_globals config['global_vars'] unless config['global_vars'].nil?
92
+ load_lint_configs config['lint_options'] unless config['lint_options'].nil?
93
+ get_formatter_constant config['formatter_proc'] unless config['formatter_proc'].nil?
94
+ end
95
+
96
+ def parse_cmdline_args
97
+
46
98
  ARGV.options do |opt|
47
99
  opt.banner = "Usage: jslint-rb [FILENAME] [OPTIONS]"
48
100
 
@@ -50,15 +102,19 @@ module JslintRb
50
102
  "List of JsHint options. "\
51
103
  "Passing default : true will "\
52
104
  "enable default options") do |options|
105
+ load_lint_configs options
106
+ end
53
107
 
54
- options.each do |lint_opt|
55
- key,value = lint_opt.split(':')
56
- if key == 'default'
57
- @options[:lint_options].merge!(DEFAULT_OPT)
58
- else
59
- @options[:lint_options][key] = value
60
- end
61
- end
108
+ opt.on('-g', "--globals ['GLOBALVAR' : BOOL]", Array,
109
+ "array of globals. Set them to true to allow them to be overridden") do |options|
110
+ load_globals options
111
+ end
112
+
113
+ opt.on('-f', "--format FORMAT", String,
114
+ "The string constant for the Formatter proc:
115
+ VIM -- format for VIM make errorformat"
116
+ ) do |option|
117
+ get_formatter_constant option
62
118
  end
63
119
 
64
120
  opt.on_tail("-h", "--help", 'Show this message') do
@@ -67,7 +123,6 @@ module JslintRb
67
123
  end
68
124
  opt.parse!
69
125
  end
70
-
71
126
  end
72
127
 
73
128
  end
@@ -15,10 +15,11 @@ module JslintRb
15
15
  end
16
16
 
17
17
  def execute
18
- set_globals
18
+ #set_globals
19
19
  context = ExecJS.compile(File.read(@config[:lint_location]))
20
- output = context.exec("JSHINT(#{MultiJson.dump(File.read(@config[:temp_file]))},"\
21
- "#{MultiJson.dump(@config[:lint_options])});"\
20
+ output = context.exec("JSHINT(#{MultiJson.dump(File.read(@filename))},"\
21
+ "#{MultiJson.dump(@config[:lint_options])},"\
22
+ "#{MultiJson.dump(@config[:global_vars])});"\
22
23
  "return JSHINT.errors")
23
24
  output.compact.map {|x| JslintRb::Error.new(x) }
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jslint-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.5'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - '='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
21
  version: 1.3.2
22
22
  type: :runtime
@@ -24,7 +24,7 @@ dependencies:
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - '='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 1.3.2
30
30
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - '='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
37
  version: 1.3.6
38
38
  type: :runtime
@@ -40,10 +40,13 @@ dependencies:
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - '='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: 1.3.6
46
- description: Ruby wrapper for JSHint, using execJS
46
+ description: Ruby wrapper to run and format JSHint, using execJS. It supports commandline
47
+ arguments or a YAML config file to specify global variables and JSHint configs.
48
+ JavaScript runtime independent so you can start linting without the hassle, on Linux,
49
+ Windows, or Mac
47
50
  email: chris@uberbrodt.net
48
51
  executables:
49
52
  - jslint-rb
@@ -58,7 +61,7 @@ files:
58
61
  - lib/jslint-rb/js/jshint.js
59
62
  - lib/jslint-rb/formatter.rb
60
63
  - lib/jslint-rb.rb
61
- - README
64
+ - README.md
62
65
  - GPL-3-LICENSE
63
66
  homepage: https://github.com/uberbrodt/jslint-rb
64
67
  licenses: []
@@ -83,5 +86,5 @@ rubyforge_project:
83
86
  rubygems_version: 1.8.23
84
87
  signing_key:
85
88
  specification_version: 3
86
- summary: Ruby wrapper to run and format JSLint, using execJS
89
+ summary: Ruby wrapper to run and format JSHint, using execJS.
87
90
  test_files: []
data/README DELETED
@@ -1,37 +0,0 @@
1
- Borne out of a set of Python/Bash scripts I had lugged around for several years. Decided
2
- to turn into an actual program instead of setting it up on yet another machine.
3
-
4
- Features:
5
- * Converted to ruby for easy portability
6
- * Using ExecJS, so no dependency on a particular JS runtime
7
- * You can specify a list of globals at the commandline (config file in the future)
8
- instead of having to update your source code files with ugly /*global x,y,z */
9
- declarations
10
- * will also be able to configure multiple formatters easily for use in different environments
11
- (VIM, RubyMine, Continuous Integration, etc)
12
-
13
- UPDATE June 15th 2012:
14
-
15
- Got ExecJS integrated, and enabled the formatting so it will work with VIM.
16
- Also converted to using JSHint instead of JSLint. General refactoring as well,
17
- but should be good enough for a general beta release
18
-
19
- UPDATE June 6th 2012:
20
-
21
- Created a ruby version of this script. Can be used like this:
22
-
23
- jslint foo.js [OPTIONS]
24
-
25
- requires ruby 1.9.x
26
-
27
- This version is missing the pretty formatting for use with VIM/Emacs, but that should be added
28
- soon.
29
-
30
- TODO:
31
-
32
- 1. Make a task to pull the latest version of JSHint
33
- 2. Test with MacVim
34
- 3. Allow the specification of formatters at the command line
35
- 4. Allow the spec of Globals from the commandline
36
- 5. Allow configuration via a YAML file in the users home directory, so that you
37
- don't have to input the same commandline options each time