You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
873 B

'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');
const inferMethod = arguments_ => {
if (arguments_.length > 0) {
const [firstArgument] = arguments_;
if (
firstArgument.type === 'Literal' &&
typeof firstArgument.value === 'number'
) {
return 'alloc';
}
}
return 'from';
};
const create = context => {
return {
'NewExpression[callee.name="Buffer"]': node => {
const method = inferMethod(node.arguments);
const range = [
node.range[0],
node.callee.range[1]
];
context.report({
node,
message: `\`new Buffer()\` is deprecated, use \`Buffer.${method}()\` instead.`,
fix: fixer => fixer.replaceTextRange(range, `Buffer.${method}`)
});
}
};
};
module.exports = {
create,
meta: {
type: 'problem',
docs: {
url: getDocumentationUrl(__filename)
},
fixable: 'code'
}
};