From 03cd7e71b88003d86af20b791480da008bdfe171 Mon Sep 17 00:00:00 2001 From: Sydney Young Date: Wed, 15 Jun 2016 16:06:58 -0400 Subject: [PATCH] Add ESlint to Travis; only style check the diff Resolves #1581 - Adds 'rake check_style' - Fixes rubocop's TargetRubyVersion - Removes HoundCI config --- .eslintrc.yml | 4 +++ .gitignore | 4 ++- .hound.yml | 7 ---- .rubocop.yml | 2 ++ .travis.yml | 19 ++++------ lib/tasks/style_checker.rake | 69 ++++++++++++++++++++++++++++++++++++ package.json | 32 +++++++++++++++++ 7 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 .eslintrc.yml delete mode 100644 .hound.yml create mode 100644 lib/tasks/style_checker.rake create mode 100644 package.json diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 000000000..9a6424826 --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,4 @@ +extends: standard +installedESLint: true +plugins: + - standard diff --git a/.gitignore b/.gitignore index a359e8362..dac93eff3 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,6 @@ diff.py Vagrantfile *.sublime* coverage/ -.env \ No newline at end of file +.env +node_modules/* +*.swp diff --git a/.hound.yml b/.hound.yml deleted file mode 100644 index e4f78f445..000000000 --- a/.hound.yml +++ /dev/null @@ -1,7 +0,0 @@ -ruby: - config_file: .rubocop.yml -javascript: - enabled: false -eslint: - enabled: true -fail_on_violations: true diff --git a/.rubocop.yml b/.rubocop.yml index fba1c4a28..f49c6a840 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,5 @@ AllCops: + TargetRubyVersion: 2.3 Include: - '**/Rakefile' - '**/config.ru' @@ -17,6 +18,7 @@ AllCops: - 'lib/**/*.erb' - 'script/**/*' - 'vendor/**/*' + - 'node_modules/**/*' # disable the documentation check at the start of classes / modules Documentation: Enabled: false diff --git a/.travis.yml b/.travis.yml index 501b6a199..543480ea1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,15 +11,14 @@ env: secure: IHUYMK2spxorl9lUeAbAfT6btuP2qRT615bUnEuUDgYrXf9y1CdQprYWJaygou/+6aWmLL0NnxYSpOxi40bHgMKeUQnTjXVbkkqQ1Tml3cMSsjkBrx7CNUygHvDDzCQCEC6m9uZjUKMZAVzVSWlOQhSMKR7MtdsSvMCrKIgA2pM= before_install: + - if [ $TRAVIS_PULL_REQUEST == "false" ]; then git clone https://github.com/$TRAVIS_REPO_SLUG.git $TRAVIS_REPO_SLUG && cd $TRAVIS_REPO_SLUG && git checkout -qf $TRAVIS_BRANCH; fi + - if [ $TRAVIS_PULL_REQUEST == "true" ]; then git checkout -qf master && git checkout -qf $TRAVIS_COMMIT; fi - convert -version - gs -v - # these are all for JS linters, uncomment when they are to be used - # - npm --version - # - node --version - # - npm install -g npm - # - echo "$(npm --version) (after npm upgrade)" - # - npm install -g jshint jscs - # - npm install jshint-stylish + - npm --version + - node --version + - npm install -g npm + - npm i - 'echo ''gem: --no-ri --no-rdoc'' > ~/.gemrc' bundler_args: --without=production staging development @@ -31,11 +30,7 @@ before_script: - bundle exec rake db:schema:load script: - # JS linters, uncomment once code passes - # - jscs . - # - jshint --reporter=node_modules/jshint-stylish/stylish.js . - # rubocop Ruby / Rails linter - - bundle exec rubocop -D + - bundle exec rake check_style - xvfb-run -a bundle exec rake # From Travis CI Support: This will route jobs to our beta build environment, diff --git a/lib/tasks/style_checker.rake b/lib/tasks/style_checker.rake new file mode 100644 index 000000000..a69385738 --- /dev/null +++ b/lib/tasks/style_checker.rake @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +RUBY = /\.(rb)|(rake)$/ +JS = /\.jsx?$/ +RUBY_PASS = %w(true no\ offenses files\ found).freeze +JS_PASS = %w(true files\ found).freeze + +EXISTING_FILES = /^[^D].*/ +FILE = /^[A-Z]\t(.*)$/ + +desc 'Style checks files that differ from master' +task :check_style do + puts diff_output + puts "\nRunning rubocop..." + puts check_ruby + puts "\nRunning eslint..." + puts check_js + exit evaluate +end + +def diff_output + "Files found in the diff:\n#{diff.join("\n")}\n" +end + +def check(type:, regex:, checker:) + files = files_that_match regex + return "No #{type} files found!" if files.empty? + "#{send(checker, files)}\n#{system send(checker, files)}\n" +end + +def check_ruby + @ruby_results ||= check(type: 'ruby', regex: RUBY, checker: :rubocop) +end + +def check_js + @js_results ||= check(type: 'javascript', regex: JS, checker: :eslint) +end + +def evaluate + return 0 if passed? + 1 +end + +def passed? + RUBY_PASS.any? { |m| check_ruby.include? m } && + JS_PASS.any? { |m| check_js.include? m } +end + +def rubocop(files) + "rubocop -D --force-exclusion #{files}" +end + +def eslint(files) + "npm run lint #{files}" +end + +def diff + @diff ||= process_diff +end + +def process_diff + all = `git diff master --name-status` + existing_files = all.split("\n").grep(EXISTING_FILES) + existing_files.map { |f| FILE.match(f)[1] } +end + +def files_that_match(regex) + diff.grep(regex).join(' ') +end diff --git a/package.json b/package.json new file mode 100644 index 000000000..5e821a07f --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "reservations", + "version": "6.2.0", + "description": "[![Build Status](https://travis-ci.org/YaleSTC/reservations.svg?branch=master)](https://travis-ci.org/YaleSTC/reservations) [![Code Climate](https://codeclimate.com/github/YaleSTC/reservations/badges/gpa.svg)](https://codeclimate.com/github/YaleSTC/reservations) [![Test Coverage](https://codeclimate.com/github/YaleSTC/reservations/badges/coverage.svg)](https://codeclimate.com/github/YaleSTC/reservations) [![Dependency Status](https://gemnasium.com/YaleSTC/reservations.svg)](https://gemnasium.com/YaleSTC/reservations) [![Inline docs](http://inch-ci.org/github/yalestc/reservations.svg?branch=master&style=flat)](http://inch-ci.org/github/yalestc/reservations)", + "main": "index.js", + "directories": { + "doc": "doc", + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "lint": "node_modules/.bin/eslint" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/YaleSTC/reservations.git" + }, + "author": "", + "license": "MIT", + "bugs": { + "url": "https://github.com/YaleSTC/reservations/issues" + }, + "homepage": "https://github.com/YaleSTC/reservations#readme", + "devDependencies": { + "eslint": "^2.12.0", + "eslint-config-standard": "^5.3.1", + "eslint-plugin-standard": "^1.3.2" + }, + "dependencies": { + "eslint-plugin-promise": "^1.3.2" + } +}