From 06635ed4cd7ce6c8b367bca9376280a08cfeffd7 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sun, 27 Oct 2013 21:53:11 +0000 Subject: [PATCH] Add a .jshintrc file and associated jshint grunt task. This .jshintrc is adopted from the jQuery project, with some basic modifications (such as single quotes instead of double quotes). This pretty closely follows our current JS standards and rather closely resembles our PHP standards, especially the love of whitespace. The major changes are enforcing === and always using braces for if statements. props kadamwhite, gnarf37, with mattwiebe and carldanley. see #25187. git-svn-id: https://develop.svn.wordpress.org/trunk@25960 602fd350-edb4-49c9-b593-d223f7449a82 --- .jshintrc | 21 +++++++++++++++ Gruntfile.js | 75 +++++++++++++++++++++++++++++++++++++++++++++------- package.json | 1 + 3 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000000..7c6c4e070a --- /dev/null +++ b/.jshintrc @@ -0,0 +1,21 @@ +{ + "boss": true, + "curly": true, + "eqeqeq": true, + "eqnull": true, + "expr": true, + "immed": true, + "noarg": true, + "quotmark": "single", + "smarttabs": true, + "trailing": true, + "undef": true, + "unused": true, + + "browser": true, + + "globals": { + "jQuery": false, + "wp": false + } +} diff --git a/Gruntfile.js b/Gruntfile.js index a1018423ad..8d4955f5ee 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,8 +1,8 @@ -/*global module:false*/ +/* jshint node:true */ module.exports = function(grunt) { - var path = require('path'); - var SOURCE_DIR = 'src/'; - var BUILD_DIR = 'build/'; + var path = require('path'), + SOURCE_DIR = 'src/', + BUILD_DIR = 'build/'; // Load tasks. require('matchdep').filterDev('grunt-*').forEach( grunt.loadNpmTasks ); @@ -47,8 +47,8 @@ module.exports = function(grunt) { }, version: { options: { - processContent: function( src, filepath ) { - return src.replace( /^(\$wp_version.+?)-src';/m, "$1';" ); + processContent: function( src ) { + return src.replace( /^(\$wp_version.+?)-src';/m, '$1\';' ); } }, files: [ @@ -80,6 +80,60 @@ module.exports = function(grunt) { ] } }, + jshint: { + options: grunt.file.readJSON('.jshintrc'), + grunt: { + files: { + src: ['Gruntfile.js'] + }, + options: { + onevar: true + } + }, + tests: { + files: { + src: [ + 'tests/qunit/**/*.js', + '!tests/qunit/vendor/qunit.js' + ] + }, + options: grunt.file.readJSON('tests/qunit/.jshintrc') + }, + 'wp-admin': { + files: { + src: [ + 'src/wp-admin/js/**/*.js', + '!src/wp-admin/js/farbtastic.js', + '!src/wp-admin/js/iris.min.js' + ] + } + }, + 'wp-includes': { + files: { + src: [ + 'src/wp-includes/js/**/*.js', + // 3rd-Party Scripts + '!src/wp-includes/js/backbone.min.js', + '!src/wp-includes/js/colorpicker.js', + '!src/wp-includes/js/crop/**/*.js', + '!src/wp-includes/js/hoverIntent.js', + '!src/wp-includes/js/imgareaselect/**/*.js', + '!src/wp-includes/js/jcrop/**/*.js', + '!src/wp-includes/js/jquery/**/*.js', + '!src/wp-includes/js/json2.js', + '!src/wp-includes/js/mediaelement/**/*.js', + '!src/wp-includes/js/plupload/**/*.js', + '!src/wp-includes/js/swfobject.js', + '!src/wp-includes/js/swfupload/**/*.js', + '!src/wp-includes/js/thickbox/**/*.js', + '!src/wp-includes/js/tinymce/**/*.js', + '!src/wp-includes/js/tw-sack.js', + '!src/wp-includes/js/underscore.min.js', + '!src/wp-includes/js/zxcvbn.min.js' + ] + } + } + }, qunit: { files: ['tests/qunit/**/*.html'] }, @@ -194,12 +248,13 @@ module.exports = function(grunt) { // On `watch:all`, automatically updates the `copy:dynamic` and `clean:dynamic` // configurations so that only the changed files are updated. grunt.event.on('watch', function( action, filepath, target ) { - if ( target != 'all' ) + if ( target !== 'all' ) { return; + } - var relativePath = path.relative( SOURCE_DIR, filepath ); - var cleanSrc = ( action == 'deleted' ) ? [relativePath] : []; - var copySrc = ( action == 'deleted' ) ? [] : [relativePath]; + var relativePath = path.relative( SOURCE_DIR, filepath ), + cleanSrc = ( action === 'deleted' ) ? [relativePath] : [], + copySrc = ( action === 'deleted' ) ? [] : [relativePath]; grunt.config(['clean', 'dynamic', 'src'], cleanSrc); grunt.config(['copy', 'dynamic', 'src'], copySrc); diff --git a/package.json b/package.json index 9a72ae1e34..491565cb2f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "grunt-contrib-watch": "~0.5.1", "grunt-contrib-compress": "~0.5.2", "grunt-contrib-concat": "~0.3.0", + "grunt-contrib-jshint": "~0.7.0", "matchdep": "~0.1.2" } }