4

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?

3
  • 3
    "But if my code is compressed, it won't format it nicely to human readable format." - Why is your code compressed? It's fine (good, even) to minify your code for deployment purposes, but you should be writing and maintaining it in a human-readable format. Commented Oct 6, 2016 at 1:58
  • 1
    If you look at the list of ESLint rules, you'll see that only some rules can be fixed automatically with ---fix. Commented Oct 6, 2016 at 2:03
  • @nnnnnn, well, I don't mean to format compressed file, it's just easier to test if eslint --fix will format them to separate lines nicely. using compressed file just for easy testing Commented Oct 7, 2016 at 1:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

thanks for your answer. What I meant to do is use eslint as a code formatter as well. e.g. if several statements on the same line, they'll be formatted to separate lines. if code is not well indented, it'll indent the code nicely. Just want to find a way to tell eslint to do such format job according to my eslint configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.