.eslintrc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. {
  2. "root": true,
  3. "parser": "@typescript-eslint/parser",
  4. "extends": [
  5. "eslint:recommended",
  6. "plugin:@typescript-eslint/recommended",
  7. "standard",
  8. "prettier"
  9. ],
  10. "env": {
  11. "es2020": true
  12. },
  13. "settings": {
  14. // Tell eslint-plugin-react to detect which version of React we are using
  15. "react": {
  16. "version": "detect"
  17. }
  18. },
  19. "rules": {
  20. // do not allow importing of implicit dependencies.
  21. "import/no-extraneous-dependencies": "error",
  22. // disable some TypeScript rules
  23. "@typescript-eslint/no-var-requires": "off",
  24. "@typescript-eslint/no-unused-vars": "off",
  25. "@typescript-eslint/no-empty-function": "off",
  26. "@typescript-eslint/no-explicit-any": "off",
  27. "@typescript-eslint/no-this-alias": "off",
  28. "@typescript-eslint/no-non-null-assertion": "off",
  29. "@typescript-eslint/ban-ts-comment": "off"
  30. },
  31. "overrides": [
  32. // NOTE: changing paths may require updating them in the Makefile too.
  33. {
  34. // Node
  35. "files": [
  36. "**/app/src/**/*.js",
  37. "app.js",
  38. "i18next-scanner.config.js",
  39. "karma.conf.js",
  40. "scripts/**/*.js",
  41. "webpack.config*.js"
  42. ],
  43. "env": {
  44. "node": true
  45. }
  46. },
  47. {
  48. // Test specific rules
  49. "files": ["**/test/**/*.*"],
  50. "plugins": [
  51. "mocha",
  52. "chai-expect",
  53. "chai-friendly"
  54. ],
  55. "env": {
  56. "mocha": true
  57. },
  58. "rules": {
  59. // mocha-specific rules
  60. "mocha/handle-done-callback": "error",
  61. "mocha/no-exclusive-tests": "error",
  62. "mocha/no-global-tests": "error",
  63. "mocha/no-identical-title": "error",
  64. "mocha/no-nested-tests": "error",
  65. "mocha/no-pending-tests": "error",
  66. "mocha/no-skipped-tests": "error",
  67. "mocha/no-mocha-arrows": "error",
  68. // Swap the no-unused-expressions rule with a more chai-friendly one
  69. "no-unused-expressions": "off",
  70. "chai-friendly/no-unused-expressions": "error",
  71. // chai-specific rules
  72. "chai-expect/missing-assertion": "error",
  73. "chai-expect/terminating-properties": "error",
  74. // prefer-arrow-callback applies to all callbacks, not just ones in mocha tests.
  75. // we don't enforce this at the top-level - just in tests to manage `this` scope
  76. // based on mocha's context mechanism
  77. "mocha/prefer-arrow-callback": "error"
  78. }
  79. },
  80. {
  81. // Frontend test specific rules
  82. "files": ["**/test/karma/**/*.js"],
  83. "globals": {
  84. "expect": true,
  85. "$": true
  86. }
  87. },
  88. {
  89. // Backend specific rules
  90. "files": ["**/app/src/**/*.js", "app.js"],
  91. "rules": {
  92. // don't allow console.log in backend code
  93. "no-console": "error",
  94. // do not allow importing of implicit dependencies.
  95. "import/no-extraneous-dependencies": ["error", {
  96. // do not allow importing of devDependencies.
  97. "devDependencies": false
  98. }],
  99. // do not allow node-fetch in backend code
  100. "no-restricted-syntax": [
  101. "error",
  102. {
  103. "selector": "CallExpression[callee.name='require'] > .arguments[value='node-fetch']",
  104. "message": "Requiring node-fetch is not allowed in production services, please use fetch-utils."
  105. }
  106. ]
  107. }
  108. },
  109. {
  110. // Cypress specific rules
  111. "files": ["cypress/**/*.{js,ts,tsx}", "**/test/frontend/**/*.spec.{js,ts,tsx}"],
  112. "extends": [
  113. "plugin:cypress/recommended"
  114. ]
  115. },
  116. {
  117. // React component specific rules
  118. //
  119. "files": ["**/frontend/js/**/components/**/*.{js,ts,tsx}", "**/frontend/js/**/hooks/**/*.{js,ts,tsx}"],
  120. "rules": {
  121. // https://astexplorer.net/
  122. "no-restricted-syntax": [
  123. "error",
  124. // prohibit direct calls to methods of window.location
  125. {
  126. "selector": "CallExpression[callee.object.object.name='window'][callee.object.property.name='location']",
  127. "message": "Modify location via useLocation instead of calling window.location methods directly"
  128. },
  129. // prohibit assignment to window.location
  130. {
  131. "selector": "AssignmentExpression[left.object.name='window'][left.property.name='location']",
  132. "message": "Modify location via useLocation instead of calling window.location methods directly"
  133. },
  134. // prohibit assignment to window.location.href
  135. {
  136. "selector": "AssignmentExpression[left.object.object.name='window'][left.object.property.name='location'][left.property.name='href']",
  137. "message": "Modify location via useLocation instead of calling window.location methods directly"
  138. },
  139. // prohibit using lookbehinds due to incidents with Safari simply crashing when the script is parsed
  140. {
  141. "selector": "Literal[regex.pattern=/\\(\\?<[!=]/]",
  142. "message": "Lookbehind is not supported in older Safari versions."
  143. }
  144. ]
  145. }
  146. },
  147. {
  148. // Frontend specific rules
  149. "files": ["**/frontend/js/**/*.{js,ts,tsx}", "**/frontend/stories/**/*.{js,ts,tsx}", "**/*.stories.{js,ts,tsx}", "**/test/frontend/**/*.{js,ts,tsx}", "**/test/frontend/components/**/*.spec.{js,ts,tsx}"],
  150. "env": {
  151. "browser": true
  152. },
  153. "parserOptions": {
  154. "sourceType": "module",
  155. "ecmaFeatures": {
  156. "jsx": true
  157. }
  158. },
  159. "plugins": [
  160. "jsx-a11y"
  161. ],
  162. "extends": [
  163. "plugin:react/recommended",
  164. "plugin:react-hooks/recommended",
  165. "plugin:jsx-a11y/recommended",
  166. "standard-jsx",
  167. "prettier"
  168. ],
  169. "globals": {
  170. "__webpack_public_path__": true,
  171. "$": true,
  172. "angular": true,
  173. "ace": true,
  174. "ga": true,
  175. "sl_console": true,
  176. "sl_debugging": true,
  177. // Injected in layout.pug
  178. "user_id": true,
  179. "ExposedSettings": true
  180. },
  181. "rules": {
  182. // TODO: remove once https://github.com/standard/eslint-config-standard-react/issues/68 (support eslint@8) is fixed.
  183. // START: inline standard-react rules
  184. // "react/jsx-no-bind": ["error", {
  185. // "allowArrowFunctions": true,
  186. // "allowBind": false,
  187. // "ignoreRefs": true
  188. // },],
  189. "react/no-did-update-set-state": "error",
  190. "react/no-unused-prop-types": "error",
  191. "react/prop-types": "error",
  192. // "react/react-in-jsx-scope": "error",
  193. // END: inline standard-react rules
  194. "react/no-unknown-property": ["error", {
  195. "ignore": ["dnd-container", "dropdown-toggle"]
  196. }],
  197. "react/jsx-no-target-blank": ["error", {
  198. "allowReferrer": true
  199. }],
  200. // Prevent usage of legacy string refs
  201. "react/no-string-refs": "error",
  202. // Prevent curly braces around strings (as they're unnecessary)
  203. "react/jsx-curly-brace-presence": ["error", {
  204. "props": "never",
  205. "children": "never"
  206. }],
  207. // Don't import React for JSX; the JSX runtime is added by a Babel plugin
  208. "react/react-in-jsx-scope": "off",
  209. "react/jsx-uses-react": "off",
  210. // Allow functions as JSX props
  211. "react/jsx-no-bind": "off", // TODO: fix occurrences and re-enable this
  212. // Fix conflict between prettier & standard by overriding to prefer
  213. // double quotes
  214. "jsx-quotes": ["error", "prefer-double"],
  215. // Override weird behaviour of jsx-a11y label-has-for (says labels must be
  216. // nested *and* have for/id attributes)
  217. "jsx-a11y/label-has-for": [
  218. "error",
  219. {
  220. "required": {
  221. "some": [
  222. "nesting",
  223. "id"
  224. ]
  225. }
  226. }
  227. ]
  228. }
  229. },
  230. // React + TypeScript-specific rules
  231. {
  232. "files": ["**/*.tsx"],
  233. "rules": {
  234. "react/prop-types": "off",
  235. "no-undef": "off"
  236. }
  237. },
  238. // TypeScript-specific rules
  239. {
  240. "files": ["**/*.ts"],
  241. "rules": {
  242. "no-undef": "off"
  243. }
  244. },
  245. {
  246. "files": ["scripts/ukamf/*.js"],
  247. "rules": {
  248. // Do not allow importing of any dependencies unless specified in either
  249. // - web/package.json
  250. // - web/scripts/ukamf/package.json
  251. "import/no-extraneous-dependencies": ["error", {"packageDir": [".", "scripts/ukamf"]}]
  252. }
  253. },
  254. {
  255. "files": ["scripts/learn/checkSanitize/*.js"],
  256. "rules": {
  257. // The checkSanitize script is used in the dev-env only.
  258. "import/no-extraneous-dependencies": ["error", {
  259. "devDependencies": true,
  260. "packageDir": [".", "../../"]
  261. }]
  262. }
  263. }
  264. ]
  265. }