Make WordPress Core

Changeset 63783


Ignore:
Timestamp:
09/21/2026 07:55:13 PM (4 weeks ago)
Author:
afercia
Message:

Build/Test Tools: Exclude minified files from Grunt jshint:plugins more effectively.

Follow-up to [63364]. Prevents grunt jshint:plugins from scanning minified JavaScript files that don't use a .min suffix in their filename. Assumes that JavaScript files that contain lines with a length that is longer than 500 characters are minified file and skips them.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13573

Props afercia, westonruter.
Fixes #66123.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Gruntfile.js

    r63556 r63783  
    10841084                                        '**/vendor/**'
    10851085                                ],
    1086                                 /*
    1087                                  * Limit JSHint's run to a single specified plugin directory:
     1086                                /**
     1087                                 * Limits JSHint's run to a single specified plugin directory:
    10881088                                 *
    1089                                  *    grunt jshint:plugins --dir=foldername
     1089                                 * Usage example:
     1090                                 * grunt jshint:plugins --dir=foldername
     1091                                 *
     1092                                 * This also automatically skips minified files that may not have
     1093                                 * a filename ending with `.min.js`, specifically when they have
     1094                                 * lines longer than 500 characters.
     1095                                 *
     1096                                 * @param {string} dirpath Directory path.
     1097                                 * @return {boolean} Whether the path is skipped.
    10901098                                 */
    10911099                                filter: function( dirpath ) {
    1092                                         var index, dir = grunt.option( 'dir' );
    1093 
    1094                                         // Don't filter when no target folder is specified.
    1095                                         if ( ! dir ) {
    1096                                                 return true;
    1097                                         }
    1098 
    1099                                         dirpath = dirpath.replace( /\\/g, '/' );
    1100                                         index = dirpath.lastIndexOf( '/' + dir );
    1101 
    1102                                         // Match only the folder name passed from cli.
    1103                                         if ( -1 !== index ) {
    1104                                                 return true;
    1105                                         }
    1106 
    1107                                         return false;
     1100                                        // Bypasses folder targets so fs.readFileSync doesn't throw errors.
     1101                                        if ( ! fs.lstatSync( dirpath ).isFile() ) {
     1102                                                return false;
     1103                                        }
     1104
     1105                                        // If `--dir` is provided.
     1106                                        var dir = grunt.option( 'dir' );
     1107                                        if ( dir ) {
     1108                                                var normalizedPath = dirpath.replace( /\\/g, '/' );
     1109                                                var index = normalizedPath.lastIndexOf( '/' + dir );
     1110
     1111                                                // If a directory was requested but this file isn't in it, exclude it.
     1112                                                if ( -1 === index ) {
     1113                                                        return false;
     1114                                                }
     1115                                        }
     1116
     1117                                        // Read the file and inspect line lengths to catch minified/bundled code.
     1118                                        var content;
     1119                                        try {
     1120                                                content = fs.readFileSync( dirpath, 'utf-8' );
     1121                                        } catch ( error ) {
     1122                                                grunt.log.writeln( 'Could not read file: ' + dirpath + ' (Error: ' + error.message + ')' );
     1123                                                return false;
     1124                                        }
     1125
     1126                                        var lines = content.split( '\n' );
     1127
     1128                                        // Cap the maximum number of iterations at 5 lines.
     1129                                        for ( const line of lines.slice( 0, 5 ) ) {
     1130                                                // Exclude files with lines longer than 500 characters.
     1131                                                if ( line.length > 500 ) {
     1132                                                        grunt.log.writeln( 'Skipping minified file: ' + dirpath );
     1133                                                        return false;
     1134                                                }
     1135                                        }
     1136
     1137                                        // File passed both independent checks: proceed with JSHint.
     1138                                return true;
    11081139                                }
    11091140                        }
Note: See TracChangeset for help on using the changeset viewer.