javascript_guidelines.rst 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. JavaScript coding guidelines
  2. ============================
  3. Write JavaScript according to the `Airbnb Styleguide <https://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 uses the `ESLint <https://eslint.org/>`_ linter 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 --no-save``
  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 lint:js -- --fix
  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. The configuration for the linting rules is managed in an external
  34. repository so that it can be easily shared across other Wagtail projects
  35. or plugins. This configuration can be found at
  36. `eslint-config-wagtail <https://github.com/wagtail/eslint-config-wagtail>`_.