summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/tests/no-useless-parameters.js
blob: 28d4d0d151f8692b207163b70813051ce4bc6292 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

var rule = require("../lib/rules/no-useless-parameters");
var RuleTester = require("eslint").RuleTester;

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: "latest" } });

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

function callError(messageId, data = {}) {
  return [{ messageId, data, type: "CallExpression" }];
}

ruleTester.run("no-useless-parameters", rule, {
  valid: [
    "Services.prefs.clearUserPref('browser.search.custom');",
    "Services.removeObserver('notification name', {});",
    "Services.io.newURI('http://example.com');",
    "Services.io.newURI('http://example.com', 'utf8');",
    "elt.addEventListener('click', handler);",
    "elt.addEventListener('click', handler, true);",
    "elt.addEventListener('click', handler, {once: true});",
    "elt.removeEventListener('click', handler);",
    "elt.removeEventListener('click', handler, true);",
    "Services.obs.addObserver(this, 'topic', true);",
    "Services.obs.addObserver(this, 'topic');",
    "Services.prefs.addObserver('branch', this, true);",
    "Services.prefs.addObserver('branch', this);",
    "array.appendElement(elt);",
    "Services.obs.notifyObservers(obj, 'topic', 'data');",
    "Services.obs.notifyObservers(obj, 'topic');",
    "window.getComputedStyle(elt);",
    "window.getComputedStyle(elt, ':before');",
  ],
  invalid: [
    {
      code: "Services.prefs.clearUserPref('browser.search.custom', false);",
      output: "Services.prefs.clearUserPref('browser.search.custom');",
      errors: callError("onlyTakes", {
        fnName: "clearUserPref",
        params: "1 parameter",
      }),
    },
    {
      code: "Services.prefs.clearUserPref('browser.search.custom',\n   false);",
      output: "Services.prefs.clearUserPref('browser.search.custom');",
      errors: callError("onlyTakes", {
        fnName: "clearUserPref",
        params: "1 parameter",
      }),
    },
    {
      code: "Services.removeObserver('notification name', {}, false);",
      output: "Services.removeObserver('notification name', {});",
      errors: callError("onlyTakes", {
        fnName: "removeObserver",
        params: "2 parameters",
      }),
    },
    {
      code: "Services.removeObserver('notification name', {}, true);",
      output: "Services.removeObserver('notification name', {});",
      errors: callError("onlyTakes", {
        fnName: "removeObserver",
        params: "2 parameters",
      }),
    },
    {
      code: "Services.io.newURI('http://example.com', null, null);",
      output: "Services.io.newURI('http://example.com');",
      errors: callError("newURIParams"),
    },
    {
      code: "Services.io.newURI('http://example.com', 'utf8', null);",
      output: "Services.io.newURI('http://example.com', 'utf8');",
      errors: callError("newURIParams"),
    },
    {
      code: "Services.io.newURI('http://example.com', null);",
      output: "Services.io.newURI('http://example.com');",
      errors: callError("newURIParams"),
    },
    {
      code: "Services.io.newURI('http://example.com', '', '');",
      output: "Services.io.newURI('http://example.com');",
      errors: callError("newURIParams"),
    },
    {
      code: "Services.io.newURI('http://example.com', '');",
      output: "Services.io.newURI('http://example.com');",
      errors: callError("newURIParams"),
    },
    {
      code: "elt.addEventListener('click', handler, false);",
      output: "elt.addEventListener('click', handler);",
      errors: callError("obmittedWhenFalse", {
        fnName: "addEventListener",
        index: "third",
      }),
    },
    {
      code: "elt.removeEventListener('click', handler, false);",
      output: "elt.removeEventListener('click', handler);",
      errors: callError("obmittedWhenFalse", {
        fnName: "removeEventListener",
        index: "third",
      }),
    },
    {
      code: "Services.obs.addObserver(this, 'topic', false);",
      output: "Services.obs.addObserver(this, 'topic');",
      errors: callError("obmittedWhenFalse", {
        fnName: "addObserver",
        index: "third",
      }),
    },
    {
      code: "Services.prefs.addObserver('branch', this, false);",
      output: "Services.prefs.addObserver('branch', this);",
      errors: callError("obmittedWhenFalse", {
        fnName: "addObserver",
        index: "third",
      }),
    },
    {
      code: "array.appendElement(elt, false);",
      output: "array.appendElement(elt);",
      errors: callError("obmittedWhenFalse", {
        fnName: "appendElement",
        index: "second",
      }),
    },
    {
      code: "Services.obs.notifyObservers(obj, 'topic', null);",
      output: "Services.obs.notifyObservers(obj, 'topic');",
      errors: callError("obmittedWhenFalse", {
        fnName: "notifyObservers",
        index: "third",
      }),
    },
    {
      code: "Services.obs.notifyObservers(obj, 'topic', '');",
      output: "Services.obs.notifyObservers(obj, 'topic');",
      errors: callError("obmittedWhenFalse", {
        fnName: "notifyObservers",
        index: "third",
      }),
    },
    {
      code: "window.getComputedStyle(elt, null);",
      output: "window.getComputedStyle(elt);",
      errors: callError("obmittedWhenFalse", {
        fnName: "getComputedStyle",
        index: "second",
      }),
    },
    {
      code: "window.getComputedStyle(elt, '');",
      output: "window.getComputedStyle(elt);",
      errors: callError("obmittedWhenFalse", {
        fnName: "getComputedStyle",
        index: "second",
      }),
    },
  ],
});