javascript_guidelines.rst 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. ------------
  16. .. code-block:: console
  17. $ npm run lint:js
  18. This will lint all the JS in the wagtail project, excluding vendor
  19. files and compiled libraries.
  20. Some of the modals are generated via server-side scripts. These include
  21. template tags that upset the linter, so modal workflow JavaScript is
  22. excluded from the linter.
  23. Formatting code
  24. ---------------
  25. .. code-block:: console
  26. $ npm run format:js
  27. This will perform safe edits to conform your JS code to the styleguide.
  28. It won't touch the line-length, or convert quotemarks from double to single.
  29. Run the linter after you've formatted the code to see what manual fixes
  30. you need to make to the codebase.
  31. Changing the linter configuration
  32. ---------------------------------
  33. Under the hood, the tasks use the `JavaScript Code Style <http://jscs.info/>`_ library.
  34. To edit the settings for ignored files, or to change the linting rules,
  35. edit the ``.jscsrc`` file in the wagtail project root.
  36. A complete list of the possible linting rules can be found here:
  37. `JSCS Rules <http://jscs.info/rules.html>`_