summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-parameters.js
blob: 4fd07344653607f6878651defc59735c051880c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * @fileoverview Reject common XPCOM methods called with useless optional
 *               parameters, or non-existent parameters.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

"use strict";

// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------

module.exports = {
  meta: {
    type: "suggestion",
    fixable: "code",
  },
  create(context) {
    function getRangeAfterArgToEnd(argNumber, args) {
      let sourceCode = context.getSourceCode();
      return [
        sourceCode.getTokenAfter(args[argNumber]).range[0],
        args[args.length - 1].range[1],
      ];
    }

    // ---------------------------------------------------------------------------
    // Public
    //  --------------------------------------------------------------------------

    return {
      CallExpression(node) {
        let callee = node.callee;
        if (
          callee.type !== "MemberExpression" ||
          callee.property.type !== "Identifier"
        ) {
          return;
        }

        let isFalse = arg => arg.type === "Literal" && arg.value === false;
        let isFalsy = arg => arg.type === "Literal" && !arg.value;
        let isBool = arg =>
          arg.type === "Literal" && (arg.value === false || arg.value === true);
        let name = callee.property.name;
        let args = node.arguments;

        if (
          ["addEventListener", "removeEventListener", "addObserver"].includes(
            name
          ) &&
          args.length === 3 &&
          isFalse(args[2])
        ) {
          context.report({
            node,
            fix: fixer => {
              return fixer.removeRange(getRangeAfterArgToEnd(1, args));
            },
            message: `${name}'s third parameter can be omitted when it's false.`,
          });
        }

        if (name === "clearUserPref" && args.length > 1) {
          context.report({
            node,
            fix: fixer => {
              return fixer.removeRange(getRangeAfterArgToEnd(0, args));
            },
            message: `${name} takes only 1 parameter.`,
          });
        }

        if (name === "removeObserver" && args.length === 3 && isBool(args[2])) {
          context.report({
            node,
            fix: fixer => {
              return fixer.removeRange(getRangeAfterArgToEnd(1, args));
            },
            message: "removeObserver only takes 2 parameters.",
          });
        }

        if (name === "appendElement" && args.length === 2 && isFalse(args[1])) {
          context.report({
            node,
            fix: fixer => {
              return fixer.removeRange(getRangeAfterArgToEnd(0, args));
            },
            message: `${name}'s second parameter can be omitted when it's false.`,
          });
        }

        if (
          name === "notifyObservers" &&
          args.length === 3 &&
          isFalsy(args[2])
        ) {
          context.report({
            node,
            fix: fixer => {
              return fixer.removeRange(getRangeAfterArgToEnd(1, args));
            },
            message: `${name}'s third parameter can be omitted.`,
          });
        }

        if (
          name === "getComputedStyle" &&
          args.length === 2 &&
          isFalsy(args[1])
        ) {
          context.report({
            node,
            fix: fixer => {
              return fixer.removeRange(getRangeAfterArgToEnd(0, args));
            },
            message: "getComputedStyle's second parameter can be omitted.",
          });
        }

        if (
          name === "newURI" &&
          args.length > 1 &&
          isFalsy(args[args.length - 1])
        ) {
          context.report({
            node,
            fix: fixer => {
              if (args.length > 2 && isFalsy(args[args.length - 2])) {
                return fixer.removeRange(getRangeAfterArgToEnd(0, args));
              }

              return fixer.removeRange(
                getRangeAfterArgToEnd(args.length - 2, args)
              );
            },
            message: "newURI's last parameters are optional.",
          });
        }
      },
    };
  },
};