Hey everyone! I'm on the hunt for a JavaScript formatter that lets me skip certain sections of code. While I'm not too picky about the formatting style itself, the ability to exclude sections is a must-have, which rules out using Prettier.
For context, here's an example of a section I'd want to keep unformatted:
```javascript
class Foo {
...
// stop-formatting
get lines() { return this.#lines.length }
get col() { return this.#x + 1 }
get row() { return this.#y + 1 }
get done() { return this.#y >= this.#lines.length }
get eol() { return this.#x >= this.current_line.length }
// resume-formatting
}
```
Any suggestions would be appreciated!
3 Answers
Prettier only supports a 'range' ignore in markdown, and it doesn’t let you skip sections for formatting. I’ve heard of Biome, which has range suppressions, but it’s only for linting, not formatting.
A workaround could be to add an 'ignore' comment right above your `class Foo` to skip formatting there.
I might be confused here, but Prettier does let you ignore code via comments. Check out their official documentation for ignoring code. Is that not working for your case?
Nope, it doesn't work for my JavaScript setup.
Can you maybe use arrow functions for this? Or is that not applicable within classes? I'm not totally sure how that works.
Unfortunately, arrow functions would bind `this` to the class and not the instance. Also, you'd end up creating duplicate functions on each instance instead of having them on the prototype. But don't take my word for it; it’s worth a check.
I’m not sure either. It would be cool if it worked like that!
I didn't know about Biome! I'll check it out as a linter, even if it won't help with formatting.