Skip to content

Commit

Permalink
Framework: add codemod to clean up duplicate state/utils imports
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed May 23, 2017
1 parent 253419f commit 26b17ca
Show file tree
Hide file tree
Showing 79 changed files with 219 additions and 157 deletions.
38 changes: 38 additions & 0 deletions bin/codemods/combine-state-utils-imports
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 );
78 changes: 78 additions & 0 deletions bin/codemods/src/combine-state-utils-imports.js
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();
}

// print
return root.toSource( { quote: 'single' } );
};
4 changes: 2 additions & 2 deletions client/extensions/wp-super-cache/state/cache/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
WP_SUPER_CACHE_DELETE_CACHE,
WP_SUPER_CACHE_DELETE_CACHE_FAILURE,
Expand Down
4 changes: 2 additions & 2 deletions client/extensions/wp-super-cache/state/notices/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import { itemsSchema } from './schema';
import {
WP_SUPER_CACHE_RECEIVE_NOTICES,
Expand Down
4 changes: 2 additions & 2 deletions client/extensions/wp-super-cache/state/settings/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import { itemsSchema } from './schema';
import {
WP_SUPER_CACHE_RECEIVE_SETTINGS,
Expand Down
4 changes: 2 additions & 2 deletions client/extensions/wp-super-cache/state/stats/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { get } from 'lodash';
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import { statsSchema } from './schema';
import {
WP_SUPER_CACHE_DELETE_FILE,
Expand Down
3 changes: 1 addition & 2 deletions client/state/account-recovery/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
* Internal dependencies
*/
import reset from './reset/reducer';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';
import settings from './settings/reducer';
import { createReducer } from 'state/utils';

import {
ACCOUNT_RECOVERY_SETTINGS_FETCH,
Expand Down
4 changes: 2 additions & 2 deletions client/state/account-recovery/reset/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { stubTrue, stubFalse } from 'lodash';
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
ACCOUNT_RECOVERY_RESET_OPTIONS_ERROR,
ACCOUNT_RECOVERY_RESET_OPTIONS_RECEIVE,
Expand Down
4 changes: 2 additions & 2 deletions client/state/account-recovery/settings/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
ACCOUNT_RECOVERY_SETTINGS_FETCH_SUCCESS,

Expand Down
3 changes: 1 addition & 2 deletions client/state/application/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
CONNECTION_LOST,
CONNECTION_RESTORED
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

export const connectionState = createReducer( 'CHECKING', {
[ CONNECTION_LOST ]: () => 'OFFLINE',
Expand Down
6 changes: 1 addition & 5 deletions client/state/automated-transfer/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { get } from 'lodash';
* Internal dependencies
*/
import eligibility from './eligibility/reducer';
import { combineReducersWithPersistence } from 'state/utils';
import {
keyedReducer,
withSchemaValidation,
} from 'state/utils';
import { combineReducersWithPersistence, keyedReducer, withSchemaValidation } from 'state/utils';
import { transferStates } from './constants';
import { automatedTransfer as schema } from './schema';
import {
Expand Down
3 changes: 1 addition & 2 deletions client/state/billing-transactions/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
BILLING_TRANSACTIONS_REQUEST_FAILURE,
BILLING_TRANSACTIONS_REQUEST_SUCCESS
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';
import { billingTransactionsSchema } from './schema';
import { createReducer } from 'state/utils';

/**
* Returns the updated items state after an action has been dispatched.
Expand Down
3 changes: 1 addition & 2 deletions client/state/country-states/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
COUNTRY_STATES_REQUEST_FAILURE,
COUNTRY_STATES_REQUEST_SUCCESS,
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';
import { itemSchema } from './schema';
import { createReducer } from 'state/utils';

// Stores the complete list of states, indexed by locale key
export const items = createReducer( {}, {
Expand Down
4 changes: 2 additions & 2 deletions client/state/current-user/email-verification/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
EMAIL_VERIFY_REQUEST,
EMAIL_VERIFY_REQUEST_SUCCESS,
Expand Down
3 changes: 1 addition & 2 deletions client/state/current-user/gravatar-status/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
GRAVATAR_UPLOAD_REQUEST_SUCCESS,
GRAVATAR_UPLOAD_REQUEST_FAILURE
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

export const isUploading = createReducer( false, {
[ GRAVATAR_UPLOAD_REQUEST ]: () => true,
Expand Down
3 changes: 1 addition & 2 deletions client/state/current-user/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
SITES_UPDATE,
PLANS_RECEIVE
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';
import { idSchema, capabilitiesSchema, currencyCodeSchema, flagsSchema } from './schema';
import gravatarStatus from './gravatar-status/reducer';
import emailVerification from './email-verification/reducer';
Expand Down
4 changes: 2 additions & 2 deletions client/state/document-head/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
DOCUMENT_HEAD_LINK_SET,
DOCUMENT_HEAD_META_SET,
Expand Down
3 changes: 1 addition & 2 deletions client/state/geo/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
GEO_REQUEST_FAILURE,
GEO_REQUEST_SUCCESS
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';
import { geoSchema } from './schema';

/**
Expand Down
4 changes: 2 additions & 2 deletions client/state/happiness-engineers/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { map } from 'lodash';
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import { itemsSchema } from './schema';
import {
HAPPINESS_ENGINEERS_FETCH,
Expand Down
3 changes: 1 addition & 2 deletions client/state/happychat/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ import {
HAPPYCHAT_TRANSCRIPT_RECEIVE,
HAPPYCHAT_SET_GEO_LOCATION,
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer, isValidStateWithSchema } from 'state/utils';
import {
HAPPYCHAT_CHAT_STATUS_DEFAULT,
} from './selectors';
import { HAPPYCHAT_MAX_STORED_MESSAGES } from './constants';
import { createReducer, isValidStateWithSchema } from 'state/utils';
import { timelineSchema, geoLocationSchema } from './schema';

/**
Expand Down
4 changes: 2 additions & 2 deletions client/state/help/courses/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
HELP_COURSES_RECEIVE,
} from 'state/action-types';
Expand Down
4 changes: 2 additions & 2 deletions client/state/help/directly/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
DIRECTLY_INITIALIZATION_START,
DIRECTLY_INITIALIZATION_SUCCESS,
Expand Down
3 changes: 1 addition & 2 deletions client/state/help/ticket/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* Internal dependencies
*/
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

import {
HELP_TICKET_CONFIGURATION_REQUEST,
Expand Down
3 changes: 1 addition & 2 deletions client/state/jetpack-connect/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import {
SERIALIZE,
DESERIALIZE
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { isValidStateWithSchema } from 'state/utils';
import { combineReducersWithPersistence, isValidStateWithSchema } from 'state/utils';
import {
jetpackConnectSessionsSchema,
jetpackAuthAttemptsSchema,
Expand Down
3 changes: 1 addition & 2 deletions client/state/jetpack/connection/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
JETPACK_DISCONNECT_REQUEST_FAILURE,
JETPACK_DISCONNECT_REQUEST_SUCCESS,
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

const createRequestReducer = ( requesting ) => {
return ( state, { siteId } ) => ( {
Expand Down
3 changes: 1 addition & 2 deletions client/state/jetpack/jumpstart/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import {
JETPACK_JUMPSTART_STATUS_REQUEST_SUCCESS,
JETPACK_JUMPSTART_STATUS_REQUEST_FAILURE
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

const createRequestReducer = ( data ) => {
return ( state, { siteId } ) => {
Expand Down
3 changes: 1 addition & 2 deletions client/state/jetpack/modules/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import {
JETPACK_SETTINGS_RECEIVE,
JETPACK_SETTINGS_UPDATE_SUCCESS,
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';

const createItemsReducer = ( active ) => {
return ( state, { siteId, moduleSlug } ) => {
Expand Down
3 changes: 1 addition & 2 deletions client/state/jetpack/settings/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
JETPACK_SETTINGS_UPDATE_SUCCESS,
JETPACK_SETTINGS_UPDATE_FAILURE
} from 'state/action-types';
import { combineReducersWithPersistence } from 'state/utils';
import { createReducer } from 'state/utils';
import { combineReducersWithPersistence, createReducer } from 'state/utils';
import { normalizeSettings } from './utils';

const createRequestsReducer = ( data ) => {
Expand Down
Loading

0 comments on commit 26b17ca

Please sign in to comment.