Added documentation and comments to update-addon-info codemod

This commit is contained in:
Trevor Eyre 2017-08-03 22:02:53 -06:00
parent 83aceed1a4
commit 256a3e6913

View File

@ -1,7 +1,39 @@
/**
* Takes the deprecated addon-info API, addWithInfo, and
* converts to the new withInfo API.
*
* Example of deprecated addWithInfo API:
*
* storiesOf('Button')
* .addWithInfo(
* 'story name',
* 'Story description.',
* () => (
* <Button label="The Button" />
* )
* )
*
* Converts to the new withInfo API:
*
* storiesOf('Button')
* .add('story name', withInfo(
* 'Story description.'
* )(() => (
* <Button label="The Button" />
* )))
*/
export default function transformer (file, api) {
const j = api.jscodeshift;
const root = j(file.source);
/**
* Returns a list of parameters for the withInfo function. The contents
* of this list is either the second argument from the original
* addWithInfo function, if no additional options were used, or a
* combined object of all the options from the original function.
* @param {list} args - original addWithInfo function parameters
* @return {list} the modified list of parameters for the new function
*/
const getOptions = args => {
if (args[3] === undefined) {
return [args[1]];
@ -14,6 +46,12 @@ export default function transformer (file, api) {
];
};
/**
* Constructs the new withInfo function from the parameters of the
* original addWithInfo function.
* @param {CallExpression} addWithInfoExpression - original function
* @returns {CallExpression} the new withInfo function
*/
const withInfo = addWithInfoExpression => {
const node = addWithInfoExpression.node;
const args = node.arguments;
@ -33,6 +71,10 @@ export default function transformer (file, api) {
return node;
};
/**
* Checks for - import { withInfo } from "@storybook/addon-info";
* Adds the import if necessary.
*/
const checkWithInfoImport = () => {
const importExists = root.find(j.ImportDeclaration)
.filter(imp => imp.node.source.value === '@storybook/addon-info')