codemod
codemod copied to clipboard
Decorators cannot be used to decorate parameters.
Trying to transform some TS that looks like this:
@Query((_returns) => AwardedBid)
async awardedBidForAgent(@Ctx() context: ResolverContext, @Arg('id', (_type) => ID) id: string) {
const { user } = context;
const bid = await this.prisma.bid.findFirst({
/// ...
rejectOnNotFound: true,
});
return {
/// ....
};
}
Using this custom plugin:
function propIsRejectOnNotFound(prop) {
return prop.key.name === 'rejectOnNotFound';
}
export default function (babel) {
const { types: t } = babel;
return {
name: 'ast-transform', // not required
visitor: {
Identifier(path) {},
CallExpression(path) {
if (['findUnique', 'findFirst'].includes(path.node.callee.property.name)) {
if (path.node.arguments[0].type === 'ObjectExpression') {
console.log(path.node.arguments);
const arg = path.node.arguments[0].properties.findIndex(propIsRejectOnNotFound);
console.log({ arg });
if (arg) {
path.node.arguments[0].properties.splice(arg, 1);
path.node.callee.property.name += 'OrThrow';
}
}
}
},
},
};
}
I get this error when using the CLI:
Decorators cannot be used to decorate parameters.
Try @codemod/[email protected] (just published) like so:
codemod --parser-plugins decorators-legacy …