Skip to content

Commit

Permalink
tools: add buffer-constructor eslint rule
Browse files Browse the repository at this point in the history
Now that the Buffer.alloc, allocUnsafe, and from methods have landed,
add a linting rule that requires their use within lib. Tests and
benchmarks are explicitly excluded by the rule.

PR-URL: #5740
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
jasnell committed Mar 19, 2016
1 parent 089bef0 commit 9e30129
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rules:
# Custom rules in tools/eslint-rules
require-buffer: 2
buffer-constructor: 2
3 changes: 3 additions & 0 deletions src/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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)
};
};

0 comments on commit 9e30129

Please sign in to comment.