From b2567db1b0b0f884f292e435560d386ef0997397 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 16 Mar 2016 10:37:35 -0700 Subject: [PATCH] tools: add buffer-constructor 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. --- lib/.eslintrc | 2 ++ src/.eslintrc | 4 ++++ tools/eslint-rules/buffer-constructor.js | 25 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/.eslintrc create mode 100644 tools/eslint-rules/buffer-constructor.js diff --git a/lib/.eslintrc b/lib/.eslintrc index 341e9327c7cf3a..1366d7bcf05d17 100644 --- a/lib/.eslintrc +++ b/lib/.eslintrc @@ -1,3 +1,5 @@ rules: # Custom rules in tools/eslint-rules require-buffer: 2 + buffer-constructor: 2 + diff --git a/src/.eslintrc b/src/.eslintrc new file mode 100644 index 00000000000000..0d6718d5a9d85f --- /dev/null +++ b/src/.eslintrc @@ -0,0 +1,4 @@ +rules: + # Custom rules in tools/eslint-rules + buffer-constructor: 2 + diff --git a/tools/eslint-rules/buffer-constructor.js b/tools/eslint-rules/buffer-constructor.js new file mode 100644 index 00000000000000..938598e8dbf618 --- /dev/null +++ b/tools/eslint-rules/buffer-constructor.js @@ -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) + }; +};