damian / jshint

Making it easy to lint your JavaScript assets in any Rails 3.1+ and Rails 4 application.
https://rubygems.org/gems/jshint
MIT License
11 stars 21 forks source link

Run the check with javascript files I want to choose #8

Closed tianhao-au closed 9 years ago

tianhao-au commented 9 years ago

In your document, you said "This Rake task runs JSHint across all the JavaScript assets within the following three folders to ensure that they're lint free. your-rails-project/app/assets/javascripts your-rails-project/vendor/assets/javascripts your-rails-project/lib/assets/javascripts"

How can we use the regex to include and exclude some javascript files?

Is that possible? Thanks.

damian commented 9 years ago

You can configure a list of files and or paths to be passed in to the linter in your jshint.yml config file. That way you can be as granular(down to a file level) or as open(to a directory) as you like. Does this solve your issue?

# your-rails-project/config/jshint.yml

files: [
  'my_file.js',
  'projects/*.js',
  'utils/**/*.js'
]
options:
  boss: true
  browser: true
  ...
tianhao-au commented 9 years ago

Hi Damian, thanks for your reply.

I tried but somehow not working as I expected. For example, my file structure is: rails-project/app/assets/javascripts/dir1/jss rails-project/app/assets/javascripts/dir2/jss rails-project/app/assets/javascripts/dir3/jss rails-project/app/assets/javascripts/dir4/jss rails-project/app/assets/javascripts/1.js rails-project/app/assets/javascripts/2.js

I use the config like either:

files: ['app/assets/javascripts/*/.js'] or files: ["javascripts/*/.js"]

doesn't work for me. I am wondering where the path start from the project directory?

Also, does this Gem provide some features like 'include' or 'exclude'? For example, for dir3 directories, I want to exclude some of certain files? Can I do that?

Thanks.

tianhao-au commented 9 years ago

Not sure for this Gem, we can do something like jslint-on-rails gem, like:

require 'jslint'

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

lint.run

That would be great for me.

damian commented 9 years ago

This gem doesn't have the concept of exclude_paths, this was a conscious decision. You should be able to define a list of paths and or specific files that you want to lint. Consequently you should not have to define a list of things you don't want linting.

The gem already looks in app/assets/javascripts so you don't have to prefix that to any entry in your files array. Based on the example above you should only have to write:

files: [
  'dir1/*.js',
  'dir2/*.js',
  'dir3/*.js'
  'dir4/*.js'
  '1.js'
  '2.js'
]
tianhao-au commented 9 years ago

OK, understand now.

For your response code, files: [ 'dir1/*.js', 'dir2/*.js', 'dir3/*.js' 'dir4/*.js' '1.js' '2.js' ]

If I want to list all javascripts under app/assets/javascripts, do I have to list all of them one by one? Can I use one entry to include all of them? I don't want to add an entry to this config file each time I create a new js directory of js file under javascripts directory.

I tried many ways, unless like your way to specify, or it will include vendor/assets/... files.

Somehow just want to include or exclude js files more flexible and one time cost. Thanks.

damian commented 9 years ago

No you shouldn't have to list them all one by one, the file listing in the default config file should get you pretty far e.g.

files: [
  "**/*.js"
]
tianhao-au commented 9 years ago

I tested it out,

files: [ "**/*.js" ]

will list vendor/assets/... as well, I only want to list app/assets/javascripts. That's the reason I really want exclude path introduced.

tianhao-au commented 9 years ago

Also, can we lint the unit test files under the spec directory?

rails-project/spec/javascripts/*?

tianhao-au commented 9 years ago

Hi Damian,

I would like to add a backlist feature (exclude some javascript files or some lib, vendor javascripts) to your gem. Will you consider to accept a pull request?

damian commented 9 years ago

Of course! :+1:

wli commented 9 years ago

Any progress on this @ysihaoy? I want to exclude vendor/ since it doesn't make any sense to lint files that aren't under your control (who would want to lint jquery or other libraries?). That's the whole point of the vendor/ directory. I've resorted to explicitly listing out my own files, which is less than ideal.

damian commented 9 years ago

I've just released JSHint v1.1.1 which includes the ability to declare excluded paths as part of the of the config.yml.

If someone want's to change the README to reflect this that'd be great!

wli commented 9 years ago

Perfect - thanks! I just used it in my project and it works as advertised.

damian commented 9 years ago

Excellent! :+1:

svbergerem commented 9 years ago

@damian: How do you check files in rails-project/spec/javascripts/*? @ysihaoy already asked this question but got no answer.