jscodeshift icon indicating copy to clipboard operation
jscodeshift copied to clipboard

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

Open runspired opened this issue 3 years ago • 3 comments

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() {}
}

runspired avatar Jan 02 '22 19:01 runspired

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 avatar Jan 25 '22 21:01 NullVoxPopuli

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

NullVoxPopuli avatar Jan 25 '22 21:01 NullVoxPopuli

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

dsokur avatar Mar 26 '23 10:03 dsokur