|
| 1 | +/** |
| 2 | + * Script to sort PHP namespace imports |
| 3 | + * |
| 4 | + * This script is meant to be run before Prettier to sort PHP imports |
| 5 | + * It preserves blank lines between different types of imports (class vs function) |
| 6 | + */ |
| 7 | + |
| 8 | +const fs = require( 'fs' ); |
| 9 | +const path = require( 'path' ); |
| 10 | + |
| 11 | +// Sort imports in a PHP file |
| 12 | +const sortImports = ( filePath ) => { |
| 13 | + try { |
| 14 | + const content = fs.readFileSync( filePath, 'utf8' ); |
| 15 | + |
| 16 | + // Find blocks of use statements |
| 17 | + const useStatementRegex = /^use\s+([^;]+);/gm; |
| 18 | + let match; |
| 19 | + let blocks = []; |
| 20 | + |
| 21 | + // Find all blocks of consecutive use statements |
| 22 | + while ( ( match = useStatementRegex.exec( content ) ) !== null ) { |
| 23 | + const currentIndex = match.index; |
| 24 | + const fullMatch = match[ 0 ]; |
| 25 | + |
| 26 | + // Check if this is part of an existing block or a new block |
| 27 | + if ( blocks.length > 0 ) { |
| 28 | + const lastBlock = blocks[ blocks.length - 1 ]; |
| 29 | + |
| 30 | + // If this statement is close to the previous one, add it to the same block |
| 31 | + // We check for newlines between statements to determine if they're in the same block |
| 32 | + const textBetween = content.substring( lastBlock.end, currentIndex ); |
| 33 | + const newlineCount = ( textBetween.match( /\n/g ) || [] ).length; |
| 34 | + |
| 35 | + if ( newlineCount <= 2 ) { |
| 36 | + lastBlock.statements.push( fullMatch ); |
| 37 | + lastBlock.end = currentIndex + fullMatch.length; |
| 38 | + continue; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Start a new block |
| 43 | + blocks.push( { |
| 44 | + start: currentIndex, |
| 45 | + end: currentIndex + fullMatch.length, |
| 46 | + statements: [ fullMatch ], |
| 47 | + } ); |
| 48 | + } |
| 49 | + |
| 50 | + // Sort each block of use statements, preserving separation between class and function imports |
| 51 | + let result = content; |
| 52 | + let offset = 0; |
| 53 | + let changed = false; |
| 54 | + |
| 55 | + blocks.forEach( ( block ) => { |
| 56 | + // Separate class and function imports |
| 57 | + const classImports = block.statements.filter( ( stmt ) => ! stmt.includes( 'use function' ) ); |
| 58 | + const functionImports = block.statements.filter( ( stmt ) => stmt.includes( 'use function' ) ); |
| 59 | + |
| 60 | + // Sort each group separately |
| 61 | + const sortedClassImports = [ ...classImports ].sort(); |
| 62 | + const sortedFunctionImports = [ ...functionImports ].sort(); |
| 63 | + |
| 64 | + // Combine with a blank line between if both types exist |
| 65 | + let sortedBlock; |
| 66 | + if ( sortedClassImports.length > 0 && sortedFunctionImports.length > 0 ) { |
| 67 | + sortedBlock = sortedClassImports.join( '\n' ) + '\n\n' + sortedFunctionImports.join( '\n' ); |
| 68 | + } else { |
| 69 | + sortedBlock = [ ...sortedClassImports, ...sortedFunctionImports ].join( '\n' ); |
| 70 | + } |
| 71 | + |
| 72 | + // Replace the original block with sorted statements |
| 73 | + const originalBlock = result.substring( block.start + offset, block.end + offset ); |
| 74 | + |
| 75 | + if ( originalBlock !== sortedBlock ) { |
| 76 | + changed = true; |
| 77 | + result = |
| 78 | + result.substring( 0, block.start + offset ) + sortedBlock + result.substring( block.end + offset ); |
| 79 | + |
| 80 | + // Update offset for subsequent replacements |
| 81 | + offset += sortedBlock.length - originalBlock.length; |
| 82 | + } |
| 83 | + } ); |
| 84 | + |
| 85 | + // Only write the file if changes were made |
| 86 | + if ( changed ) { |
| 87 | + fs.writeFileSync( filePath, result, 'utf8' ); |
| 88 | + return { changed: true }; |
| 89 | + } |
| 90 | + |
| 91 | + return { changed: false }; |
| 92 | + } catch ( error ) { |
| 93 | + console.error( `Error processing ${ filePath }:`, error ); |
| 94 | + return { changed: false }; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +// If this script is run directly |
| 99 | +if ( require.main === module ) { |
| 100 | + const args = process.argv.slice( 2 ); |
| 101 | + if ( args.length === 0 ) { |
| 102 | + console.error( 'Please provide a file path' ); |
| 103 | + process.exit( 1 ); |
| 104 | + } |
| 105 | + |
| 106 | + const filePath = args[ 0 ]; |
| 107 | + sortImports( filePath ); |
| 108 | +} |
| 109 | + |
| 110 | +// Export for use in other scripts |
| 111 | +module.exports = { sortImports }; |
0 commit comments