ggascoigne / prettier-plugin-import-sort

Prettier plugin to pass javascript and typescript through import-sort
MIT License
110 stars 11 forks source link

Parsing error: Stage 2 decorators cannot be used to decorate parameters #3

Closed bbenoist closed 4 years ago

bbenoist commented 4 years ago

Hi, thanks for your work!

I have an error with one of my TypeScript project. I've made a simple project reproducing the issue: bbenoist/prettier-plugin-import-sort-arg-decorator-bug

The following valid TypeScript code:

function ArgDecorator(_target: Record<string, any>, _propertyKey: string | symbol, _parameterIndex: number): void {
    console.log(_target, _propertyKey, _parameterIndex)
}

class MyClass {
    myFunction(@ArgDecorator myArg: string){
        console.log(myArg);
    }
}

const foo = new MyClass;
foo.myFunction("hello");

Gives this error (full log):

$ npm run-script lint
> eslint --ext .jsx,.js,.ts,.tsx .
...
src/index.ts
  6:16  error  Parsing error: Stage 2 decorators cannot be used to decorate parameters  prettier/prettier
...

Before adding prettier-plugin-import-sort and import-sort-style-module, prettier didn't complain about the function argument decorator so I suppose that there is something wrong on this side...

bbenoist commented 4 years ago

I've created an issue upstream ( https://github.com/renke/import-sort/issues/101 ) as I had the same problem with import-sort-cli.

ggascoigne commented 4 years ago

That's for that, I rather assumed that that would be the source of the issue but hadn't had time to check it out.

bbenoist commented 4 years ago

Fixed with adding "parser": "typescript" in my package.json config:

  "importSort": {
    ".js, .jsx, .ts, .tsx": {
      "style": "module",
      "parser": "typescript"
    }
  }
ggascoigne commented 4 years ago

interesting, I wonder why I didn't need this. Either way, I'll update the docs to reflect this as a possible option.

Thank you for coming back with a solution.