From 9bc64627ebf7a47aa21689c5f312c21afc93bb61 Mon Sep 17 00:00:00 2001 From: Matt Robenolt Date: Mon, 4 Jan 2016 18:35:04 -0800 Subject: [PATCH] Use eslint Fixes GH-386 --- .eslintrc | 70 ++++++++++++++++++++++++++++++++++++++++++++++ .jshintrc | 11 -------- Gruntfile.js | 26 ++++++++--------- package.json | 1 + plugins/angular.js | 4 +-- plugins/console.js | 4 +-- src/raven.js | 22 +++++++++------ src/utils.js | 8 +++--- 8 files changed, 103 insertions(+), 43 deletions(-) create mode 100644 .eslintrc delete mode 100644 .jshintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 000000000000..6d2ae3506864 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,70 @@ +{ + "env": { + "jasmine": true, + "node": true, + "mocha": true, + "browser": true, + "builtin": true + }, + "globals": { + "require": false + }, + "rules": { + "block-scoped-var": 2, + "dot-notation": [ + 2, + { + "allowKeywords": true + } + ], + "eqeqeq": [ + 2, + "allow-null" + ], + "guard-for-in": 2, + "new-cap": 2, + "no-caller": 2, + "no-cond-assign": [ + 2, + "except-parens" + ], + "no-debugger": 2, + "no-empty": 2, + "no-eval": 2, + "no-extend-native": 2, + "no-extra-parens": 2, + "no-irregular-whitespace": 2, + "no-iterator": 2, + "no-loop-func": 2, + "no-multi-str": 2, + "no-new": 2, + "no-proto": 2, + "no-script-url": 2, + "no-sequences": 2, + "no-shadow": 2, + "no-undef": 2, + "no-unused-vars": [ + 2, + {"args": "none"} + ], + "no-with": 2, + "quotes": [ + 2, + "single", + "avoid-escape" + ], + "semi": [ + 0, + "never" + ], + "strict": [ + 2, + "global" + ], + "valid-typeof": 2, + "wrap-iife": [ + 2, + "inside" + ] + } +} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 48e872bea2b7..000000000000 --- a/.jshintrc +++ /dev/null @@ -1,11 +0,0 @@ -{ - "es3": true, - "globalstrict": true, - "browser": true, - "predef": [ - "console", - "module", - "require", - "JSON" - ] -} diff --git a/Gruntfile.js b/Gruntfile.js index cfb932256001..d1340a4a703f 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,6 +1,6 @@ -module.exports = function(grunt) { - "use strict"; +'use strict'; +module.exports = function(grunt) { var _ = require('lodash'); var path = require('path'); var through = require('through2'); @@ -14,12 +14,12 @@ module.exports = function(grunt) { var plugins = grunt.option('plugins'); // Create plugin paths and verify they exist plugins = _.map(plugins ? plugins.split(',') : [], function (plugin) { - var path = 'plugins/' + plugin + '.js'; + var p = 'plugins/' + plugin + '.js'; - if(!grunt.file.exists(path)) + if(!grunt.file.exists(p)) throw new Error("Plugin '" + plugin + "' not found in plugins directory."); - return path; + return p; }); // custom browserify transformer to re-write plugins to @@ -136,7 +136,7 @@ module.exports = function(grunt) { compress: { dead_code: true, global_defs: { - "TEST": false + 'TEST': false } } }, @@ -151,11 +151,8 @@ module.exports = function(grunt) { all: ['build/**/*.map'] }, - jshint: { - options: { - jshintrc: '.jshintrc' - }, - all: ['Gruntfile.js', 'src/**/*.js', 'plugins/**/*.js'] + eslint: { + target: ['Gruntfile.js', 'src/**/*.js', 'plugins/**/*.js'] }, mocha: { @@ -270,8 +267,7 @@ module.exports = function(grunt) { grunt.registerMultiTask('fixSourceMaps', function () { this.files.forEach(function (f) { - var result; - var sources = f.src.filter(function (filepath) { + f.src.filter(function (filepath) { if (!grunt.file.exists(filepath)) { grunt.log.warn('Source file "' + filepath + '" not found.'); return false; @@ -294,7 +290,6 @@ module.exports = function(grunt) { // Grunt contrib tasks grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-clean'); - grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-connect'); grunt.loadNpmTasks('grunt-contrib-copy'); @@ -305,6 +300,7 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-s3'); grunt.loadNpmTasks('grunt-gitinfo'); grunt.loadNpmTasks('grunt-sri'); + grunt.loadNpmTasks('grunt-eslint'); // Build tasks grunt.registerTask('_prep', ['clean', 'gitinfo', 'version']); @@ -317,7 +313,7 @@ module.exports = function(grunt) { grunt.registerTask('dist', ['build.core', 'copy:dist']); // Test task - grunt.registerTask('test', ['jshint', 'browserify.core', 'browserify:test', 'mocha']); + grunt.registerTask('test', ['eslint', 'browserify.core', 'browserify:test', 'mocha']); // Webserver tasks grunt.registerTask('run:test', ['connect:test']); diff --git a/package.json b/package.json index 3044584b4fba..e5f9119f849e 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "grunt-contrib-copy": "^0.8.2", "grunt-contrib-jshint": "^0.11.3", "grunt-contrib-uglify": "^0.11.0", + "grunt-eslint": "^17.3.1", "grunt-gitinfo": "^0.1.7", "grunt-mocha": "^0.4.15", "grunt-release": "^0.13.0", diff --git a/plugins/angular.js b/plugins/angular.js index 4b4cff987554..ed886b597d19 100644 --- a/plugins/angular.js +++ b/plugins/angular.js @@ -25,9 +25,9 @@ function angularPlugin(Raven, angular) { ['Raven', '$delegate', exceptionHandler]); } - function exceptionHandler(Raven, $delegate) { + function exceptionHandler(R, $delegate) { return function (ex, cause) { - Raven.captureException(ex, { + R.captureException(ex, { extra: { cause: cause } }); $delegate(ex, cause); diff --git a/plugins/console.js b/plugins/console.js index 3f2b14a4966f..bf6180426d7e 100644 --- a/plugins/console.js +++ b/plugins/console.js @@ -13,8 +13,8 @@ function consolePlugin(Raven, console) { logLevels = ['debug', 'info', 'warn', 'error'], level = logLevels.pop(); - var logForGivenLevel = function(level) { - var originalConsoleLevel = console[level]; + var logForGivenLevel = function(l) { + var originalConsoleLevel = console[l]; // warning level is the only level that doesn't map up // correctly with what Sentry expects. diff --git a/src/raven.js b/src/raven.js index f5fbacc75665..b44e4fa7d543 100644 --- a/src/raven.js +++ b/src/raven.js @@ -56,7 +56,7 @@ function Raven() { this._plugins = []; this._startTime = now(); - for (var method in this._originalConsole) { + for (var method in this._originalConsole) { // eslint-disable-line guard-for-in this._originalConsoleMethods[method] = this._originalConsole[method]; } } @@ -98,7 +98,7 @@ Raven.prototype = { if (options) { each(options, function(key, value){ // tags and extra are special and need to be put into context - if (key == 'tags' || key == 'extra') { + if (key === 'tags' || key === 'extra') { self._globalContext[key] = value; } else { self._globalOptions[key] = value; @@ -535,7 +535,9 @@ Raven.prototype = { // IE9 if quirks try { document.fireEvent('on' + evt.eventType.toLowerCase(), evt); - } catch(e) {} + } catch(e) { + // Do nothing + } } }, @@ -555,7 +557,7 @@ Raven.prototype = { // Make a copy of the arguments var args = [].slice.call(arguments); var originalCallback = args[0]; - if (typeof (originalCallback) === 'function') { + if (typeof originalCallback === 'function') { args[0] = self.wrap(originalCallback); } @@ -591,7 +593,9 @@ Raven.prototype = { if (fn && fn.handleEvent) { fn.handleEvent = self.wrap(fn.handleEvent); } - } catch (err) {} // can sometimes get 'Permission denied to access property "handle Event' + } catch (err) { + // can sometimes get 'Permission denied to access property "handle Event' + } return orig.call(this, evt, self.wrap(fn), capture, secure); }; }); @@ -658,7 +662,7 @@ Raven.prototype = { return dsn; }, - _handleOnErrorStackInfo: function(stackInfo, options) { + _handleOnErrorStackInfo: function() { // if we are intentionally ignoring errors via onerror, bail out if (!this._ignoreOnError) { this._handleStackInfo.apply(this, arguments); @@ -712,9 +716,9 @@ Raven.prototype = { normalized.in_app = !( // determine if an exception came from outside of our app // first we check the global includePaths list. - (!!this._globalOptions.includePaths.test && !this._globalOptions.includePaths.test(normalized.filename)) || + !!this._globalOptions.includePaths.test && !this._globalOptions.includePaths.test(normalized.filename) || // Now we check for fun, if the function name is Raven or TraceKit - /(Raven|TraceKit)\./.test(normalized['function']) || + /(Raven|TraceKit)\./.test(normalized.function) || // finally, we do a last ditch effort and check for raven.min.js /raven\.(min\.)?js$/.test(normalized.filename) ); @@ -762,7 +766,7 @@ Raven.prototype = { }, _processException: function(type, message, fileurl, lineno, frames, options) { - var stacktrace, i, fullMessage; + var stacktrace, fullMessage; if (!!this._globalOptions.ignoreErrors.test && this._globalOptions.ignoreErrors.test(message)) return; diff --git a/src/utils.js b/src/utils.js index cc40cad82561..71c167bda89f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -19,7 +19,7 @@ function isObject(what) { } function isEmptyObject(what) { - for (var k in what) return false; + for (var _ in what) return false; // eslint-disable-line guard-for-in, no-unused-vars return true; } @@ -87,7 +87,7 @@ function joinRegExp(patterns) { if (isString(pattern)) { // If it's a string, we need to escape it // Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions - sources.push(pattern.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1")); + sources.push(pattern.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1')); } else if (pattern && pattern.source) { // If it's a regexp already, we want to extract the source sources.push(pattern.source); @@ -126,13 +126,13 @@ function uuid4() { return v; }; - return (pad(arr[0]) + pad(arr[1]) + pad(arr[2]) + pad(arr[3]) + pad(arr[4]) + + return pad(arr[0]) + pad(arr[1] + pad(arr[2]) + pad(arr[3]) + pad(arr[4]) + pad(arr[5]) + pad(arr[6]) + pad(arr[7])); } else { // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523 return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, - v = c == 'x' ? r : (r&0x3|0x8); + v = c === 'x' ? r : r&0x3|0x8; return v.toString(16); }); }