Browse Source

Custom implementation of jscs, which checks only next commit files.

fredck 11 years ago
parent
commit
3623e9d420
6 changed files with 123 additions and 26 deletions
  1. 2 0
      dev/tasks/githooks.js
  2. 72 7
      dev/tasks/jscs.js
  3. 4 1
      dev/tasks/jshint.js
  4. 25 0
      dev/tasks/res/tools.js
  5. 2 1
      gruntfile.js
  6. 18 17
      package.json

+ 2 - 0
dev/tasks/githooks.js

@@ -1,3 +1,5 @@
+/* global module */
+
 'use strict';
 'use strict';
 
 
 module.exports = function( grunt ) {
 module.exports = function( grunt ) {

+ 72 - 7
dev/tasks/jscs.js

@@ -1,14 +1,14 @@
+/* global module */
+/* global require */
+/* global console */
+
 'use strict';
 'use strict';
 
 
 module.exports = function( grunt ) {
 module.exports = function( grunt ) {
-	grunt.config.merge( {
-		jscs: {
-			src: [ '**/*.js' ],
-			options: defaultConfig
-		}
+	// Register our custom jscs task.
+	grunt.registerTask( 'jscs', 'JavaScript Code Style checker', function() {
+		task.call( this, grunt );
 	} );
 	} );
-
-	grunt.loadNpmTasks( 'grunt-jscs' );
 };
 };
 
 
 var defaultConfig = {
 var defaultConfig = {
@@ -76,3 +76,68 @@ var defaultConfig = {
 	'requireDotNotation': true,
 	'requireDotNotation': true,
 	'disallowYodaConditions': true
 	'disallowYodaConditions': true
 };
 };
+
+var Vow = require( 'vow' ),
+	Jscs = require( 'jscs' ),
+	tools = require( './res/tools' );
+
+function task( grunt ) {
+	// Checking is asynchronous.
+	var done = this.async();
+
+	// Get the list of files that will end up in the next commit.
+	var files = tools.getGitDirtyFiles();
+
+	// Reduce the files list to *.js.
+	files = files.filter( function( file ) {
+		// Accepts .js files only
+		return file && ( /\.js$/ ).test( file );
+	} );
+
+	// Create and configure the Checker.
+	var checker = new Jscs();
+	checker.registerDefaultRules();
+	checker.configure( defaultConfig );
+
+	// Get the check promises for each file.
+	var checks = files.map( function( file ) {
+		// Returns a check promise for each file.
+		return checker.checkPath( file );
+	} );
+
+	// Once the promises are done...
+	Vow.allResolved( checks ).spread( function() {
+		var results, errorCount = 0;
+
+		// grunt.async() hide errors, so better to catch them.
+		try {
+			results = Array.prototype.filter.call( arguments, function( promise ) {
+				return promise && promise.isFulfilled();
+			} ).map( function( promise ) {
+				// Take the jscs error object out of each promise.
+				return promise.valueOf()[ 0 ];
+			} );
+
+			// Loop throw all files with errors.
+			results.forEach( function( fileErrors ) {
+				// Loop through all errors in the file.
+				fileErrors.getErrorList().forEach( function( error ) {
+					errorCount++;
+					console.log( fileErrors.explainError( error, true ) );
+					console.log( '' );
+				} );
+			} );
+		} catch ( e ) {
+			console.log( e );
+			done( false );
+		}
+
+		if ( errorCount ) {
+			grunt.log.error( errorCount + ' code style errors found!' );
+		} else {
+			grunt.log.ok( results.length + ' files without code style errors.' );
+		}
+
+		done( !errorCount );
+	} );
+}

+ 4 - 1
dev/tasks/jshint.js

@@ -1,3 +1,5 @@
+/* global module */
+
 'use strict';
 'use strict';
 
 
 module.exports = function( grunt ) {
 module.exports = function( grunt ) {
@@ -12,5 +14,6 @@ module.exports = function( grunt ) {
 };
 };
 
 
 var defaultConfig = {
 var defaultConfig = {
-	'globalstrict': true
+	'globalstrict': true,
+	'validthis': true
 };
 };

+ 25 - 0
dev/tasks/res/tools.js

@@ -0,0 +1,25 @@
+/* global module */
+/* global require */
+
+'use strict';
+
+module.exports = {
+	getGitDirtyFiles: function() {
+		return this.shExec( 'git diff-index --name-only HEAD' ).split( '\n' );
+	},
+
+	shExec: function( command ) {
+		var sh = require( 'shelljs' );
+		sh.config.silent = true;
+
+		var ret = sh.exec( command );
+
+		if ( ret.code ) {
+			throw new Error(
+				'Error while executing `' + command + '`:\n\n' +
+				ret.output
+			);
+		}
+		return ret.output;
+	}
+};

+ 2 - 1
gruntfile.js

@@ -1,3 +1,5 @@
+/* global module */
+
 'use strict';
 'use strict';
 
 
 module.exports = function( grunt ) {
 module.exports = function( grunt ) {
@@ -8,7 +10,6 @@ module.exports = function( grunt ) {
 			options: {
 			options: {
 				'ignores': lintIgnores,
 				'ignores': lintIgnores,
 				'predef': [
 				'predef': [
-					'module'
 				]
 				]
 			}
 			}
 		},
 		},

+ 18 - 17
package.json

@@ -1,19 +1,20 @@
 {
 {
-	"name": "",
-	"version": "",
-	"description": "",
-	"keywords": [],
-	"dependencies": {
-	},
-	"devDependencies": {
-		"grunt": "~0",
-		"grunt-jscs": "~0",
-		"grunt-contrib-jshint": "~0",
-		"grunt-githooks": "~0",
-		"load-grunt-tasks": "~1"
-	},
-	"author": "CKSource (http://cksource.com/)",
-	"license": "See LICENSE.md",
-	"homepage": "",
-	"bugs": ""
+  "name": "",
+  "version": "",
+  "description": "",
+  "keywords": [],
+  "dependencies": {},
+  "devDependencies": {
+    "grunt": "~0",
+    "grunt-contrib-jshint": "~0",
+    "grunt-githooks": "~0",
+    "load-grunt-tasks": "~1",
+    "shelljs": "~0.3",
+    "jscs": "~1",
+    "vow": "~0.4"
+  },
+  "author": "CKSource (http://cksource.com/)",
+  "license": "See LICENSE.md",
+  "homepage": "",
+  "bugs": ""
 }
 }