2
0

.eslintrc.js 7.3 KB

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