PR-URL: https://github.com/nodejs/node/pull/19701 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
35 lines
992 B
JavaScript
35 lines
992 B
JavaScript
'use strict';
|
|
const BUFFER_REQUIRE = 'const { Buffer } = require(\'buffer\');';
|
|
|
|
module.exports = function(context) {
|
|
|
|
function flagIt(reference) {
|
|
const msg = `Use ${BUFFER_REQUIRE} 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}\n`);
|
|
}
|
|
});
|
|
}
|
|
|
|
return {
|
|
'Program:exit': function() {
|
|
const globalScope = context.getScope();
|
|
const variable = globalScope.set.get('Buffer');
|
|
if (variable) {
|
|
variable.references.forEach(flagIt);
|
|
}
|
|
}
|
|
};
|
|
};
|