mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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:
@@ -1,3 +1,4 @@
|
||||
rules:
|
||||
# Custom rules in tools/eslint-rules
|
||||
require-buffer: 2
|
||||
buffer-constructor: 2
|
||||
|
||||
3
src/.eslintrc
Normal file
3
src/.eslintrc
Normal file
@@ -0,0 +1,3 @@
|
||||
rules:
|
||||
# Custom rules in tools/eslint-rules
|
||||
buffer-constructor: 2
|
||||
25
tools/eslint-rules/buffer-constructor.js
Normal file
25
tools/eslint-rules/buffer-constructor.js
Normal 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)
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user