-
Notifications
You must be signed in to change notification settings - Fork 308
/
eslint-local-rules.js
401 lines (353 loc) · 15.6 KB
/
eslint-local-rules.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
'use strict';
function traverse(node) {
while (node) {
switch (node.type) {
case 'CallExpression':
node = node.callee;
break;
case 'MemberExpression':
node = node.object;
break;
case 'Identifier':
return node;
default:
node = null;
break;
}
}
return false;
}
function xpath(node, path) {
path = String(path).split('/');
var i = 0;
var l = path.length;
while (node && l > i) {
const loc = path[i++].split('[');
if ((node = node[loc[0]]) && loc.length > 1) {
const [prop, value] = loc[1].slice(0, -1).split('=');
if (node[prop] !== value) {
node = null;
break;
}
}
}
return node || false;
}
// Returns true if the function call node is attached to a jQuery element set.
function isjQuery(node) {
const id = traverse(node);
return id && id.name.startsWith('$');
}
function dump(what) {
if (typeof what !== 'string') {
what = require('util').inspect(what);
}
process.stderr.write(what + '\n');
}
const jQueryEVh = {'on': 1, 'off': 1, 'bind': 1, 'unbind': 1, 'one': 1, 'trigger': 1, 'rebind': 1};
module.exports = {
'open': {
meta: {
docs: {
description: 'Ensure safe window.open() calls.',
category: 'possible-errors',
recommended: true
},
schema: []
},
create(context) {
return {
Identifier(node) {
if (node.name === 'open') {
const parent = xpath(node, 'parent/type');
const args = 'MemberExpression' === parent ? xpath(node, 'parent/parent/arguments')
: parent === 'CallExpression' && xpath(node, 'parent/arguments');
if (args && (args.length < 3
|| !String(args[2].value).includes('noopener')
|| !String(args[2].value).includes('noreferrer'))) {
let a0 = args[0] || false;
if (a0.type === 'BinaryExpression') {
do {
a0 = a0.left || false;
}
while (a0.type === 'BinaryExpression');
}
if (a0.type === 'CallExpression') {
a0 = a0.callee.name;
}
else if (a0.type === 'Literal') {
a0 = a0.value;
}
if (!/^(?:GET|POST|https:\/\/mega\.(?:nz|io)|get(?:App)?BaseUrl)\b/.test(a0)) {
context.report(node,
'If this is a window.open() call, it must be explicitly invoked with noopener|noreferrer.'
);
}
}
}
},
};
}
},
'good-boy': {
meta: {
docs: {
description: 'Eslint rules for some safe practices.',
category: 'possible-errors',
recommended: true
},
schema: []
},
create(context) {
return {
FunctionExpression(node) {
if (node.id && node.id.name === 'populate_l') {
const escaped = new Set();
const unescaped = new Map();
const {body = false} = node.body;
for (let i = body.length; i--;) {
let {left, right} = xpath(body[i], 'expression[type=AssignmentExpression]');
if ((left = traverse(left)).name === 'l') {
switch ((right = traverse(right)).name) {
case 'l': {
const p = xpath(right, 'parent/property');
unescaped.set(right, p.name || p.value);
break;
}
case 'escapeHTML': {
const p = xpath(left, 'parent/property');
escaped.add(p.name || p.value);
const [a0] = xpath(right, 'parent/arguments');
if (a0.type === 'CallExpression' && traverse(a0).name === 'l') {
context.report(a0,
'This will yield a TypeError if the locale string becomes ' +
'unavailable, wrap it in a template-literal if you do *really* ' +
'have to mutate it prior to the escapeHTML() call.');
}
break;
}
default:
if (right.type === 'Identifier') {
context.report(right, `Missing call to escapeHTML(${right.name}*)`);
}
}
}
}
for (const [node, value] of unescaped) {
if (!escaped.has(value)) {
context.report(node, `Missing call to escapeHTML(l['${value}'])`);
}
}
}
},
};
}
},
'hints': {
meta: {
docs: {
description: 'ESLint rule for some miscellaneous stuff.',
category: 'possible-errors',
recommended: true
},
schema: []
},
create(context) {
return {
MemberExpression(node) {
if (node.parent.type === 'AssignmentExpression') {
const {object: {name: o}, property: {name: p}} = node;
if (o === "$" && p === "dialog") {
context.report(node,
'Overwriting $.dialog is discouraged, handling of' +
' new dialogs should be achieved through M.safeShowDialog(), see MR!1419');
}
else if ((o === 'location' && p === 'href')
|| p === "location" && (o === "window" || o === 'self')) {
context.report(node,
'Assigning to (self|window).location.* will not work over our browser extension.');
}
}
},
NewExpression(node) {
if (node.callee.name === 'MegaPromise' && !node.arguments.length && node.parent.type !== 'CallExpression') {
context.report(node,
'New code should use the native Promise implementation instead of MegaPromise, ' +
'or provide an executor to the MegaPromise constructor. ' +
'The promisify() helper function is worth looking into as well.');
}
},
Identifier(node) {
// Enforce the use of toArray.apply(null, arguments)
if (node.name === "arguments" && xpath(node, 'parent/callee/property/name') !== 'apply') {
// do not complain about safe property access (i.e. length, or by idx)
if (xpath(node, 'parent/property/type') === false) {
context.report(node, 'The `arguments` object must not be passed or leaked anywhere.');
}
}
},
TryStatement(node) {
context.report(node,
'The use of try/catch statements can impact performance, they must be isolated ' +
'to a minimal function so that the main code is not affected. ' +
'I.e. consider using our tryCatch() helper.');
}
};
}
},
'misc-warnings': {
meta: {
docs: {
description: 'ESLint rule for some miscellaneous stuff.',
category: 'enhancements',
recommended: true
},
schema: []
},
create(context) {
return {
MemberExpression(node) {
const obj = node.object.name;
const prop = node.property.name;
if (prop === 'forEach') {
context.report(node, 'Prefer for() loops instead of Array.forEach');
}
else if (obj === 'localStorage' && !/^test|[Dd]ebug/.test(prop)) {
context.report(node, 'Do not abuse localStorage, ' +
'consider using sessionStorage or M.setPersistentData() instead.');
}
}
};
}
},
'classes': {
meta: {
docs: {
description: 'ESLint rules for es6+ classes and related.',
category: 'suggestion',
recommended: 'strict'
},
schema: []
},
create(context) {
return {
ClassBody(node) {
// xxx: Based on typescript-eslint/no-extraneous-class.ts
const {parent, body} = node;
if (parent && !parent.superClass) {
if (body.length === 0) {
context.report(node, 'Unexpected empty class.');
}
else {
let onlyStatic = true;
let onlyConstructor = true;
for (const prop of body) {
if (prop.kind === 'constructor') {
if (prop.value.params.length) {
onlyStatic = false;
onlyConstructor = false;
}
}
else {
onlyConstructor = false;
if ('static' in prop && !prop.static) {
onlyStatic = false;
}
}
if (!(onlyStatic || onlyConstructor)) {
break;
}
}
if (onlyConstructor) {
context.report(node, 'Unexpected class with only a constructor.');
}
if (onlyStatic) {
context.report(node, 'Unexpected class with only static properties.');
}
}
}
},
VariableDeclaration(node) {
// xxx: like no-implicit-globals, but only for const/let (aka, lexicalBindings)
if (node.kind === "const" || node.kind === "let") {
if (node.parent && node.parent.type === 'Program') {
context.report(node, `Unexpected globally-scoped '${node.kind}' usage.`);
}
}
}
};
}
},
'jquery-replacements': {
meta: {
docs: {
description: 'ESLint rule to disallow and/or replace jQuery methods',
category: 'possible-errors',
recommended: true
},
schema: []
},
create(context) {
return {
CallExpression(node) {
if (node.callee.type === 'MemberExpression') {
const methods = ['html', 'append', 'prepend'];
const prop = node.callee.property.name;
if (methods.includes(prop) && isjQuery(node)) {
let alt = 'safe' + (prop === 'html' ? 'HTML' : (prop[0].toUpperCase() + prop.substr(1)));
let msg = `Please do use ${alt} instead of jQuery.${prop}`;
if (prop === 'html' && !node.arguments.length) {
msg = 'jQuery.html() is a discouraged method to deal with the DOM...';
}
context.report(node, msg);
}
else if (jQueryEVh[prop] && isjQuery(node)) {
if (prop === 'on') {
context.report(node, 'Prefer $.rebind to $.on and $.off + $.on');
}
const arg0 = node.arguments[0] || false;
if (arg0.type === 'Literal' && String(arg0.value).indexOf(',') !== -1) {
context.report(node, 'Unexpected comma, that is not a valid separator.');
}
}
else if ((prop === 'show' || prop === 'hide') && isjQuery(node)) {
let rep = prop === 'show' ? 'removeClass' : 'addClass';
context.report(node, `jQuery.${prop}() should be replaced with jQuery.${rep}('hidden')`);
}
}
}
};
}
},
'jquery-scopes': {
meta: {
docs: {
description: 'ESLint rule to suggest enhancements to jQuery namespaces/scopes',
category: 'possible-enhancements',
recommended: false
},
schema: []
},
create(context) {
return {
CallExpression(node) {
const arg0 = node.arguments[0];
if (node.callee.type === 'MemberExpression') {
const methods = ['unbind', 'rebind', 'off', 'on'];
const prop = node.callee.property.name;
if (methods.includes(prop) && isjQuery(node)
&& arg0 && arg0.type === "Literal" && arg0.value && arg0.value.indexOf(".") === -1) {
context.report(node,
'Found "' + prop + '" call, but the event name was not containing a ' +
'namespace, which may cause accidental removal of other event handlers. ' +
'Please add namespace to ' + node.arguments[0].raw);
}
}
else if (isjQuery(node) && node.arguments.length === 1 && arg0.type === "Literal") {
context.report(node, 'Found unscoped/global DOM query: $(' + node.arguments[0].raw + ')');
}
}
};
}
},
};