|
| 1 | +import { createTransformer } from '../lib/create-transformer'; |
| 2 | + |
| 3 | +export default createTransformer((fileInfo, api, options, context) => { |
| 4 | + const { j, root } = context; |
| 5 | + |
| 6 | + // Helper function to convert snake_case to camelCase |
| 7 | + function snakeToCamel(str: string): string { |
| 8 | + return str.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase()); |
| 9 | + } |
| 10 | + |
| 11 | + // Helper function to recursively transform snake_case properties in an object |
| 12 | + function transformFalProperties(objExpression: any) { |
| 13 | + if (objExpression.type !== 'ObjectExpression') return; |
| 14 | + |
| 15 | + objExpression.properties.forEach((prop: any) => { |
| 16 | + if ( |
| 17 | + (prop.type === 'ObjectProperty' || prop.type === 'Property') && |
| 18 | + prop.key.type === 'Identifier' |
| 19 | + ) { |
| 20 | + const originalName = prop.key.name; |
| 21 | + const camelCaseName = snakeToCamel(originalName); |
| 22 | + |
| 23 | + // Only transform if the name actually changes (contains snake_case) |
| 24 | + if (originalName !== camelCaseName && originalName.includes('_')) { |
| 25 | + context.hasChanges = true; |
| 26 | + prop.key = j.identifier(camelCaseName); |
| 27 | + } |
| 28 | + |
| 29 | + // Recursively transform nested objects |
| 30 | + if (prop.value.type === 'ObjectExpression') { |
| 31 | + transformFalProperties(prop.value); |
| 32 | + } |
| 33 | + } |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + // Find all ObjectExpression nodes that could contain providerOptions |
| 38 | + root.find(j.ObjectExpression).forEach(objectPath => { |
| 39 | + // Look for providerOptions property within this object |
| 40 | + objectPath.node.properties.forEach((prop: any) => { |
| 41 | + if ( |
| 42 | + (prop.type === 'ObjectProperty' || prop.type === 'Property') && |
| 43 | + prop.key.type === 'Identifier' && |
| 44 | + prop.key.name === 'providerOptions' && |
| 45 | + prop.value.type === 'ObjectExpression' |
| 46 | + ) { |
| 47 | + // Found providerOptions, now look for fal property |
| 48 | + prop.value.properties.forEach((providerProp: any) => { |
| 49 | + if ( |
| 50 | + (providerProp.type === 'ObjectProperty' || |
| 51 | + providerProp.type === 'Property') && |
| 52 | + providerProp.key.type === 'Identifier' && |
| 53 | + providerProp.key.name === 'fal' && |
| 54 | + providerProp.value.type === 'ObjectExpression' |
| 55 | + ) { |
| 56 | + // Found fal, transform its properties |
| 57 | + transformFalProperties(providerProp.value); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments