forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Framework: add codemod to clean up duplicate state/utils imports
- Loading branch information
Showing
79 changed files
with
219 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env node | ||
|
||
/* | ||
This codemod combines state/utils imports | ||
How to use: | ||
./bin/codemods/combine-state-utils-imports path-to-transform/ | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
const path = require( 'path' ); | ||
const child_process = require( 'child_process' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const config = require( './config' ); | ||
const helpers = require( './helpers' ); | ||
|
||
const args = process.argv.slice( 2 ); | ||
if ( args.length === 0 ) { | ||
process.stdout.write( 'No files to transform\n' ); | ||
process.exit( 0 ); | ||
} | ||
|
||
const binArgs = [ | ||
// jscodeshift options | ||
'--transform=bin/codemods/src/combine-state-utils-imports.js', | ||
...config.jscodeshiftArgs, | ||
|
||
// Transform target | ||
args[ 0 ], | ||
]; | ||
const binPath = path.join( '.', 'node_modules', '.bin', 'jscodeshift' ); | ||
const jscodeshift = child_process.spawn( binPath, binArgs ); | ||
helpers.bindEvents( jscodeshift ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
This codemod updates | ||
import { createReducer } from 'state/utils'; | ||
import { combineReducersWithPersistence as bar, baz } from 'state/utils' | ||
to | ||
import { baz, combineReducersWithPersistence as bar, createReducer } from 'state/utils'; | ||
*/ | ||
|
||
module.exports = function ( file, api ) { | ||
// alias the jscodeshift API | ||
const j = api.jscodeshift; | ||
// parse JS code into an AST | ||
const root = j( file.source ); | ||
|
||
const stateUtilsImports = root.find( j.ImportDeclaration, { | ||
source: { | ||
type: 'Literal', | ||
value: 'state/utils', | ||
}, | ||
} ); | ||
|
||
if ( stateUtilsImports.length < 2 ) { | ||
return; | ||
} | ||
|
||
//grab each identifier | ||
const importNames = []; | ||
stateUtilsImports.find( j.ImportSpecifier ).forEach( item => { | ||
importNames.push( { | ||
local: item.value.local.name, | ||
imported: item.value.imported.name | ||
} ); | ||
} ); | ||
|
||
//sort by imported name | ||
importNames.sort(( a, b ) => { | ||
if( a.imported < b.imported ) { | ||
return -1; | ||
} | ||
if( a.imported > b.imported ) { | ||
return 1; | ||
} | ||
return 0; | ||
} ); | ||
|
||
//Save Comment if possible | ||
const comments = stateUtilsImports.at( 0 ).get().node.comments; | ||
|
||
const addImport = ( importNames ) => { | ||
const names = importNames.map( name => { | ||
if ( name.local === name.imported ) { | ||
return j.importSpecifier( j.identifier( name.local ) ); | ||
} | ||
if ( name.local !== name.imported ) { | ||
return j.importSpecifier( j.identifier( name.imported ), j.identifier( name.local ) ); | ||
} | ||
} ); | ||
const combinedImport = j.importDeclaration( | ||
names, | ||
j.literal( 'state/utils' ) | ||
); | ||
combinedImport.comments = comments; | ||
return combinedImport; | ||
}; | ||
|
||
//replace the first one with the combined import | ||
stateUtilsImports.at( 0 ).replaceWith( addImport( importNames ) ); | ||
//remove the rest | ||
for ( let i = 1; i < stateUtilsImports.length; i ++ ) { | ||
stateUtilsImports.at( i ).remove(); | ||
} | ||
|
||
return root.toSource( { quote: 'single' } ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.