facebook / jscodeshift

A JavaScript codemod toolkit.
https://jscodeshift.com
MIT License
9.27k stars 478 forks source link

code.find(codeshift.Decorator) ignores decorators on class properties #469

Open runspired opened 2 years ago

runspired commented 2 years ago

code.find(codeshift.Decorator) ignores decorators on nodes of type ClassProperty

Example input code:

class Foo {
  @service aProp;
  @service() bProp;
  @service
  get cProp() {}
  @service
  dProp() {}
}

Example transform

export const parser = "ts";

export default function transformer(file, api) {
  const j = api.jscodeshift;
  const code = j(file.source);

  code.find(j.Decorator).forEach(path => {
    if (
      path.value.expression.type === "Identifier" &&
      path.value.expression.name === "service"
    ) {
      path.value.expression.name = "inject";
    }
  });

  return code.toSource();
}

Output (unexpected)

class Foo {
  @service aProp;
  @service() bProp;
  @inject
  get cProp() {}
  @inject
  dProp() {}
}
NullVoxPopuli commented 2 years ago

I just ran in to this same thing.

Here is my example: https://astexplorer.net/#/gist/a8ffcb056b86f73e6b23bd8a8b08e69e/fa79daed27bfbb99fad3274b28e717b716c45a5f I'm using the flow parser here

decorators exist on methods, but not properties? image

NullVoxPopuli commented 2 years ago

Looks like I have to switch to the ts parser: image

but that doesn't make sense either :thinking: the find(j.Decorator) still only finds 3 decorators, when I expect there to be 8

dsokur commented 1 year ago

Any update on this issue ? i can't create Class function decorators like @property https://astexplorer.net/#/gist/a8ffcb056b86f73e6b23bd8a8b08e69e/b0cb9478fd38744aa1fbe0f1c78607ab4953ceca