From e558d031eeb584bf6773f07c339c6e4192f96d7e Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Thu, 7 Nov 2024 22:56:55 -0700 Subject: [PATCH] remove sortImports, https://github.com/phetsims/chipper/issues/1461 --- js/grunt/sortImports.js | 76 ---------------------------------- js/grunt/tasks/sort-imports.ts | 23 ---------- 2 files changed, 99 deletions(-) delete mode 100644 js/grunt/sortImports.js delete mode 100644 js/grunt/tasks/sort-imports.ts diff --git a/js/grunt/sortImports.js b/js/grunt/sortImports.js deleted file mode 100644 index a20119294..000000000 --- a/js/grunt/sortImports.js +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2020-2024, University of Colorado Boulder - -/** - * Sorts imports for a given file. - * - * This follows the Intellij/Webstorm defaults, where we do NOT sort based on the eventual name, but instead only based - * on the import path (e.g. everything after the `from` in the import). - * - * This will attempt to group all of the imports in one block. - * - * @author Jonathan Olson - */ - - -// 3rd-party packages -const _ = require( 'lodash' ); -const fs = require( 'fs' ); - -// constants -const disallowedComments = [ - '// modules', - '// images', - '// strings', - '// mipmaps' -]; -const isImport = line => line.startsWith( 'import ' ); - -/** - * @param {string} file - * @param {boolean} verifyOnly - Don't rewrite file, just verify already sorted - * @returns {boolean} - Was the file properly sorted to begin with? - */ -module.exports = function( file, verifyOnly = false ) { - const before = fs.readFileSync( file, 'utf-8' ); - let lines = before.split( /\r?\n/ ); - - // remove the grouping comments - lines = lines.filter( ( line, i ) => { - const nextLine = lines[ i + 1 ]; - return !disallowedComments.includes( line ) || !nextLine || !isImport( nextLine ); - } ); - - // pull out and sort imports - let firstImportIndex = _.findIndex( lines, isImport ); - const importLines = lines.filter( isImport ); - const nonImportLines = lines.filter( _.negate( isImport ) ); - lines = [ - ...nonImportLines.slice( 0, firstImportIndex ), - ..._.sortBy( importLines, line => line.slice( line.indexOf( '\'' ) ).toLowerCase() ), // sort after the first ' - ...nonImportLines.slice( firstImportIndex ) - ]; - - // get rid of blank lines - const lastImportIndex = _.findLastIndex( lines, isImport ); - const afterLastImportIndex = lastImportIndex + 1; - while ( lines[ afterLastImportIndex ].length === 0 && lines[ lastImportIndex + 2 ].length === 0 ) { - lines.splice( afterLastImportIndex, 1 ); - } - - // add a blank line after imports if there was none - if ( lines[ afterLastImportIndex ].length !== 0 ) { - lines.splice( afterLastImportIndex, 0, '' ); - } - - // remove multiple blank lines above the imports - while ( lines[ firstImportIndex - 1 ] === '' && lines[ firstImportIndex - 2 ] === '' ) { - lines.splice( firstImportIndex - 1, 1 ); - firstImportIndex--; - } - - const after = lines.join( '\n' ); - if ( !verifyOnly ) { - fs.writeFileSync( file, after, 'utf-8' ); - } - return ( after === before ); -}; \ No newline at end of file diff --git a/js/grunt/tasks/sort-imports.ts b/js/grunt/tasks/sort-imports.ts deleted file mode 100644 index ba70ca5b9..000000000 --- a/js/grunt/tasks/sort-imports.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2013-2024, University of Colorado Boulder - -/** - * Sort the import statements for a single file (if --file={{FILE}} is provided), or does so for all JS files if not specified - * TODO: Delete this and the module, https://github.com/phetsims/chipper/issues/1461 - * @author Sam Reid (PhET Interactive Simulations) - */ - -import * as grunt from 'grunt'; -import getOption from '../../../../perennial-alias/js/grunt/tasks/util/getOption'; -import getRepo from '../../../../perennial-alias/js/grunt/tasks/util/getRepo'; - -const sortImports = require( '../sortImports.js' ); - -const repo = getRepo(); -const file = getOption( 'file' ); - -if ( file ) { - sortImports( file ); -} -else { - grunt.file.recurse( `../${repo}/js`, absfile => sortImports( absfile ) ); -} \ No newline at end of file