codemod icon indicating copy to clipboard operation
codemod copied to clipboard

Decorators cannot be used to decorate parameters.

Open forabi-cosuno opened this issue 2 years ago • 1 comments

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.

forabi-cosuno avatar Feb 15 '23 13:02 forabi-cosuno

Try @codemod/[email protected] (just published) like so:

codemod --parser-plugins decorators-legacy …

eventualbuddha avatar Feb 17 '23 06:02 eventualbuddha