Giter Club home page Giter Club logo

jslint_on_rails's Introduction

JSLint on Rails

Build Status

JSLint on Rails is a Ruby library which lets you run the JSLint JavaScript code checker on your Javascript code easily.

Requirements

  • Ruby 1.8.7 or 1.9.2+
  • Javascript engine compatible with execjs (on Mac and Windows it's provided by the OS)
  • JSON engine compatible with multi_json (included in Ruby 1.9, on 1.8 use e.g. json gem)
  • should work with (but doesn't require) Rails 2.x and 3.x

Installation

To use JSLint in Rails 3 you just need to do one thing:

  • add gem 'jslint_on_rails' to bundler's Gemfile

In Rails 2 and other frameworks JSLint on Rails can't be loaded automatically using a Railtie, so you have to load it explicitly. The procedure in this case is:

  • install the gem in your application using whatever technique is recommended for your framework (e.g. using bundler, or by installing the gem manually with gem install jslint_on_rails and loading it with require 'jslint')

  • in your Rakefile, add a line to load the JSLint tasks:

      require 'jslint/tasks'
    

Configuration

It's strongly recommended that you create your own copy of the JSLint config file provided by the gem and tweak it to suit your preferences. To create a new config file from the template in your config directory, call this rake task:

[bundle exec] rake jslint:copy_config

This will create a config file at config/jslint.yml listing all available options. If for some reason you'd like to put the config file at a different location, set the config_path variable somewhere in your Rakefile:

JSLint.config_path = "config/lint.yml"

There are two things you can change in the config file:

  • define which Javascript files are checked by default; you'll almost certainly want to change that, because the default is public/javascripts/**/*.js which means all Javascript files, and you probably don't want JSLint to check entire jQuery, Prototype or whatever other libraries you use - so change this so that only your scripts are checked (you can put multiple entries under "paths:" and "exclude_paths:")
  • enable or disable specific checks - I've set the defaults to what I believe is reasonable, but what's reasonable for me may not be reasonable for you

Running

To start the check, run the rake task:

[bundle exec] rake jslint

You will get a result like this (if everything goes well):

Running JSLint:

checking public/javascripts/Event.js... OK
checking public/javascripts/Map.js... OK
checking public/javascripts/Marker.js... OK
checking public/javascripts/Reports.js... OK

No JS errors found.

If anything is wrong, you will get something like this instead:

Running JSLint:

checking public/javascripts/Event.js... 2 errors:

Lint at line 24 character 15: Use '===' to compare with 'null'.
if (a == null && b == null) {

Lint at line 72 character 6: Extra comma.
},

checking public/javascripts/Marker.js... 1 error:

Lint at line 275 character 27: Missing radix parameter.
var x = parseInt(mapX);


Found 3 errors.
rake aborted!
JSLint test failed.

If you want to test specific file or files (just once, without modifying the config), you can pass paths to include and/or paths to exclude to the rake task:

rake jslint paths=public/javascripts/models/*.js,public/javascripts/lib/*.js exclude_paths=public/javascripts/lib/jquery.js

For the best effect, you should include JSLint check in your Continuous Integration build - that way you'll get immediate notification when you've committed JS code with errors.

Running automatically with Guard

If you want to run JSLint on Rails automatically everytime you save a JS file, check out the guard-jslint-on-rails gem by Ryan Sonnek.

Running from your code

If you would prefer to write your own rake task to run JSLint, you can create and execute the JSLint object manually:

require 'jslint'

lint = JSLint::Lint.new(
  :paths => ['public/javascripts/**/*.js'],
  :exclude_paths => ['public/javascripts/vendor/**/*.js'],
  :config_path => 'config/jslint.yml'
)

lint.run

Additional options

I've added some additional options to JSLint to get rid of some warnings which I thought didn't make sense. They're all disabled by default, but feel free to enable any or all of them if you feel abused by JSLint.

Here's a documentation for all the extra options:

lastsemic

If set to true, this will ignore warnings about missing semicolon after a statement, if the statement is the last one in a block or function, and the whole block is on the same line. I've added this because I like to omit the semicolon in one-liner anonymous functions, in situations like this:

var ids = $$('.entry').map(function(e) { return e.id });

Note: in versions up to 1.0.3, this option also disabled the warning in blocks that span multiple lines, but I've changed that in 1.0.4, because removing a last semicolon in a multi-line block doesn't really affect the readability (while removing the only semicolon in a one-liner like above does, IMHO).

newstat

Allows you to use a call to 'new' as a whole statement, without assigning the result anywhere. Sometimes you want to create an instance of a class, but you don't need to assign it anywhere - the call to constructor starts the action automatically. This includes calls like new Ajax.Request(...) or new Effect.Highlight(...) used when working with Prototype and Scriptaculous.

statinexp

JSLint has a warning that says "Expected an assignment or function call and instead saw an expression" - you get it when you write an expression and you don't use it for anything, like if you wrote such line:

$$('.entry').length;

Just checking the length without assigning it anywhere or passing to any function doesn't make any sense, so it's good that JSLint complains. However, there are some cases where the code makes perfect sense, but JSLint still thinks it doesn't. Examples:

element && element.show();  // call show only if element is not null
selected ? element.show() : element.hide();  // more readable than if & else with brackets

So I've tweaked the code that creates this warning so that it doesn't print it if the code makes sense. Specifically:

  • expressions joined with && or || are accepted if the last one in the line is a statement
  • expressions with ?: are accepted if both alternatives (before and after the colon) are statements

Version 2.0 and JSHint

Some people have asked about JSHint support (some have even made forks replacing JSLint with JSHint). I like the idea of a less strict lint giving the user more control; the reason I haven't integrated JSHint yet is because I want the gem to provide both JSLint and JSHint and let you choose which engine you want to use. The thing is, I can't do that easily because their option sets have diverged - a few options have been changed in JSLint since JSHint was forked off from it, and JSHint has changed or added some other options. There was even a plan to completely redesign the option system in JSHint. So I've decided to hold off the integration until I can be sure that the option sets won't be completely changed again. I'm hoping this might happen as a part of the JSHint Next project, but I don't know what the timeline is.

I haven't been updating JSLint for the same reason - some options have changed there and I'd rather wait until I can update everything together at the same time. If you're impatient, check out some of the forks that use JSHint; some of them have even been released as gems (e.g. see jshint_on_rails) - although as far as I know, none of them have adapted the config file template to match JSHint's option set.

Another thing that's planned for 2.0, whenever that happens, is a redesign of the Javascript runner code to make it possible to use other ways of running Javascript - probably using execjs.

Credits

jslint_on_rails's People

Contributors

botandrose-machine avatar mackuba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jslint_on_rails's Issues

Error: can't convert nil into String

Hi,

I'm using Rails 3.2.3 and jslint_on_rails 1.1 under Windows 7 x64 and this is the error that I get:

$ bundle
Fetching gem metadata from https://rubygems.org/.......
[...]
Installing jslint_on_rails (1.1)
[...]
Using rails (3.2.3)
[...]
Your bundle is complete!

$ rake jslint:copy_config
rake aborted!
can't convert nil into String

$ rake jslint --trace
rake aborted!
can't convert nil into String
c:/Ruby193/lib/ruby/gems/1.9.1/gems/jslint_on_rails-1.1/lib/jslint/utils.rb:14:in `join'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/jslint_on_rails-1.1/lib/jslint/utils.rb:14:in `<module:JSLint>'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/jslint_on_rails-1.1/lib/jslint/utils.rb:4:in `<top (required)>'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/jslint_on_rails-1.1/lib/jslint.rb:2:in `require'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/jslint_on_rails-1.1/lib/jslint.rb:2:in `<top (required)>'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/jslint_on_rails-1.1/lib/jslint_on_rails.rb:1:in `require'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/jslint_on_rails-1.1/lib/jslint_on_rails.rb:1:in `<top (required)>'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.rc.7/lib/bundler/runtime.rb:68:in `require'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.rc.7/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.rc.7/lib/bundler/runtime.rb:66:in `each'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.rc.7/lib/bundler/runtime.rb:66:in `block in require'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.rc.7/lib/bundler/runtime.rb:55:in `each'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.rc.7/lib/bundler/runtime.rb:55:in `require'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.rc.7/lib/bundler.rb:118:in `require'
c:/Users/Tanguy/MyApp/config/application.rb:7:in `<top (required)>'
c:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
c:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
c:/Users/Tanguy/MyApp/Rakefile:5:in `<top (required)>'
c:/Ruby193/lib/ruby/1.9.1/rake/rake_module.rb:25:in `load'
c:/Ruby193/lib/ruby/1.9.1/rake/rake_module.rb:25:in `load_rakefile'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:501:in `raw_load_rakefile'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:82:in `block in load_rakefile'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:81:in `load_rakefile'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:65:in `block in run'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:63:in `run'
c:/Ruby193/bin/rake:32:in `<main>'

Regards,

looking for a new maintainer

Last year I decided to leave the webdev world and concentrate only on Cocoa development. I still love Ruby as a language (especially as compared to ObjC - Swift is much better though), but it was just too much effort to try to keep up with Ruby, JS and Cocoa all the time, and I had to choose something.

I haven't been up to date with all the latest developments in the Ruby world since then, I'm not even sure which version of Ruby is the latest stable one now (2.1 I think?). And obviously I had no use for a Rails JSLint plugin for some time now. I'm not willing to put any more effort into this gem, since I have other projects to maintain too, but if someone is interested in taking it over and maintaining it, I can transfer the ownership of the repo and the RubyGems entry.

Current project status:

It hasn't been updated in a loooong time. The bundled JSLint version hasn't been updated for even longer, ever since Crockford made some major changes there and added some annoying checks which couldn't be disabled (I think he might have changed that back later). If you want to update it, you'd need to either copy my hacks (those 3 extra options) over to the newest JSLint, or just drop these options (I don't really care).

There's some code on master which hasn't been released that gets rid of Rhino and lets you use any execjs-compatible JS runtime. It should work, or at least it did, though you'd have to test it (is execjs still a thing?). There's also some refactoring done on the hackkrk branch and unmerged, and some experiments with output coloring in the last commits on master. You can use those if you want.

Regarding JSHint:

I don't think it makes sense to drop JSLint and switch to JSHint, there are already a few forks that have done just that and re-released the gem as jshint_on_rails. And I don't think it makes sense to try to support both in one gem, as they have significantly diverged in the last few years and the options lists would have to be completely different - but you could try to do that if you want (there are some old commits made in that direction on the jshint branch).

cc @hlascelles @khansen @wok @wyattearp

JSLint should run in WebKit's JSC when availible

As Apple announced in the fall, Java is not going to be preinstalled on Macs starting with Lion.

jslint_on_rails should detect if jsc or Rhino is appropriote and choose the correct interperter for JSLint to keep people from having to install Java.

predef comma-separated string doesn't work

In jslint.yml, I have this:

predef:   'window,$,jQuery' # Names of predefined global variables - comma-separated string

This passes the string on the command like, like so:

java -cp /full/path/to/rhino.jar org.mozilla.javascript.tools.shell.Main /full/path/to/jslint.js [other options],predef="window,$,jQuery",[more other options]

However, the rhino function at the end of lint.js splits on commas to separate options, which means this isn't parsed correctly. The only way I've found to make it work is to put the following (slightly ridiculous) string in jslint.yml:

predef:   'new Array("window").concat(["$"]).concat(["jQuery"])'

Ubuntu install

I tried installing this via gem deployed with apt on Ubuntu. I installed via your instructions:
gem install jslint_on_rails

It fails with:
no such file to load -- jslint/tasks

Adding colors

Hi,

Is there any chance of adding colors in output?
Red for the Lint warnings. Normal color for the code snippet.
Summary may also be in red if the test fails, green otherwise.

I hope you will consider these recommendations.

Cheers

Does not work with ruby 1.9

The library does not work with ruby 1.9 because of dependency of ftools.

Any ideas how we can workaround this problem?

Add License information to gemfile

This will make it show up on rubygems.org. I'm doing due diligence on our gems and need to find out the licenses for all the gems. Having it show up on rubygems.org cuts out the step of having to go to the github repo.

Local options erroneously apply to other files

I have a few js files with local jslint options set using /*jslint eqeqeq:true */ for example. I expect these options to apply to that one source js file and no others. But it seems that jslint_on_rails applies this option to that file and all subsequent files that it checks. I expected it to reset the options to those specified in the jslint.yml file for each new source file that it checks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.