Search code examples
formattinggruntjswebstormjslint

WebStorm 2016 auto format code based on custom requirements


I am trying to auto format my code to pass JSLint violations. I know I can press Ctrl + Alt + L, but that doesn't format the code in the way I want.

For example, JSLint complains about variables not all being defined on top of the function. I would want my code formatter to move those variables to the top of the function (or having the linter not complain about that would be better). Another example would be the linter complaining about unused variables. In this instance, I would want the formatter to remove the unused variables. Of course, spacing issues should also be fixed as the default formatter does.

If this is not possible within WebStorm, can I edit a JSLint config file to turn off some warnings (like defining all variables on top of the function).

I am using grunt-jslint.


Solution

  • Unfortunately WebStorm doesn't support importing code style preferences from JSLint (WEB-2227). Neither it supports auto-fixing JSlint errors. And most JSLint rules are hardcoded and thus can't be disabled (see http://www.jslint.com/help.html for available options; they can be setup right in your Gruntfile - see http://derpturkey.com/jslint-with-grunt/)

    I'd say that JSLint is a bit out of date, and it's unlikely that more support for it will be added to WebStorm. Try using ESLint instead - it's much more flexible, and is deeply integrated in webstorm - https://blog.jetbrains.com/webstorm/2017/06/webstorm-2017-2-eap-172-3198/, https://blog.jetbrains.com/webstorm/2016/09/webstorm-2016-3-eap-163-3983/. And it can be set up as a part of Grunt build process - https://www.npmjs.com/package/grunt-eslint