Aaron Shen
Aaron Shen

Reputation: 8374

How to use eslint to format my javascript code?

I'm using vim as my editor, I use eslint for linting javascript code. Before, I use js-beautify for formatting js code. But recently I found that eslint has a --fix option, which can fix normal coding style issue violate eslint rules.

I also find this post which suggests use eslint for code formatting. I feel it's a great idea, because I find it's kind of redundant to have both eslint and js-beautify. Since eslint can already detects all my coding style issues, why can't it do format for me?

there's also one drawback to have both js-beautify and eslint, that I need to adjust js-beautify to be in accordance with eslint rules. This is completely unnecessary.

But when I use eslint --fix to js file, it only do some fixing like: insert semicolon, add some space where necessary. But if my code is compressed, it won't format it nicely to human readable format.

How can I make eslint do code format as js-beautify does?

Upvotes: 4

Views: 1531

Answers (2)

Brad
Brad

Reputation: 163752

Not all rules can be automatically fixed. Sometimes, it's important for a human to decide how to correct a problem. These rules won't be fixed for you.

Other rules, like trailing commas, can be safely fixed automatically, and are applied with --fix.

Upvotes: 0

Aaron Shen
Aaron Shen

Reputation: 8374

I didn't really make eslint format code. But a trick can be used is to pipe the result of js-beautify to eslint --fix. Thus multiple statements on same line will be formatted by js-beautify and the result will be sent to eslint --fix to fix normal style issues, in the end, you get the formatted and fixed version of code.

Upvotes: 3

Related Questions