-
Notifications
You must be signed in to change notification settings - Fork 140
/
utils.js
506 lines (441 loc) 路 13.7 KB
/
utils.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import url from 'url';
import upath from 'upath';
import Jed from 'jed';
import semver from 'semver';
import { oneLine } from 'common-tags';
import osLocale from 'os-locale';
import log from 'logger';
import { PACKAGE_TYPES, LOCAL_PROTOCOLS } from 'const';
/* global nodeRequire, localesRoot */
const SOURCE_MAP_RE = new RegExp(/\/\/[#@]\s(source(?:Mapping)?URL)=\s*(\S+)/);
// Represents an error condition related to a user error (e.g. an invalid
// configuration option passed to the linter class, usually through the
// command line arguments).
//
// In bin/addons-linter instances of this error are recognized through the
// error name property and by default they will be logged on stderr as
// plain error messages and the error stack trace omitted (unless explicitly
// requested by passing --stack as an additional CLI options, useful for
// debugging reasons).
export class AddonsLinterUserError extends Error {
get name() {
return 'AddonsLinterUserError';
}
}
export function errorParamsToUnsupportedVersionRange(errorParams) {
const { min_manifest_version, max_manifest_version } = errorParams || {};
if (min_manifest_version != null || max_manifest_version != null) {
return [
min_manifest_version ? `< ${min_manifest_version}` : undefined,
max_manifest_version ? `> ${max_manifest_version}` : undefined,
]
.filter((e) => e !== undefined)
.join(', ');
}
return '';
}
export function normalizePath(iconPath) {
// Convert the icon path to a URL so we can strip any fragments and resolve
// . and .. automatically. We need an absolute URL to use as a base so we're
// using https://example.com/.
const { pathname } = new URL(iconPath, 'https://example.com/');
// Convert filename to unix path separator (as the ones stored
// into the scanned files map).
return upath.toUnix(decodeURIComponent(pathname).slice(1));
}
/*
* Takes an AST node and returns the root property.
*
* example: foo().bar.baz() will return the AST node for foo.
*/
export function getRootExpression(node) {
let root = node.callee;
// If we encounter a member, grab the parent
if (node.callee.type === 'MemberExpression') {
let parent = node.callee.object;
while (parent.type !== 'Identifier') {
if (parent.callee.type === 'MemberExpression') {
parent = parent.callee.object;
} else {
parent = parent.callee;
}
}
root = parent;
}
return root;
}
/*
* Returns the name of the reference node passed.
*
* example: var foo = document;
* The node for foo will return 'document'
*/
export function getNodeReference(context, node) {
const { variables } = context.getScope();
let scopeVar;
// Just return the value if the node passed in is a reference to a literal.
if (typeof node === 'undefined' || node.type === 'Literal') {
return node;
}
// Finds variable reference in current scope.
for (let i = 0; i < variables.length; i++) {
const variable = variables[i];
if (variable.name === node.name) {
scopeVar = variable;
break;
}
}
if (
scopeVar &&
scopeVar.defs &&
scopeVar.defs[0] &&
scopeVar.defs[0].parent &&
scopeVar.defs[0].parent.parent &&
scopeVar.defs[0].parent.parent.body
) {
// This represents all occurrences of the variable
const occurances = scopeVar.defs[0].parent.parent.body;
let lastAssignment;
if (occurances instanceof Array) {
occurances.forEach((occurance) => {
if (
occurance.type === 'VariableDeclaration' &&
occurance.declarations[0].init !== null
) {
// Get what the name of what it was assigned to or the raw
// value depending on the initalization
lastAssignment = occurance.declarations[0].init;
} else if (
occurance.type === 'ExpressionStatement' &&
occurance.expression.type === 'AssignmentExpression'
) {
// Get the right hand side of the assignment
lastAssignment = occurance.expression.right;
}
});
}
// Return the name of the first definition of the variable which
// corresponds to the node passed in.
if (lastAssignment) {
return lastAssignment;
}
}
// If that variable doesn't exist in scope, then just return the node's
// name.
return node;
}
/*
* Get a variable from a eslint context object if it exists, otherwise
* undefined.
*/
export function getVariable(context, name) {
const { variables } = context.getScope();
let result;
variables.forEach((variable) => {
if (
variable.name === name &&
variable.defs &&
variable.defs[0] &&
variable.defs[0].name &&
variable.defs[0].name.parent
) {
result = variable.defs[0].name.parent.init;
}
});
return result;
}
export function getLocale() {
return osLocale.sync();
}
export function getI18Data(locale) {
let i18ndata = {};
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
i18ndata = nodeRequire(`${localesRoot}/${locale}/messages.js`);
} catch (err) {
log.info('Initialize locales using extract-locales command');
}
return i18ndata;
}
/*
* Gettext utils. Used for translating strings.
*/
export function buildI18nObject(i18nData) {
const _jed = new Jed(i18nData);
return {
jed: _jed,
getI18Data,
_: (str) => {
return _jed.gettext(str);
},
gettext: (str) => {
return _jed.gettext(str);
},
sprintf: (fmt, args) => {
return _jed.sprintf(fmt, args);
},
};
}
export const i18n = buildI18nObject(getI18Data(getLocale()));
/*
* Check the minimum node version is met
*/
export async function checkMinNodeVersion(minVersion, _process = process) {
// eslint-disable-next-line no-param-reassign
minVersion = minVersion || '0.12.0';
if (!semver.gte(_process.version, minVersion)) {
throw new Error(oneLine`Node version must be ${minVersion} or
greater. You are using ${_process.version}.`);
}
}
export function getPackageTypeAsString(numericPackageType) {
const packageKeys = Object.keys(PACKAGE_TYPES);
for (let i = 0; i < packageKeys.length; i++) {
const packageType = packageKeys[i];
if (parseInt(numericPackageType, 10) === PACKAGE_TYPES[packageType]) {
return packageType;
}
}
throw new Error(`Invalid package type constant "${numericPackageType}"`);
}
/*
* Looks through all exported functions and returns only
* "public" *functions* that aren't prefixed with an _
*
* Used for ignoring private functions and constants in rules files.
* Rules can have private functions we don't run; anything that
* starts with an "_" shouldn't be returned.
*
* This exists because we export private functions in rule files
* for testing.
*/
export function ignorePrivateFunctions(list) {
const filteredList = {};
Object.keys(list).forEach((functionName) => {
if (
functionName.startsWith('_') === false &&
typeof list[functionName] === 'function'
) {
filteredList[functionName] = list[functionName];
}
});
return filteredList;
}
/*
* Check a filename to make sure it's valid; used by scanners so we never
* accept new scanners that don't specify which file they're referencing.
*/
export function ensureFilenameExists(filename) {
if (typeof filename !== 'string' || filename.length < 1) {
throw new Error('Filename is required');
}
}
export function isLocalUrl(urlInput) {
const parsedUrl = url.parse(urlInput);
const { protocol, path } = parsedUrl;
// Check protocol is chrome: or resource: if set.
// Details on the chrome protocol are here: https://goo.gl/W52T0Q
// Details on resource protocol are here: https://goo.gl/HHqeJA
if (protocol && !LOCAL_PROTOCOLS.includes(protocol)) {
return false;
}
// Disallow protocol-free remote urls.
if (path.startsWith('//')) {
return false;
}
return true;
}
export function apiToMessage(string) {
return string
.replace(/^extension/, 'ext')
.replace(/\./g, '_')
.toUpperCase()
.substr(0, 25);
}
export function isBrowserNamespace(string) {
return ['browser', 'chrome'].includes(string);
}
export function parseCspPolicy(policy) {
if (!policy) {
return {};
}
// eslint-disable-next-line no-param-reassign
policy = policy.toLowerCase();
const parsedPolicy = {};
const directives = policy.split(';');
directives.forEach((directive) => {
// eslint-disable-next-line no-param-reassign
directive = directive.trim();
const tokens = directive.split(/\s+/);
const name = tokens[0];
if (name) {
parsedPolicy[name] = tokens.slice(1, tokens.length);
}
});
return parsedPolicy;
}
export function getLineAndColumnFromMatch(match) {
const matchedLines = match.input.substr(0, match.index).split('\n');
const matchedColumn = matchedLines.slice(-1)[0].length + 1;
const matchedLine = matchedLines.length;
return { matchedLine, matchedColumn };
}
/**
* Determines if the source text is minified.
* Using the percentage no. of the indented lines from a sample set of lines
* to determine if the js file is minified.
* Inspired by code for the Firefox Developer Toolbar.
*/
export function couldBeMinifiedCode(code) {
// Fast exit if `code` is empty. Could happen in tests, but also in real
// files.
if (!code) {
return false;
}
// If there's a source map reference it's very certainly minified code.
if (SOURCE_MAP_RE.test(code)) {
return true;
}
// Number of lines to look at, taken from the head of the code.
const sampleSize = 30;
// Threshold in percent of indented lines to mark a file as not
// minified.
const indentCountThreshold = 20; // percentage
// Length of a line that looks suspicious of being minified
const hugeLinesLength = 500;
// Number of huge lines to also mark a file as potentially minified
// Hint: Minified AngularJS has 12 lines, jQuery 4
const hugeLinesThreshold = 4;
let lineEndIndex = 0;
let lineStartIndex = 0;
let lines = 1;
let indentCount = 0;
let hugeLinesCount = 0;
// Strip comments.
const normalizedCode = code.replace(/\/\*[\S\s]*?\*\/|\/\/.+/g, '');
while (lines < sampleSize) {
lineEndIndex = normalizedCode.indexOf('\n', lineStartIndex);
if (lineEndIndex === -1) {
break;
}
const currentLine = normalizedCode.slice(lineStartIndex, lineEndIndex);
if (/^\s+/.test(currentLine)) {
indentCount++;
}
if (currentLine.length >= hugeLinesLength) {
hugeLinesCount++;
}
lineStartIndex = lineEndIndex + 1;
lines++;
}
return (
(indentCount / lines) * 100 < indentCountThreshold ||
hugeLinesCount > hugeLinesThreshold
);
}
export function firefoxStrictMinVersion(manifestJson) {
if (
manifestJson.applications &&
manifestJson.applications.gecko &&
manifestJson.applications.gecko.strict_min_version &&
typeof manifestJson.applications.gecko.strict_min_version === 'string'
) {
return parseInt(
manifestJson.applications.gecko.strict_min_version.split('.')[0],
10
);
}
return null;
}
export function basicCompatVersionComparison(versionAdded, minVersion) {
const asNumber = parseInt(versionAdded, 10);
return !Number.isNaN(asNumber) && asNumber > minVersion;
}
/**
* @param {*} supportInfo - bcd support info of a feature
* @returns {string|boolean} The first version number to support the feature
* or a boolean indicating if the feature is supported at all. We do
* not consider any holes in the supported versions, only the first
* stable version is taken into account.
*/
export function firstStableVersion(supportInfo) {
let supportInfoArray = supportInfo;
if (!Array.isArray(supportInfo)) {
supportInfoArray = [supportInfo];
}
return supportInfoArray.reduce((versionAdded, supportEntry) => {
if (
!Object.prototype.hasOwnProperty.call(supportEntry, 'flags') &&
(!versionAdded ||
(supportEntry.version_added &&
!basicCompatVersionComparison(
supportEntry.version_added,
parseInt(versionAdded, 10)
)))
) {
return supportEntry.version_added;
}
return versionAdded;
}, false);
}
export function isCompatible(bcd, path, minVersion, application) {
const steps = path.split('.');
let { api } = bcd.webextensions;
for (const step of steps) {
if (Object.prototype.hasOwnProperty.call(api, step)) {
api = api[step];
} else {
break;
}
}
// API namespace may be undocumented or not implemented, ignore in that case.
if (api.__compat) {
const supportInfo = api.__compat.support[application];
const versionAdded = firstStableVersion(supportInfo);
return !basicCompatVersionComparison(versionAdded, minVersion);
}
return true;
}
export function createCompatibilityRule(
application,
message,
context,
bcd,
hasBrowserApi
) {
const { addonMetadata } = context.settings;
const minVersion =
addonMetadata &&
firefoxStrictMinVersion({
applications: {
gecko: {
strict_min_version: context.settings.addonMetadata.firefoxMinVersion,
},
},
});
if (minVersion) {
return {
MemberExpression(node) {
if (
!node.computed &&
node.object.object &&
isBrowserNamespace(node.object.object.name)
) {
const namespace = node.object.property.name;
const property = node.property.name;
const api = `${namespace}.${property}`;
if (
hasBrowserApi(namespace, property, addonMetadata) &&
!isCompatible(bcd, api, minVersion, application)
) {
context.report(node, message.messageFormat, {
api,
minVersion: addonMetadata.firefoxMinVersion,
});
}
}
},
};
}
return {};
}