recommended.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. /**
  6. * The configuration is based on eslint:recommended config. The details for all
  7. * the ESLint rules, and which ones are in the recommended configuration can
  8. * be found here:
  9. *
  10. * https://eslint.org/docs/rules/
  11. */
  12. module.exports = {
  13. env: {
  14. browser: true,
  15. es2021: true,
  16. "mozilla/privileged": true,
  17. "mozilla/specific": true,
  18. },
  19. extends: ["eslint:recommended", "plugin:prettier/recommended"],
  20. overrides: [
  21. {
  22. // System mjs files and jsm files are not loaded in the browser scope,
  23. // so we turn that off for those. Though we do have our own special
  24. // environment for them.
  25. env: {
  26. browser: false,
  27. "mozilla/jsm": true,
  28. },
  29. files: ["**/*.sys.mjs", "**/*.jsm", "**/*.jsm.js"],
  30. rules: {
  31. "mozilla/lazy-getter-object-name": "error",
  32. "mozilla/reject-eager-module-in-lazy-getter": "error",
  33. "mozilla/reject-global-this": "error",
  34. "mozilla/reject-globalThis-modification": "error",
  35. // For all system modules, we expect no properties to need importing,
  36. // hence reject everything.
  37. "mozilla/reject-importGlobalProperties": ["error", "everything"],
  38. "mozilla/reject-mixing-eager-and-lazy": "error",
  39. "mozilla/reject-top-level-await": "error",
  40. // TODO: Bug 1575506 turn `builtinGlobals` on here.
  41. // We can enable builtinGlobals for jsms due to their scopes.
  42. "no-redeclare": ["error", { builtinGlobals: false }],
  43. },
  44. },
  45. {
  46. files: ["**/*.mjs", "**/*.jsm"],
  47. rules: {
  48. // Modules are far easier to check for no-unused-vars on a global scope,
  49. // than our content files. Hence we turn that on here.
  50. "no-unused-vars": [
  51. "error",
  52. {
  53. args: "none",
  54. vars: "all",
  55. },
  56. ],
  57. },
  58. },
  59. {
  60. files: ["**/*.sys.mjs"],
  61. rules: {
  62. "mozilla/use-static-import": "error",
  63. },
  64. },
  65. {
  66. excludedFiles: ["**/*.sys.mjs"],
  67. files: ["**/*.mjs"],
  68. rules: {
  69. "mozilla/reject-import-system-module-from-non-system": "error",
  70. "mozilla/reject-lazy-imports-into-globals": "error",
  71. },
  72. },
  73. {
  74. files: ["**/*.mjs"],
  75. rules: {
  76. "mozilla/use-static-import": "error",
  77. // This rule defaults to not allowing "use strict" in module files since
  78. // they are always loaded in strict mode.
  79. strict: "error",
  80. },
  81. },
  82. {
  83. files: ["**/*.jsm", "**/*.jsm.js"],
  84. rules: {
  85. "mozilla/mark-exported-symbols-as-used": "error",
  86. },
  87. },
  88. {
  89. env: {
  90. browser: false,
  91. "mozilla/privileged": false,
  92. "mozilla/sjs": true,
  93. },
  94. files: ["**/*.sjs"],
  95. rules: {
  96. // TODO Bug 1501127: sjs files have their own sandbox, and do not inherit
  97. // the Window backstage pass directly. Turn this rule off for sjs files for
  98. // now until we develop a solution.
  99. "mozilla/reject-importGlobalProperties": "off",
  100. },
  101. },
  102. ],
  103. parserOptions: {
  104. ecmaVersion: 12,
  105. },
  106. // When adding items to this file please check for effects on sub-directories.
  107. plugins: ["html", "fetch-options", "no-unsanitized"],
  108. // When adding items to this file please check for effects on all of toolkit
  109. // and browser
  110. rules: {
  111. // Warn about cyclomatic complexity in functions.
  112. // XXX Get this down to 20?
  113. complexity: ["error", 34],
  114. // Functions must always return something or nothing
  115. "consistent-return": "error",
  116. // XXX This rule line should be removed to enable it. See bug 1487642.
  117. // Require super() calls in constructors
  118. "constructor-super": "off",
  119. // Require braces around blocks that start a new line
  120. curly: ["error", "all"],
  121. // Encourage the use of dot notation whenever possible.
  122. "dot-notation": "error",
  123. // XXX This rule should be enabled, see Bug 1557040
  124. // No credentials submitted with fetch calls
  125. "fetch-options/no-fetch-credentials": "off",
  126. // XXX This rule line should be removed to enable it. See bug 1487642.
  127. // Enforce return statements in getters
  128. "getter-return": "off",
  129. // Don't enforce the maximum depth that blocks can be nested. The complexity
  130. // rule is a better rule to check this.
  131. "max-depth": "off",
  132. // Maximum depth callbacks can be nested.
  133. "max-nested-callbacks": ["error", 10],
  134. "mozilla/avoid-removeChild": "error",
  135. "mozilla/consistent-if-bracing": "error",
  136. "mozilla/import-browser-window-globals": "error",
  137. "mozilla/import-globals": "error",
  138. "mozilla/no-compare-against-boolean-literals": "error",
  139. "mozilla/no-cu-reportError": "error",
  140. "mozilla/no-define-cc-etc": "error",
  141. "mozilla/no-throw-cr-literal": "error",
  142. "mozilla/no-useless-parameters": "error",
  143. "mozilla/no-useless-removeEventListener": "error",
  144. "mozilla/prefer-boolean-length-check": "error",
  145. "mozilla/prefer-formatValues": "error",
  146. "mozilla/reject-addtask-only": "error",
  147. "mozilla/reject-chromeutils-import-params": "error",
  148. "mozilla/reject-importGlobalProperties": ["error", "allownonwebidl"],
  149. "mozilla/reject-multiple-getters-calls": "error",
  150. "mozilla/reject-osfile": "warn",
  151. "mozilla/reject-scriptableunicodeconverter": "warn",
  152. "mozilla/rejects-requires-await": "error",
  153. "mozilla/use-cc-etc": "error",
  154. "mozilla/use-chromeutils-generateqi": "error",
  155. "mozilla/use-chromeutils-import": "error",
  156. "mozilla/use-default-preference-values": "error",
  157. "mozilla/use-includes-instead-of-indexOf": "error",
  158. "mozilla/use-isInstance": "error",
  159. "mozilla/use-ownerGlobal": "error",
  160. "mozilla/use-returnValue": "error",
  161. "mozilla/use-services": "error",
  162. "mozilla/valid-lazy": "error",
  163. "mozilla/valid-services": "error",
  164. // Use [] instead of Array()
  165. "no-array-constructor": "error",
  166. // Disallow use of arguments.caller or arguments.callee.
  167. "no-caller": "error",
  168. // XXX Bug 1487642 - decide if we want to enable this or not.
  169. // Disallow lexical declarations in case clauses
  170. "no-case-declarations": "off",
  171. // XXX Bug 1487642 - decide if we want to enable this or not.
  172. // Disallow the use of console
  173. "no-console": "off",
  174. // Disallows expressions where the operation doesn't affect the value.
  175. "no-constant-binary-expression": "error",
  176. // XXX Bug 1487642 - decide if we want to enable this or not.
  177. // Disallow constant expressions in conditions
  178. "no-constant-condition": "off",
  179. // No duplicate keys in object declarations
  180. "no-dupe-keys": "error",
  181. // If an if block ends with a return no need for an else block
  182. "no-else-return": "error",
  183. // No empty statements
  184. "no-empty": ["error", { allowEmptyCatch: true }],
  185. // Disallow eval and setInteral/setTimeout with strings
  186. "no-eval": "error",
  187. // Disallow unnecessary calls to .bind()
  188. "no-extra-bind": "error",
  189. // Disallow fallthrough of case statements
  190. "no-fallthrough": [
  191. "error",
  192. {
  193. // The eslint rule doesn't allow for case-insensitive regex option.
  194. // The following pattern allows for a dash between "fall through" as
  195. // well as alternate spelling of "fall thru". The pattern also allows
  196. // for an optional "s" at the end of "fall" ("falls through").
  197. commentPattern:
  198. "[Ff][Aa][Ll][Ll][Ss]?[\\s-]?([Tt][Hh][Rr][Oo][Uu][Gg][Hh]|[Tt][Hh][Rr][Uu])",
  199. },
  200. ],
  201. // Disallow assignments to native objects or read-only global variables
  202. "no-global-assign": "error",
  203. // Disallow eval and setInteral/setTimeout with strings
  204. "no-implied-eval": "error",
  205. // This has been superseded since we're using ES6.
  206. // Disallow variable or function declarations in nested blocks
  207. "no-inner-declarations": "off",
  208. // Disallow the use of the __iterator__ property
  209. "no-iterator": "error",
  210. // No labels
  211. "no-labels": "error",
  212. // Disallow unnecessary nested blocks
  213. "no-lone-blocks": "error",
  214. // No single if block inside an else block
  215. "no-lonely-if": "error",
  216. // Disallow the use of number literals that immediately lose precision at runtime when converted to JS Number
  217. "no-loss-of-precision": "error",
  218. // Nested ternary statements are confusing
  219. "no-nested-ternary": "error",
  220. // Use {} instead of new Object()
  221. "no-new-object": "error",
  222. // Disallow use of new wrappers
  223. "no-new-wrappers": "error",
  224. // We don't want this, see bug 1551829
  225. "no-prototype-builtins": "off",
  226. // Disable builtinGlobals for no-redeclare as this conflicts with our
  227. // globals declarations especially for browser window.
  228. "no-redeclare": ["error", { builtinGlobals: false }],
  229. // Disallow use of event global.
  230. "no-restricted-globals": ["error", "event"],
  231. // Disallows unnecessary `return await ...`.
  232. "no-return-await": "error",
  233. // No unnecessary comparisons
  234. "no-self-compare": "error",
  235. // No comma sequenced statements
  236. "no-sequences": "error",
  237. // No declaring variables from an outer scope
  238. // "no-shadow": "error",
  239. // No declaring variables that hide things like arguments
  240. "no-shadow-restricted-names": "error",
  241. // Disallow throwing literals (eg. throw "error" instead of
  242. // throw new Error("error")).
  243. "no-throw-literal": "error",
  244. // Disallow the use of Boolean literals in conditional expressions.
  245. "no-unneeded-ternary": "error",
  246. // No unsanitized use of innerHTML=, document.write() etc.
  247. // cf. https://github.com/mozilla/eslint-plugin-no-unsanitized#rule-details
  248. "no-unsanitized/method": "error",
  249. "no-unsanitized/property": "error",
  250. // No declaring variables that are never used
  251. "no-unused-vars": [
  252. "error",
  253. {
  254. args: "none",
  255. vars: "local",
  256. },
  257. ],
  258. // No using variables before defined
  259. // "no-use-before-define": ["error", "nofunc"],
  260. // Disallow unnecessary .call() and .apply()
  261. "no-useless-call": "error",
  262. // Don't concatenate string literals together (unless they span multiple
  263. // lines)
  264. "no-useless-concat": "error",
  265. // XXX Bug 1487642 - decide if we want to enable this or not.
  266. // Disallow unnecessary escape characters
  267. "no-useless-escape": "off",
  268. // Disallow redundant return statements
  269. "no-useless-return": "error",
  270. // No using with
  271. "no-with": "error",
  272. // Require object-literal shorthand with ES6 method syntax
  273. "object-shorthand": ["error", "always", { avoidQuotes: true }],
  274. // This generates too many false positives that are not easy to work around,
  275. // and false positives seem to be inherent in the rule.
  276. "require-atomic-updates": "off",
  277. // XXX Bug 1487642 - decide if we want to enable this or not.
  278. // Require generator functions to contain yield
  279. "require-yield": "off",
  280. },
  281. // To avoid bad interactions of the html plugin with the xml preprocessor in
  282. // eslint-plugin-mozilla, we turn off processing of the html plugin for .xml
  283. // files.
  284. settings: {
  285. "html/xml-extensions": [".xhtml"],
  286. },
  287. };