Skip to content

Instantly share code, notes, and snippets.

@jonesnxt
Created October 22, 2022 22:03
Show Gist options
  • Save jonesnxt/6a01a8dfd657a87459b39ae8f039ef98 to your computer and use it in GitHub Desktop.
Save jonesnxt/6a01a8dfd657a87459b39ae8f039ef98 to your computer and use it in GitHub Desktop.
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