/
prefer-add-event-listener.mjs
180 lines (158 loc) · 3.37 KB
/
prefer-add-event-listener.mjs
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
176
177
178
179
180
import outdent from 'outdent';
import {getTester} from './utils/test.mjs';
const {test} = getTester(import.meta);
const excludeFooOptions = [{excludedPackages: ['foo']}];
test.snapshot({
valid: [
'foo.addEventListener(\'click\', () => {})',
'foo.removeEventListener(\'click\', onClick)',
'foo.onclick',
'foo[onclick] = () => {}',
'foo["onclick"] = () => {}',
'foo.onunknown = () => {}',
'foo.setCallBack = () => {console.log(\'foo\')}',
'setCallBack = () => {console.log(\'foo\')}',
'foo.onclick.bar = () => {}',
'foo[\'x\'] = true;',
outdent`
const Koa = require('koa');
const app = new Koa();
app.onerror = () => {};
`,
outdent`
const sax = require('sax');
const parser = sax.parser();
parser.onerror = () => {};
`,
outdent`
import Koa from 'koa';
const app = new Koa();
app.onerror = () => {};
`,
outdent`
import sax from 'sax';
const parser = sax.parser();
parser.onerror = () => {};
`,
outdent`
import {sax as foo} from 'sax';
const parser = foo.parser();
parser.onerror = () => {};
`,
{
code: outdent`
const foo = require('foo');
foo.onerror = () => {};
`,
options: excludeFooOptions,
},
{
code: outdent`
import foo from 'foo';
foo.onclick = () => {};
`,
options: excludeFooOptions,
},
],
invalid: [
'foo.onclick = () => {}',
'foo.onclick = 1',
'foo.bar.onclick = onClick',
'const bar = null; foo.onclick = bar;',
'foo.onkeydown = () => {}',
'foo.ondragend = () => {}',
outdent`
foo.onclick = function (e) {
console.log(e);
}
`,
'foo.onclick = null',
'foo.onclick = undefined',
'window.onbeforeunload = null',
'window.onbeforeunload = undefined',
'window.onbeforeunload = foo',
'window.onbeforeunload = () => \'foo\'',
outdent`
window.onbeforeunload = () => {
return bar;
}
`,
outdent`
window.onbeforeunload = function () {
return 'bar';
}
`,
outdent`
window.onbeforeunload = function () {
return;
}
`,
outdent`
window.onbeforeunload = function () {
(() => {
return 'foo';
})();
}
`,
outdent`
window.onbeforeunload = e => {
console.log(e);
}
`,
outdent`
const foo = require('foo');
foo.onerror = () => {};
`,
outdent`
import foo from 'foo';
foo.onerror = () => {};
`,
outdent`
foo.onerror = () => {};
function bar() {
const koa = require('koa');
koa.onerror = () => {};
}
`,
{
code: outdent`
const Koa = require('koa');
const app = new Koa();
app.onerror = () => {};
`,
options: excludeFooOptions,
},
{
code: outdent`
import {Koa as Foo} from 'koa';
const app = new Foo();
app.onerror = () => {};
`,
options: excludeFooOptions,
},
{
code: outdent`
const sax = require('sax');
const parser = sax.parser();
parser.onerror = () => {};
`,
options: excludeFooOptions,
},
'myWorker.port.onmessage = function(e) {}',
'((foo)).onclick = ((0, listener))',
'window.onload = window.onunload = function() {};',
'window.onunload ??= function() {};',
'window.onunload ||= function() {};',
'window.onunload += function() {};',
],
});
test.typescript({
valid: [],
invalid: [
{
code: '(el as HTMLElement).onmouseenter = onAnchorMouseEnter;',
output: '(el as HTMLElement).addEventListener(\'mouseenter\', onAnchorMouseEnter);',
errors: 1,
},
],
});