diff --git a/Gruntfile.js b/Gruntfile.js index 08da38fb748e2..f39a8afbd15b8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1083,28 +1083,59 @@ module.exports = function(grunt) { '**/test/**', '**/vendor/**' ], - /* - * Limit JSHint's run to a single specified plugin directory: + /** + * Limits JSHint's run to a single specified plugin directory: + * + * Usage example: + * grunt jshint:plugins --dir=foldername + * + * This also automatically skips minified files that may not have + * a filename ending with `.min.js`, specifically when they have + * lines longer than 500 characters. * - * grunt jshint:plugins --dir=foldername + * @param {string} dirpath Directory path. + * @return {boolean} Whether the path is skipped. */ filter: function( dirpath ) { - var index, dir = grunt.option( 'dir' ); + // Bypasses folder targets so fs.readFileSync doesn't throw errors. + if ( ! fs.lstatSync( dirpath ).isFile() ) { + return false; + } - // Don't filter when no target folder is specified. - if ( ! dir ) { - return true; + // If `--dir` is provided. + var dir = grunt.option( 'dir' ); + if ( dir ) { + var normalizedPath = dirpath.replace( /\\/g, '/' ); + var index = normalizedPath.lastIndexOf( '/' + dir ); + + // If a directory was requested but this file isn't in it, exclude it. + if ( -1 === index ) { + return false; + } } - dirpath = dirpath.replace( /\\/g, '/' ); - index = dirpath.lastIndexOf( '/' + dir ); + // Read the file and inspect line lengths to catch minified/bundled code. + var content; + try { + content = fs.readFileSync( dirpath, 'utf-8' ); + } catch ( error ) { + grunt.log.writeln( 'Could not read file: ' + dirpath + ' (Error: ' + error.message + ')' ); + return false; + } - // Match only the folder name passed from cli. - if ( -1 !== index ) { - return true; + var lines = content.split( '\n' ); + + // Cap the maximum number of iterations at 5 lines. + for ( const line of lines.slice( 0, 5 ) ) { + // Exclude files with lines longer than 500 characters. + if ( line.length > 500 ) { + grunt.log.writeln( 'Skipping minified file: ' + dirpath ); + return false; + } } - return false; + // File passed both independent checks: proceed with JSHint. + return true; } } },