2015-06-11 19:08:25 +02:00
|
|
|
'use strict';
|
2017-11-20 02:22:28 +01:00
|
|
|
const BUFFER_REQUIRE = 'const { Buffer } = require(\'buffer\');\n';
|
2015-06-11 19:08:25 +02:00
|
|
|
|
|
|
|
module.exports = function(context) {
|
2017-11-20 02:22:28 +01:00
|
|
|
|
2016-02-13 22:57:34 +01:00
|
|
|
function flagIt(reference) {
|
|
|
|
const msg = 'Use const Buffer = require(\'buffer\').Buffer; ' +
|
|
|
|
'at the beginning of this file';
|
2017-11-20 02:22:28 +01:00
|
|
|
|
|
|
|
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], BUFFER_REQUIRE);
|
|
|
|
}
|
|
|
|
});
|
2016-02-13 22:57:34 +01:00
|
|
|
}
|
|
|
|
|
2015-06-11 19:08:25 +02:00
|
|
|
return {
|
|
|
|
'Program:exit': function() {
|
2016-02-13 22:57:34 +01:00
|
|
|
const globalScope = context.getScope();
|
|
|
|
const variable = globalScope.set.get('Buffer');
|
|
|
|
if (variable) {
|
|
|
|
variable.references.forEach(flagIt);
|
|
|
|
}
|
2015-06-11 19:08:25 +02:00
|
|
|
}
|
2015-10-05 23:59:36 +02:00
|
|
|
};
|
|
|
|
};
|