2
0

.eslintrc.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. module.exports = {
  2. extends: [
  3. '@wagtail/eslint-config-wagtail',
  4. 'plugin:@typescript-eslint/recommended',
  5. ],
  6. parser: '@typescript-eslint/parser',
  7. plugins: ['@typescript-eslint'],
  8. env: {
  9. jest: true,
  10. browser: true,
  11. },
  12. rules: {
  13. '@typescript-eslint/explicit-function-return-type': 'off',
  14. '@typescript-eslint/explicit-member-accessibility': 'off',
  15. '@typescript-eslint/explicit-module-boundary-types': 'off',
  16. '@typescript-eslint/no-explicit-any': 'off',
  17. '@typescript-eslint/no-use-before-define': ['error'],
  18. // it is often helpful to pull out logic to class methods that may not use `this`
  19. 'class-methods-use-this': 'off',
  20. 'import/extensions': [
  21. 'error',
  22. 'always',
  23. {
  24. ignorePackages: true,
  25. pattern: {
  26. js: 'never',
  27. jsx: 'never',
  28. ts: 'never',
  29. tsx: 'never',
  30. },
  31. },
  32. ],
  33. // does not align with the majority of legacy and newer code, some use named others use default exports
  34. 'import/prefer-default-export': 'off',
  35. // allow no lines between single line members (e.g. static declarations)
  36. 'lines-between-class-members': [
  37. 'error',
  38. 'always',
  39. { exceptAfterSingleLine: true },
  40. ],
  41. 'max-classes-per-file': 'off',
  42. // note you must disable the base rule as it can report incorrect errors
  43. 'no-use-before-define': 'off',
  44. 'react/jsx-filename-extension': ['error', { extensions: ['.js', '.tsx'] }],
  45. 'no-underscore-dangle': [
  46. 'error',
  47. { allow: ['__REDUX_DEVTOOLS_EXTENSION__'] },
  48. ],
  49. // this rule can be confusing as it forces some non-intuitive code for variable assignment
  50. 'prefer-destructuring': 'off',
  51. },
  52. settings: {
  53. 'import/core-modules': ['jquery'],
  54. 'import/resolver': { node: { extensions: ['.js', '.ts', '.tsx'] } },
  55. },
  56. overrides: [
  57. // Rules that we are ignoring currently due to legacy code in React components only
  58. {
  59. files: ['client/src/components/**'],
  60. rules: {
  61. 'jsx-a11y/click-events-have-key-events': 'off',
  62. 'jsx-a11y/interactive-supports-focus': 'off',
  63. 'jsx-a11y/no-noninteractive-element-interactions': 'off',
  64. 'jsx-a11y/role-supports-aria-props': 'off',
  65. 'no-restricted-syntax': 'off',
  66. 'react-hooks/exhaustive-deps': 'off',
  67. 'react-hooks/rules-of-hooks': 'off',
  68. 'react/button-has-type': 'off',
  69. 'react/destructuring-assignment': 'off',
  70. 'react/forbid-prop-types': 'off',
  71. 'react/function-component-definition': 'off',
  72. 'react/jsx-props-no-spreading': 'off',
  73. 'react/no-danger': 'off',
  74. 'react/no-deprecated': 'off',
  75. 'react/require-default-props': 'off',
  76. },
  77. },
  78. // Rules we want to enforce or change for Stimulus Controllers
  79. {
  80. files: ['*Controller.ts'],
  81. rules: {
  82. '@typescript-eslint/member-ordering': [
  83. 'error',
  84. {
  85. classes: {
  86. memberTypes: ['signature', 'field', 'method'],
  87. },
  88. },
  89. ],
  90. '@typescript-eslint/naming-convention': [
  91. 'error',
  92. {
  93. selector: 'method',
  94. format: ['camelCase'],
  95. custom: {
  96. // Use connect or initialize instead of constructor, avoid generic 'render' or 'update' methods and instead be more specific.
  97. regex: '^(constructor|render|update)$',
  98. match: false,
  99. },
  100. },
  101. {
  102. selector: 'property',
  103. format: ['camelCase'],
  104. custom: {
  105. // Use Stimulus values where possible for internal state, avoid a generic state object as these are not reactive.
  106. regex: '^(state)$',
  107. match: false,
  108. },
  109. },
  110. ],
  111. 'no-restricted-properties': [
  112. 'error',
  113. {
  114. object: 'window',
  115. property: 'Stimulus',
  116. message:
  117. "Please import the base Controller or only access the Stimulus instance via the controller's `this.application` attribute.",
  118. },
  119. ],
  120. },
  121. },
  122. // Rules we don’t want to enforce for test and tooling code.
  123. {
  124. files: [
  125. 'client/extract-translatable-strings.js',
  126. 'client/tests/**',
  127. 'webpack.config.js',
  128. 'tailwind.config.js',
  129. 'storybook/**/*',
  130. '*.test.ts',
  131. '*.test.tsx',
  132. '*.test.js',
  133. '*.stories.js',
  134. '*.stories.tsx',
  135. ],
  136. rules: {
  137. '@typescript-eslint/no-empty-function': 'off',
  138. '@typescript-eslint/no-unused-vars': 'off',
  139. '@typescript-eslint/no-var-requires': 'off',
  140. 'global-require': 'off',
  141. 'import/first': 'off',
  142. 'import/no-extraneous-dependencies': 'off',
  143. 'no-unused-expressions': 'off',
  144. 'react/function-component-definition': 'off',
  145. 'react/jsx-props-no-spreading': 'off',
  146. },
  147. },
  148. // Files that use jquery via a global
  149. {
  150. files: [
  151. 'docs/_static/**',
  152. 'wagtail/contrib/modeladmin/static_src/wagtailmodeladmin/js/prepopulate.js',
  153. 'wagtail/contrib/search_promotions/templates/wagtailsearchpromotions/includes/searchpromotions_formset.js',
  154. 'wagtail/contrib/settings/static_src/wagtailsettings/js/site-switcher.js',
  155. 'wagtail/documents/static_src/wagtaildocs/js/add-multiple.js',
  156. 'wagtail/embeds/static_src/wagtailembeds/js/embed-chooser-modal.js',
  157. 'wagtail/images/static_src/wagtailimages/js/add-multiple.js',
  158. 'wagtail/images/static_src/wagtailimages/js/focal-point-chooser.js',
  159. 'wagtail/images/static_src/wagtailimages/js/image-url-generator.js',
  160. 'wagtail/search/static_src/wagtailsearch/js/query-chooser-modal.js',
  161. 'wagtail/search/templates/wagtailsearch/queries/chooser_field.js',
  162. 'wagtail/snippets/static_src/wagtailsnippets/js/snippet-multiple-select.js',
  163. 'wagtail/users/static_src/wagtailusers/js/group-form.js',
  164. ],
  165. globals: { $: 'readonly', jQuery: 'readonly' },
  166. },
  167. // Files that use other globals or legacy/vendor code that is unable to be easily linted
  168. {
  169. files: ['wagtail/**/**'],
  170. globals: {
  171. buildExpandingFormset: 'readonly',
  172. cancelSpinner: 'readonly',
  173. escapeHtml: 'readonly',
  174. jsonData: 'readonly',
  175. ModalWorkflow: 'readonly',
  176. DOCUMENT_CHOOSER_MODAL_ONLOAD_HANDLERS: 'writable',
  177. EMBED_CHOOSER_MODAL_ONLOAD_HANDLERS: 'writable',
  178. IMAGE_CHOOSER_MODAL_ONLOAD_HANDLERS: 'writable',
  179. QUERY_CHOOSER_MODAL_ONLOAD_HANDLERS: 'writable',
  180. SNIPPET_CHOOSER_MODAL_ONLOAD_HANDLERS: 'writable',
  181. },
  182. rules: {
  183. '@typescript-eslint/no-unused-vars': 'off',
  184. '@typescript-eslint/no-use-before-define': 'off',
  185. 'camelcase': [
  186. 'error',
  187. {
  188. allow: [
  189. '__unused_webpack_module',
  190. '__webpack_modules__',
  191. '__webpack_require__',
  192. ],
  193. properties: 'never',
  194. },
  195. ],
  196. 'consistent-return': 'off',
  197. 'func-names': 'off',
  198. 'id-length': 'off',
  199. 'indent': 'off',
  200. 'key-spacing': 'off',
  201. 'new-cap': 'off',
  202. 'newline-per-chained-call': 'off',
  203. 'no-param-reassign': 'off',
  204. 'no-underscore-dangle': 'off',
  205. 'object-shorthand': 'off',
  206. 'prefer-arrow-callback': 'off',
  207. 'quote-props': 'off',
  208. 'space-before-function-paren': 'off',
  209. 'vars-on-top': 'off',
  210. },
  211. },
  212. ],
  213. };