jscheid / prettier.el

Prettier code formatting for Emacs.
GNU General Public License v3.0
163 stars 11 forks source link

Typescript casting not supported #129

Closed farynaio closed 7 months ago

farynaio commented 7 months ago

Describe the bug Running prettier through the Typescript code with casting end up with SyntaxError: Missing semicolon. error.

Link to M-x prettier-info output

https://gist.github.com/farynaio/2a1eb382e65ab67b44d396fa44f3100f

To Reproduce

  1. Create foo.tsx file
  2. Put in one line const foobar = process.env.FOO as string;
  3. Run prettier-prettify

Expected behavior The code with casting should be considered proper Typescript.

Additional context none

jscheid commented 7 months ago

You're using rjsx-mode which makes this package guess the babel parser, which doesn't support TypeScript syntax. Really this package ought to ignore major mode (at least for buffers corresponding to a file) and use Prettier's guess based on the file extension instead, but that's not how it currently works.

You can work around this in one of a few different ways:

If you don't want to use typescript-mode I'd be curious to hear why you prefer rjsx, just because I edit quite a bit of TS code and wondering what I might be missing out on :-)

farynaio commented 7 months ago

You're using rjsx-mode which makes this package guess the babel parser, which doesn't support TypeScript syntax. Really this package ought to ignore major mode (at least for buffers corresponding to a file) and use Prettier's guess based on the file extension instead, but that's not how it currently works.

You can work around this in one of a few different ways:

* Use `typescript-mode` (I find it works well for tsx files as well)

* Force `prettier-parser`, for instance through a mode hook, file variables or dir locals

* Tweak `prettier-major-mode-parsers` to add rjsx mode (bit of a hack)

If you don't want to use typescript-mode I'd be curious to hear why you prefer rjsx, just because I edit quite a bit of TS code and wondering what I might be missing out on :-)

Thanks for prompt reply. The typescript-mode is not maintained anymore, and it used to highlight pretty badly some of my TSX files - half of the file was highlighted as a string. And from my experience it doesn't support TSX but rather only TS. I'm testing rjsx-mode now as it used to work great with JSX files, and tbh so far so good. I have a quick shortcut to switch to web-mode but I haven't used it yet. I needed quick fix for Prettier so I temporarily switched to different plugin, but if I hit the wall with them, I will be happy to back.