Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: add buffer-constructor eslint rule #5740

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
rules:
# Custom rules in tools/eslint-rules
require-buffer: 2
buffer-constructor: 2

4 changes: 4 additions & 0 deletions src/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rules:
# Custom rules in tools/eslint-rules
buffer-constructor: 2

25 changes: 25 additions & 0 deletions tools/eslint-rules/buffer-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @fileoverview Require use of new Buffer constructor methods in lib
* @author James M Snell
*/
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const msg = 'Use of the Buffer() constructor has been deprecated. ' +
'Please use either Buffer.alloc(), Buffer.allocUnsafe(), ' +
'or Buffer.from()';

function test(context, node) {
if (node.callee.name === 'Buffer') {
context.report(node, msg);
}
}

module.exports = function(context) {
return {
'NewExpression': (node) => test(context, node),
'CallExpression': (node) => test(context, node)
};
};