Skip to content

Commit

Permalink
Merge pull request #8 from Automattic/add/remove-create-reducer
Browse files Browse the repository at this point in the history
Add codemod to remove create-reducer
  • Loading branch information
sgomes authored and sirreal committed Nov 14, 2019
1 parent 8ff755c commit 69b805e
Show file tree
Hide file tree
Showing 5 changed files with 456 additions and 11 deletions.
21 changes: 10 additions & 11 deletions packages/calypso-codemods/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
const jscodeshiftArgs = [
'--extensions=js,jsx',
];
const jscodeshiftArgs = [ '--extensions=js,jsx' ];

// Used primarily by 5to6-codemod transformations
const recastArgs = [
'--useTabs=true',
'--arrayBracketSpacing=true',
];
const recastArgs = [ '--useTabs=true', '--arrayBracketSpacing=true' ];

const recastOptions = {
arrayBracketSpacing: true,
Expand All @@ -17,18 +12,19 @@ const recastOptions = {
objects: true,
arrays: true,
parameters: false,
}
},
};

const commonArgs = {
'5to6': [
// Recast options via 5to6
...recastArgs,
],
'react': [
react: [
// Recast options via react-codemod
`--printOptions=${ JSON.stringify( recastOptions ) }`,
],
}
};

const codemodArgs = {
'commonjs-exports': [
Expand All @@ -40,11 +36,13 @@ const codemodArgs = {
...commonArgs[ '5to6' ],
'--transform=node_modules/5to6-codemod/transforms/cjs.js',
],

'commonjs-imports-hoist': [
...commonArgs[ '5to6' ],
'--transform=node_modules/5to6-codemod/transforms/cjs.js',
'--hoist=true',
],

'named-exports-from-default': [
...commonArgs[ '5to6' ],
'--transform=node_modules/5to6-codemod/transforms/named-export-generation.js',
Expand All @@ -58,11 +56,12 @@ const codemodArgs = {
'--pure-component=true',
'--mixin-module-name="react-pure-render/mixin"', // Your days are numbered, pure-render-mixin!
],

'react-proptypes': [
...commonArgs[ 'react' ],
'--transform=node_modules/react-codemod/transforms/React-PropTypes-to-prop-types.js',
],
}
};

module.exports = {
codemodArgs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`create-reducer.js 1`] = `
"// This comment should be preserved even if the line below is removed.
import { withSchemaValidation, withoutPersistence } from 'state/utils';
const COMPUTED_IDENTIFIER = 'COMPUTED_IDENTIFIER';
const isFetchingSettings = withoutPersistence((state = false, action) => {
switch (action.type) {
case COMPUTED_IDENTIFIER:
return 'computed_id';
case 'COMPUTED_STRING':
return state;
case \\"NON_COMPUTED_STRING\\":
return action.thing;
case \\"2\\":
return 2;
case \\"FUNCTION_HANDLER\\":
return function( s, a ) {
return s;
}(state, action);
case \\"ARROW_FUNCTION_HANDLER\\":
return state;
case \\"ARROW_FUNCTION_WITH_DESTRUCT\\":
{
const { thing } = action;
return thing;
}
case \\"VARIABLE_HANDLER\\":
return f(state, action);
}
return state;
});
function f() {
return 'a function reducer';
}
const persistentReducer = (state = false, action) => {
switch (action.type) {
case COMPUTED_IDENTIFIER:
return \\"computed_id\\";
case \\"SERIALIZE\\":
return state;
}
return state;
};
persistentReducer.hasCustomPersistence = true;
export const exportedPersistentReducer = (state = false, action) => {
switch (action.type) {
case COMPUTED_IDENTIFIER:
return \\"computed_id\\";
case \\"SERIALIZE\\":
return state;
}
return state;
};
exportedPersistentReducer.hasCustomPersistence = true;
const persistentReducerArray = [];
reducerArray[0] = (state = false, action) => {
switch (action.type) {
case COMPUTED_IDENTIFIER:
return \\"computed_id\\";
case \\"DESERIALIZE\\":
return state;
}
return state;
};
reducerArray[0].hasCustomPersistence = true;
const persistentReducerObj = {
key: // TODO: HANDLE PERSISTENCE
(state = false, action) => {
switch (action.type) {
case COMPUTED_IDENTIFIER:
return \\"computed_id\\";
case \\"DESERIALIZE\\":
return state;
}
return state;
}
};
const validatedReducer = withSchemaValidation(schema, (state = false, action) => {
switch (action.type) {
case COMPUTED_IDENTIFIER:
return \\"computed_id\\";
}
return state;
});
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_folder(__dirname);
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This comment should be preserved even if the line below is removed.
import { createReducer, createReducerWithValidation } from 'state/utils';

const COMPUTED_IDENTIFIER = 'COMPUTED_IDENTIFIER';

const isFetchingSettings = createReducer( false, {
[ COMPUTED_IDENTIFIER ]: () => 'computed_id',
[ 'COMPUTED_STRING' ]: state => state,
NON_COMPUTED_STRING: ( state, action ) => action.thing,
2: () => 2,
FUNCTION_HANDLER: function( s, a ) {
return s;
},
ARROW_FUNCTION_HANDLER: ( state, action ) => state,
ARROW_FUNCTION_WITH_DESTRUCT: ( state, { thing } ) => thing,
VARIABLE_HANDLER: f,
} );

function f() {
return 'a function reducer';
}

const persistentReducer = createReducer(false, {
[COMPUTED_IDENTIFIER]: () => "computed_id",
["SERIALIZE"]: state => state,
});

export const exportedPersistentReducer = createReducer(false, {
[COMPUTED_IDENTIFIER]: () => "computed_id",
["SERIALIZE"]: state => state,
});

const persistentReducerArray = [];
reducerArray[0] = createReducer(false, {
[COMPUTED_IDENTIFIER]: () => "computed_id",
["DESERIALIZE"]: state => state,
});

const persistentReducerObj = {
key: createReducer(false, {
[COMPUTED_IDENTIFIER]: () => "computed_id",
["DESERIALIZE"]: state => state,
})
};

const validatedReducer = createReducerWithValidation(false, {
[COMPUTED_IDENTIFIER]: () => "computed_id",
}, schema);

0 comments on commit 69b805e

Please sign in to comment.