0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/tools/eslint-rules/buffer-constructor.js
James M Snell 9e30129fa7 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>
2016-03-18 17:05:18 -07:00

26 lines
743 B
JavaScript

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