no-useless-parameters.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @fileoverview Reject common XPCOM methods called with useless optional
  3. * parameters, or non-existent parameters.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. "use strict";
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. url:
  14. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/no-useless-parameters.html",
  15. },
  16. fixable: "code",
  17. type: "suggestion",
  18. },
  19. create(context) {
  20. function getRangeAfterArgToEnd(argNumber, args) {
  21. let sourceCode = context.getSourceCode();
  22. return [
  23. sourceCode.getTokenAfter(args[argNumber]).range[0],
  24. args[args.length - 1].range[1],
  25. ];
  26. }
  27. return {
  28. CallExpression(node) {
  29. let callee = node.callee;
  30. if (
  31. callee.type !== "MemberExpression" ||
  32. callee.property.type !== "Identifier"
  33. ) {
  34. return;
  35. }
  36. let isFalse = arg => arg.type === "Literal" && arg.value === false;
  37. let isFalsy = arg => arg.type === "Literal" && !arg.value;
  38. let isBool = arg =>
  39. arg.type === "Literal" && (arg.value === false || arg.value === true);
  40. let name = callee.property.name;
  41. let args = node.arguments;
  42. if (
  43. ["addEventListener", "removeEventListener", "addObserver"].includes(
  44. name
  45. ) &&
  46. args.length === 3 &&
  47. isFalse(args[2])
  48. ) {
  49. context.report({
  50. node,
  51. fix: fixer => {
  52. return fixer.removeRange(getRangeAfterArgToEnd(1, args));
  53. },
  54. message: `${name}'s third parameter can be omitted when it's false.`,
  55. });
  56. }
  57. if (name === "clearUserPref" && args.length > 1) {
  58. context.report({
  59. node,
  60. fix: fixer => {
  61. return fixer.removeRange(getRangeAfterArgToEnd(0, args));
  62. },
  63. message: `${name} takes only 1 parameter.`,
  64. });
  65. }
  66. if (name === "removeObserver" && args.length === 3 && isBool(args[2])) {
  67. context.report({
  68. node,
  69. fix: fixer => {
  70. return fixer.removeRange(getRangeAfterArgToEnd(1, args));
  71. },
  72. message: "removeObserver only takes 2 parameters.",
  73. });
  74. }
  75. if (name === "appendElement" && args.length === 2 && isFalse(args[1])) {
  76. context.report({
  77. node,
  78. fix: fixer => {
  79. return fixer.removeRange(getRangeAfterArgToEnd(0, args));
  80. },
  81. message: `${name}'s second parameter can be omitted when it's false.`,
  82. });
  83. }
  84. if (
  85. name === "notifyObservers" &&
  86. args.length === 3 &&
  87. isFalsy(args[2])
  88. ) {
  89. context.report({
  90. node,
  91. fix: fixer => {
  92. return fixer.removeRange(getRangeAfterArgToEnd(1, args));
  93. },
  94. message: `${name}'s third parameter can be omitted.`,
  95. });
  96. }
  97. if (
  98. name === "getComputedStyle" &&
  99. args.length === 2 &&
  100. isFalsy(args[1])
  101. ) {
  102. context.report({
  103. node,
  104. fix: fixer => {
  105. return fixer.removeRange(getRangeAfterArgToEnd(0, args));
  106. },
  107. message: "getComputedStyle's second parameter can be omitted.",
  108. });
  109. }
  110. if (
  111. name === "newURI" &&
  112. args.length > 1 &&
  113. isFalsy(args[args.length - 1])
  114. ) {
  115. context.report({
  116. node,
  117. fix: fixer => {
  118. if (args.length > 2 && isFalsy(args[args.length - 2])) {
  119. return fixer.removeRange(getRangeAfterArgToEnd(0, args));
  120. }
  121. return fixer.removeRange(
  122. getRangeAfterArgToEnd(args.length - 2, args)
  123. );
  124. },
  125. message: "newURI's last parameters are optional.",
  126. });
  127. }
  128. },
  129. };
  130. },
  131. };