UNPKG

chicago

Version:

A front-end JavaScript library for user-interface developers.

203 lines (171 loc) 5.99 kB
module.exports = function( grunt ) { // Custom Tasks grunt.registerTask('chicago-style', 'Custom style enforcement task created for the Chicago JavaScript library', function() { // Set global variables var distFile = 'dist/Chicago.js'; // Functions for repeated use function getContent() { return grunt.file.read( distFile ); } function getContentArray() { return getContent().split('\n'); } // Fixes parenthesis misplacement after `jsbeautifier` is run. (function() { if( ! grunt.file.exists( distFile ) ) { grunt.log.error( 'File not found: ' + distFile ); return; } // var content = grunt.file.read( distFile ) // .replace(/\n\(function/g, ';(function') // .replace(/\t\(function/g, '\t;(function'), var contentArray = getContentArray(), output = []; for( var i = 0; i < contentArray.length; i++ ) { var line = contentArray[i]; if( line.trim() === ';' ) { continue; } output.push( line ); } var content = output .join('\n') .replace( /;;/g, ';' ) .replace( /: ;\(function/g, ': (function' ) .replace( /\( {/g, '\({' ); grunt.file.write( distFile, content ); })(); // Remove inline comments (function() { var contentArray = getContentArray(), output = []; for( var i = 0; i < contentArray.length; i++ ) { var line = contentArray[i]; if( line.indexOf('//') < 0 || line.trim()[0] === '*' ) { output.push( line ); } } grunt.file.write( distFile, output.join('\n') ); })(); // Force proper spacing around parenthesis // (function() { // var contentArray = getContentArray(), // output = [], // ifNotWithoutSpace = /if\(!(?! )/g, // parenWithoutSpace = /\([^\s]/g, // ifWithoutParenSpace = /\){/g, // ifWithoutClosingSpace = /[^\s]\)/g; // for( var i = 0; i < contentArray.length; i++ ) { // var line = contentArray[i], // match; // // Line contains an `if not` statement without proper spacing // if( ifNotWithoutSpace.exec( line ) ) { // line = line.replace( ifNotWithoutSpace, 'if( ! ' ); // } // // Line contains an open parenthesis character without proper spacing // match = line.match( parenWithoutSpace ); // if( match ) { // for( var _i = 0; _i < match.length; _i++ ) { // var string = match[ _i ]; // if( string === '()' || string === '(/' ) { // continue; // } // var newstring = string.replace( /\(/g, '( ' ); // line = line.replace( string, newstring ); // } // // Catch errors // line = line.replace( /\( function/g, '(function' ); // match = null; // } // // Line contains an opening curly bracket character that is not preceded by a space // if( ifWithoutParenSpace.exec( line ) ) { // line = line.replace( ifWithoutParenSpace, ') {' ); // } // // Line doesn not contain a space between the statement and closing parenthesis // match = line.match( ifWithoutClosingSpace ); // if( match ) { // for( var _i = 0; _i < match.length; _i++ ) { // var string = match[ _i ]; // console.log(i, string); // if( string === '})' || string === '()' || string === '/)' ) { // continue; // } // var newstring = string.replace( /\)/g, ' )' ); // line = line.replace( string, newstring ); // } // match = null; // } // // Write line to output // output.push( line ); // } // // Fixes space around copyright // var content = output.join( '\n' ) // .replace( /\( c \)/gi, '(c)' ) // .replace( /\/\( \?/g, '/(?' ) // .replace( /\w \)\//g, 'w)/' ); // // grunt.file.write( distFile, content ); // })(); // Remove any rogue `console.log` statements left from dev // Shamelessly stole regex from: https://github.com/ehynds/grunt-remove-logging/blob/master/tasks/lib/removelogging.js (function() { var namespace = [ 'console', 'window.console' ], methods = 'log info warn error assert count clear group groupEnd groupCollapsed trace debug dir dirxml profile profileEnd time timeEnd timeStamp table exception'.split(' '), regex = new RegExp("(" + namespace.join("|") + ")" + ".(?:" + methods.join("|") + ")\\s{0,}\\([^;]*\\)(?!\\s*[;,]?\\s*\\/\\*\\s*RemoveLogging:skip\\s*\\*\\/)\\s{0,};?", "gi");; var content = getContent().replace( regex, '' ); grunt.file.write( distFile, content ); })(); // Remove unnecessary whitespace (function() { var contentArray = getContentArray(), output = []; for( var i = 0; i < contentArray.length; i++ ) { var line = contentArray[i]; if( line.trim() ) { output.push( line ); } } // Reset variables contentArray = output; output = []; for( var i = 0; i < contentArray.length; i++ ) { var line = contentArray[i]; if( line.indexOf( '*/' ) > -1 ) { line += '\n'; } if( line.indexOf( '/*!' ) > -1 && i > 0 ) { line = '\n' + line; } if( line.trim().indexOf( '//' ) === 0 ) { var prevline = contentArray[ ( i - 1 ) ]; if( prevline.trim().indexOf( '//' ) < 0 ) { line = '\n' + line; } } if( line.indexOf( '})(function' ) > -1 ) { line = '\n' + line + '\n'; } output.push( line ); } grunt.file.write( distFile, output.join( '\n' ) ); })(); // Enforces proper comparison operators (function() { var content = getContent() .replace( / != /g, ' !== ' ) .replace( / == /g, ' === ' ); grunt.file.write( distFile, content ); })(); // Update version in files (function() { var pkg = grunt.config( 'pkg' ); grunt.file.recurse('dist', function(abspath, rootdir, subdir, filename) { var content = grunt.file.read( abspath ); content = content.replace( /@VERSION/g, pkg.version ); grunt.file.write( abspath, content ); }); })(); // Add extra line break var content = getContent(); grunt.file.write( distFile, content + '\n' ); }); };