tools: add buffer-constructor eslint rule

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: https://github.com/nodejs/node/pull/5740
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
James M Snell
2016-03-16 10:37:35 -07:00
parent 089bef0a81
commit 9e30129fa7
3 changed files with 29 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
rules:
# Custom rules in tools/eslint-rules
require-buffer: 2
buffer-constructor: 2

3
src/.eslintrc Normal file
View File

@@ -0,0 +1,3 @@
rules:
# Custom rules in tools/eslint-rules
buffer-constructor: 2

View File

@@ -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)
};
};