javascript_guidelines.rst 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. JavaScript coding guidelines
  2. ============================
  3. Write JavaScript according to the `Airbnb Styleguide <http://github.com/airbnb/javascript>`_, with some exceptions:
  4. - Use soft-tabs with a four space indent. Spaces are the only way to
  5. guarantee code renders the same in any person's environment.
  6. - We accept ``snake_case`` in object properties, such as
  7. ``ajaxResponse.page_title``, however camelCase or UPPER_CASE should be used
  8. everywhere else.
  9. Linting and formatting code
  10. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. Wagtail provides some tooling configuration to help check your code meets the
  12. styleguide. You'll need node.js and npm on your development machine.
  13. Ensure project dependencies are installed by running ``npm install``
  14. **Linting code**
  15. ``npm run lint:js``
  16. This will lint all the JS in the wagtail project, excluding vendor
  17. files and compiled libraries.
  18. Some of the modals are generated via server-side scripts. These include
  19. template tags that upset the linter, so modal workflow JavaScript is
  20. excluded from the linter.
  21. **Formatting code**
  22. ``npm run format:js``
  23. This will perform safe edits to conform your JS code to the styleguide.
  24. It won't touch the line-length, or convert quotemarks from double to single.
  25. Run the linter after you've formatted the code to see what manual fixes
  26. you need to make to the codebase.
  27. **Changing the linter configuration**
  28. Under the hood, the tasks use the `JavaScript Code Style <http://jscs.info/>`_ library.
  29. To edit the settings for ignored files, or to change the linting rules,
  30. edit the ``.jscsrc`` file in the wagtail project root.
  31. A complete list of the possible linting rules can be found here:
  32. `JSCS Rules <http://jscs.info/rules.html>`_