0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/tools/eslint-rules/require-buffer.js

36 lines
990 B
JavaScript
Raw Normal View History

'use strict';
const BUFFER_REQUIRE = 'const { Buffer } = require(\'buffer\');\n';
module.exports = function(context) {
function flagIt(reference) {
const msg = 'Use const Buffer = require(\'buffer\').Buffer; ' +
'at the beginning of this file';
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);
}
});
}
return {
'Program:exit': function() {
const globalScope = context.getScope();
const variable = globalScope.set.get('Buffer');
if (variable) {
variable.references.forEach(flagIt);
}
}
};
};