forked from meganz/webclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eslint-local-rules.js
249 lines (226 loc) · 9.67 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
'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:
return null;
}
}
}
function xpath(node, path) {
path = String(path).split('/');
var i = 0;
var l = path.length;
while (node && l > i) {
node = node[path[i++]];
}
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.'
);
}
}
}
},
};
}
},
'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') {
if (node.object.name === "$" && node.property.name === "dialog") {
context.report(node,
'Overwriting $.dialog is discouraged, handling of' +
' new dialogs should be achieved through M.safeShowDialog(), see MR!1419');
}
}
},
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.');
}
}
};
}
},
'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 + ')');
}
}
};
}
},
};