Hey everyone! I'm on the hunt for a JavaScript formatter that has the ability to skip certain sections of code when formatting. I'm not too concerned about the specific style it uses, but the ability to exclude sections is a huge requirement for me, so I've ruled out Prettier.
Here's an example of the code section I want to exclude from formatting:
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
}
Thanks in advance for any suggestions!
3 Answers
Maybe I’m missing something here, but I thought Prettier allows ignoring specific lines via comments? Check out their documentation: https://prettier.io/docs/ignore. Does that not work for your situation?
I’ve found that Prettier only allows for a “range” in markdown, which isn’t quite what you're looking for. Biome does offer range suppressions, but you should know it works for linting and not actual formatting. There might be some performance limitations with other tools regarding range formatting. I'd suggest adding an “ignore” comment right at the start of `class Foo` to exclude the whole thing from formatting.
I hadn’t heard of Biome before—definitely going to check that out! Since it’s only for linting, it might not solve your issue, but it’s worth a look.
Have you thought about using arrow functions? I wonder if they can help with this, but I'm not sure how they operate in classes.
Unfortunately, arrow functions would bind `this` to the class itself, which isn’t ideal. Additionally, they would need to be stored in fields, meaning each class instance would create duplicate copies instead of sharing them via the prototype chain. But hey, double-check to be certain!
Sounds like a useful feature if it were possible! Anyone else have insights?
Nope, it doesn't seem to function that way in JavaScript unfortunately.