|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const prettier = require( 'prettier' ); |
|
|
const config = require( './config' ); |
|
|
|
|
|
export default function transformer( file, api ) { |
|
|
const j = api.jscodeshift; |
|
|
const root = j( file.source ); |
|
|
|
|
|
|
|
|
const stepEls = root.findJSXElements( 'Step' ); |
|
|
if ( stepEls.size() === 0 ) { |
|
|
|
|
|
return null; |
|
|
} |
|
|
|
|
|
stepEls.forEach( ( stepEl ) => { |
|
|
|
|
|
const stepElValue = stepEl.get().value; |
|
|
const { children } = stepElValue; |
|
|
|
|
|
|
|
|
const trIden = j.identifier( 'translate' ); |
|
|
const trProp = j.property( 'init', trIden, trIden ); |
|
|
trProp.shorthand = true; |
|
|
const fragmentIden = j.jsxIdentifier( 'Fragment' ); |
|
|
const childrenFunc = j.arrowFunctionExpression( |
|
|
[ j.objectPattern( [ trProp ] ) ], |
|
|
j.jsxElement( |
|
|
j.jsxOpeningElement( fragmentIden ), |
|
|
j.jsxClosingElement( fragmentIden ), |
|
|
children |
|
|
) |
|
|
); |
|
|
|
|
|
|
|
|
stepElValue.children = [ j.jsxExpressionContainer( childrenFunc ) ]; |
|
|
} ); |
|
|
|
|
|
|
|
|
const reactImport = root.find( j.ImportDeclaration, { source: { value: 'react' } } ); |
|
|
reactImport.get().value.specifiers.push( j.importSpecifier( j.identifier( 'Fragment' ) ) ); |
|
|
|
|
|
|
|
|
const i18nImport = root.find( j.ImportDeclaration, { source: { value: 'i18n-calypso' } } ); |
|
|
i18nImport.remove(); |
|
|
|
|
|
return prettier.format( root.toSource( config.recastOptions ), {} ); |
|
|
} |
|
|
|