blob: bc49ff6c8746ed6154e78af06c05397444906038 (
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
|
'use strict';
// This rule makes sure that no Globals are going to be used in /lib.
// That could otherwise result in problems with the repl.
module.exports = function(context) {
function flagIt(msg, fix) {
return (reference) => {
context.report({
node: reference.identifier,
message: msg,
fix: (fixer) => {
const sourceCode = context.getSourceCode();
const useStrict = /'use strict';\n\n?/g;
const hasUseStrict = !!useStrict.exec(sourceCode.text);
const firstLOC = sourceCode.ast.range[0];
const rangeNeedle = hasUseStrict ? useStrict.lastIndex : firstLOC;
return fixer.insertTextBeforeRange([rangeNeedle], `${fix}\n`);
}
});
};
}
return {
'Program:exit': function() {
const globalScope = context.getScope();
let variable = globalScope.set.get('Buffer');
if (variable) {
const fix = "const { Buffer } = require('buffer');";
const msg = `Use ${fix} at the beginning of this file`;
variable.references.forEach(flagIt(msg, fix));
}
variable = globalScope.set.get('URL');
if (variable) {
const fix = "const { URL } = require('url');";
const msg = `Use ${fix} at the beginning of this file`;
variable.references.forEach(flagIt(msg, fix));
}
variable = globalScope.set.get('URLSearchParams');
if (variable) {
const fix = "const { URLSearchParams } = require('url');";
const msg = `Use ${fix} at the beginning of this file`;
variable.references.forEach(flagIt(msg, fix));
}
}
};
};
|