Created
October 22, 2022 22:03
-
-
Save jonesnxt/6a01a8dfd657a87459b39ae8f039ef98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
S('CallExpression.objectType').each((objectType) => { | |
const obj = objectType.children('ObjectExpression'); | |
const nameString = obj.children('Property.name').children().eq(1).text(); | |
const definition = obj.children('Property.definition'); | |
const stringFunctions = definition.find('CallExpression.t.string'); | |
const refactoredStringFunctions = stringFunctions.map((fn) => { | |
const paramAsValue = fn.children().eq(1).text(); | |
const paramAsKey = | |
paramAsValue.startsWith("'") || paramAsValue.startsWith('"') | |
? paramAsValue.slice(1, paramAsValue.length - 1) | |
: `[${paramAsValue}]`; | |
// check if we have a resolve function to use | |
if (fn.find('Property.resolve').length !== 0) { | |
const resolve = fn.find('Property.resolve').text(); | |
return `${paramAsKey}: t.string({ | |
${resolve} | |
})`; | |
} | |
// if not its just an expose. | |
return `${paramAsKey}: t.exposeString(${paramAsValue})`; | |
}); | |
// rewrite with what we have | |
objectType.text( | |
`builder.objectRef< TSFIXUP >(${nameString}).implement({ | |
fields: t => ({ | |
${refactoredStringFunctions.join(',\n')} | |
}) | |
});` | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment