-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
3 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
rules: | ||
# Custom rules in tools/eslint-rules | ||
require-buffer: 2 | ||
buffer-constructor: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
rules: | ||
# Custom rules in tools/eslint-rules | ||
buffer-constructor: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}; | ||
}; |