Skip to content

Commit 9e30129

Browse files
committed
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: #5740 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 089bef0 commit 9e30129

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
rules:
22
# Custom rules in tools/eslint-rules
33
require-buffer: 2
4+
buffer-constructor: 2

src/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rules:
2+
# Custom rules in tools/eslint-rules
3+
buffer-constructor: 2
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @fileoverview Require use of new Buffer constructor methods in lib
3+
* @author James M Snell
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
const msg = 'Use of the Buffer() constructor has been deprecated. ' +
11+
'Please use either Buffer.alloc(), Buffer.allocUnsafe(), ' +
12+
'or Buffer.from()';
13+
14+
function test(context, node) {
15+
if (node.callee.name === 'Buffer') {
16+
context.report(node, msg);
17+
}
18+
}
19+
20+
module.exports = function(context) {
21+
return {
22+
'NewExpression': (node) => test(context, node),
23+
'CallExpression': (node) => test(context, node)
24+
};
25+
};

0 commit comments

Comments
 (0)