jscs-dev / node-jscs

:arrow_heading_up: JavaScript Code Style checker (unmaintained)
https://jscs-dev.github.io
MIT License
4.96k stars 513 forks source link

validateIndentation: Add 'consistent' mode. #2239

Closed kylepaulsen closed 8 years ago

kylepaulsen commented 8 years ago

Consistent mode tries to guess the intended indentation in the file and sets the indentation settings accordingly.

Hello, I finally decided to take the time to do this. This just adds a 'consistent' option to validateIndentation. So now validateIndentation can be a number, "\t" or "consistent". This is helpful for people who work on a wide variety of projects with their own indentation rules where the projects don't necessarily have a jscs file. It is also helpful for people who have a global jscsrc file so they can enjoy consistent indentation along with all of their other favorite style rules. This is especially helpful for editors that use that global jscsrc file on any source file (a new unsaved file or otherwise).

It's important to note that when using consistent mode, jscs has to make a guess at what the intended indentation should be. So there might be cases where it will guess wrong. So the trade off is more flexibility when switching between projects with different indentation rules and incorrect indentation warnings (probably a rare occurrence).

Here's how it works: After a lot of thought, I decided that the most reliable place to sample indentation is the first Whitespace node directly inside a BlockStatement node from the AST. Additionally, the Whitespace node must start with a newline character (\n or \r\n). If it can't find a place to sample the indentation, it will try once more, but this time the Whitespace node doesn't have to be in a BlockStatement. However, if it has to search a second time, it will only take note of the type of indentation being used (so it will only warn if the code mixes spaces and tabs).

I think this would be a good option for jscs to have. It would certainly save me a lot of frustration. Let me know if I need to change anything about the PR. Thanks!

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-0.06%) to 96.723% when pulling ecd9396d4d8a6e8e6e08858d32ef62ad52fe4ba7 on kylepaulsen:kylepaulsen/indentation-consistent-option into 2cdb26d172fc8e19779dffcbab88ca8ab8f06fb9 on jscs-dev:master.

markelog commented 8 years ago

/cc @mdevils

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.04%) to 96.821% when pulling 41ca94e4fb6ba182cae4ef09b8545a23804ff112 on kylepaulsen:kylepaulsen/indentation-consistent-option into 2cdb26d172fc8e19779dffcbab88ca8ab8f06fb9 on jscs-dev:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.04%) to 96.786% when pulling ac9d7ff96e174363ca364802418e00d087eb5191 on kylepaulsen:kylepaulsen/indentation-consistent-option into 724b2df8e2fa1db75ff0fc61dab86ca7efc67a47 on jscs-dev:master.

kylepaulsen commented 8 years ago

I think Travis is complaining about things unrelated to my PR. Can someone trigger a rebuild?

markelog commented 8 years ago

Really sorry, we do not accept new features at this time :/.

See https://medium.com/@markelog/jscs-end-of-the-line-bc9bf0b3fdb2#.jsnhpxsjz