diff --git a/test/parallel/test-eslint-buffer-constructor.js b/test/parallel/test-eslint-buffer-constructor.js index dfff1374c1b9c0..16dca859e8963d 100644 --- a/test/parallel/test-eslint-buffer-constructor.js +++ b/test/parallel/test-eslint-buffer-constructor.js @@ -11,16 +11,29 @@ const message = 'Use of the Buffer() constructor has been deprecated. ' + new RuleTester().run('buffer-constructor', rule, { valid: [ - 'Buffer.from(foo)' + 'Buffer.from(foo)', + 'Buffer.from(foo, bar)', + 'Buffer.alloc(foo)', + 'Buffer.alloc(foo, bar)', + 'Buffer.allocUnsafe(foo)', + 'Buffer.allocUnsafe(foo, bar)' ], invalid: [ { code: 'Buffer(foo)', errors: [{ message }] }, + { + code: 'Buffer(foo, bar)', + errors: [{ message }] + }, { code: 'new Buffer(foo)', errors: [{ message }] + }, + { + code: 'new Buffer(foo, bar)', + errors: [{ message }] } ] }); diff --git a/tools/eslint-rules/buffer-constructor.js b/tools/eslint-rules/buffer-constructor.js index 938598e8dbf618..065ed9a83250b5 100644 --- a/tools/eslint-rules/buffer-constructor.js +++ b/tools/eslint-rules/buffer-constructor.js @@ -13,7 +13,22 @@ const msg = 'Use of the Buffer() constructor has been deprecated. ' + function test(context, node) { if (node.callee.name === 'Buffer') { - context.report(node, msg); + const sourceCode = context.getSourceCode(); + const argumentList = []; + node.arguments.forEach((argumentNode) => { + argumentList.push(sourceCode.getText(argumentNode)); + }); + + context.report({ + node, + message: msg, + fix: (fixer) => { + return fixer.replaceText( + node, + `Buffer.from(${argumentList.join(', ')})` + ); + } + }); } }