UNPKG

873 BJavaScriptView Raw
1'use strict';
2const getDocumentationUrl = require('./utils/get-documentation-url');
3
4const inferMethod = arguments_ => {
5 if (arguments_.length > 0) {
6 const [firstArgument] = arguments_;
7 if (
8 firstArgument.type === 'Literal' &&
9 typeof firstArgument.value === 'number'
10 ) {
11 return 'alloc';
12 }
13 }
14
15 return 'from';
16};
17
18const create = context => {
19 return {
20 'NewExpression[callee.name="Buffer"]': node => {
21 const method = inferMethod(node.arguments);
22 const range = [
23 node.range[0],
24 node.callee.range[1]
25 ];
26
27 context.report({
28 node,
29 message: `\`new Buffer()\` is deprecated, use \`Buffer.${method}()\` instead.`,
30 fix: fixer => fixer.replaceTextRange(range, `Buffer.${method}`)
31 });
32 }
33 };
34};
35
36module.exports = {
37 create,
38 meta: {
39 type: 'problem',
40 docs: {
41 url: getDocumentationUrl(__filename)
42 },
43 fixable: 'code'
44 }
45};