Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +1100 to +1102
}

// 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 ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me. Iterating over all lines in an an array is extremely fast, especially as the lines are already in memory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to aim for perfection.
Also I'd like to remind that in https://core.trac.wordpress.org/ticket/65950 I'm asking to consider whether jshint:plugins is still useful to some extend in the first place.
Honestly, I think it is not really used by developers to check their own plugins as today more modern and efficient tools are in use. I would consider to just remove this task.

// 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;
}
}
},
Expand Down
Loading