javascript_guidelines.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. JavaScript coding guidelines
  2. ============================
  3. Write JavaScript according to the `Airbnb Styleguide <https://github.com/airbnb/javascript>`_, with some exceptions:
  4. - Use `Prettier <https://prettier.io/>`_ for all formatting considerations.
  5. - We accept ``snake_case`` in object properties, such as
  6. ``ajaxResponse.page_title``, however camelCase or UPPER_CASE should be used
  7. everywhere else.
  8. Linting and formatting code
  9. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. Wagtail uses `ESLint <https://eslint.org/>`_ and `Prettier <https://prettier.io/>`_.
  11. You'll need Node and npm on your development machine.
  12. Ensure project dependencies are installed by running ``npm install --no-save``.
  13. Linting code
  14. ------------
  15. .. code-block:: console
  16. $ npm run lint:js
  17. This will lint all the JS in the wagtail project, excluding vendor
  18. files and compiled libraries.
  19. Some of the modals are generated via server-side scripts. These include
  20. template tags that upset the linter, so modal workflow JavaScript is
  21. excluded from the linter.
  22. Formatting code
  23. ---------------
  24. For Prettier auto-formatting, run:
  25. .. code-block:: console
  26. $ npm run format
  27. If you want to autofix linting errors:
  28. .. code-block:: console
  29. $ npm run lint:js -- --fix
  30. This will perform safe edits to conform your JS code to the styleguide.
  31. It won't touch the line-length, or convert quotemarks from double to single.
  32. Run the linter after you've formatted the code to see what manual fixes
  33. you need to make to the codebase.
  34. Changing the linter configuration
  35. ---------------------------------
  36. The configuration for the linting rules is managed in an external
  37. repository so that it can be easily shared across other Wagtail projects
  38. or plugins. This configuration can be found at
  39. `eslint-config-wagtail <https://github.com/wagtail/eslint-config-wagtail>`_.