Skip to content

Commit 0fc9e7d

Browse files
SamVerschuerensindresorhus
authored andcommitted
Add no-new-buffer rule - fixes #59 (#61)
1 parent d576ea5 commit 0fc9e7d

File tree

5 files changed

+141
-2
lines changed

5 files changed

+141
-2
lines changed

docs/rules/no-new-buffer.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Enforce the use of `Buffer.from()` and `Buffer.alloc()` instead of the deprecated `new Buffer()`
2+
3+
Enforces the use of [Buffer.from](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array) and [Buffer.alloc()](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding) instead of [new Buffer()](https://nodejs.org/api/buffer.html#buffer_new_buffer_array), which has been deprecated since Node.js 4.
4+
5+
6+
## Fail
7+
8+
```js
9+
const buf = new Buffer('7468697320697320612074c3a97374', 'hex');
10+
const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
11+
```
12+
13+
```js
14+
const buf = new Buffer(10);
15+
```
16+
17+
18+
## Pass
19+
20+
```js
21+
const buf = Buffer.from('7468697320697320612074c3a97374', 'hex');
22+
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])
23+
```
24+
25+
```js
26+
const buf = Buffer.alloc(10);
27+
```

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = {
2020
'unicorn/no-process-exit': 'error',
2121
'unicorn/throw-new-error': 'error',
2222
'unicorn/number-literal-case': 'error',
23-
'unicorn/no-array-instanceof': 'error'
23+
'unicorn/no-array-instanceof': 'error',
24+
'unicorn/no-new-buffer': 'error'
2425
}
2526
}
2627
}

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Configure it in `package.json`.
4040
"unicorn/no-process-exit": "error",
4141
"unicorn/throw-new-error": "error",
4242
"unicorn/number-literal-case": "error",
43-
"unicorn/no-array-instanceof": "error"
43+
"unicorn/no-array-instanceof": "error",
44+
"unicorn/no-new-buffer": "error"
4445
}
4546
}
4647
}
@@ -57,6 +58,7 @@ Configure it in `package.json`.
5758
- [throw-new-error](docs/rules/throw-new-error.md) - Require `new` when throwing an error. *(fixable)*
5859
- [number-literal-case](docs/rules/number-literal-case.md) - Enforce lowercase identifier and uppercase value for number literals. *(fixable)*
5960
- [no-array-instanceof](docs/rules/no-array-instanceof.md) - Require `Array.isArray()` instead of `instanceof Array`. *(fixable)*
61+
- [no-new-buffer](docs/rules/no-new-buffer.md) - Enforce the use of `Buffer.from()` and `Buffer.alloc()` instead of the deprecated `new Buffer()`. *(fixable)*
6062

6163

6264
## Recommended config

rules/no-new-buffer.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const inferMethod = args => (args.length > 0 && typeof args[0].value === 'number') ? 'alloc' : 'from';
3+
4+
const create = context => {
5+
return {
6+
NewExpression: node => {
7+
if (node.callee.name === 'Buffer') {
8+
const method = inferMethod(node.arguments);
9+
const range = [
10+
node.start,
11+
node.callee.end
12+
];
13+
14+
context.report({
15+
node,
16+
message: `\`new Buffer()\` is deprecated, use \`Buffer.${method}()\` instead.`,
17+
fix: fixer => fixer.replaceTextRange(range, `Buffer.${method}`)
18+
});
19+
}
20+
}
21+
};
22+
};
23+
24+
module.exports = {
25+
create,
26+
meta: {
27+
fixable: 'code'
28+
}
29+
};

test/no-new-buffer.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import test from 'ava';
2+
import avaRuleTester from 'eslint-ava-rule-tester';
3+
import rule from '../rules/no-new-buffer';
4+
5+
const ruleTester = avaRuleTester(test, {
6+
env: {
7+
es6: true
8+
},
9+
parserOptions: {
10+
sourceType: 'module'
11+
}
12+
});
13+
14+
const allocError = {
15+
ruleId: 'no-new-buffer',
16+
message: '`new Buffer()` is deprecated, use `Buffer.alloc()` instead.'
17+
};
18+
19+
const fromError = {
20+
ruleId: 'no-new-buffer',
21+
message: '`new Buffer()` is deprecated, use `Buffer.from()` instead.'
22+
};
23+
24+
ruleTester.run('no-new-buffer', rule, {
25+
valid: [
26+
`const buf = Buffer.from('buf')`,
27+
`const buf = Buffer.from('7468697320697320612074c3a97374', 'hex')`,
28+
'const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])',
29+
'const buf = Buffer.alloc(10)'
30+
],
31+
invalid: [
32+
{
33+
code: 'const buf = new Buffer()',
34+
errors: [fromError],
35+
output: 'const buf = Buffer.from()'
36+
},
37+
{
38+
code: `const buf = new Buffer('buf')`,
39+
errors: [fromError],
40+
output: `const buf = Buffer.from('buf')`
41+
},
42+
{
43+
code: `const buf = new Buffer('7468697320697320612074c3a97374', 'hex')`,
44+
errors: [fromError],
45+
output: `const buf = Buffer.from('7468697320697320612074c3a97374', 'hex')`
46+
},
47+
{
48+
code: 'const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])',
49+
errors: [fromError],
50+
output: 'const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])'
51+
},
52+
{
53+
code: 'const buf = new Buffer(10)',
54+
errors: [allocError],
55+
output: 'const buf = Buffer.alloc(10)'
56+
},
57+
{
58+
code: `
59+
const ab = new ArrayBuffer(10);
60+
const buf = new Buffer(ab, 0, 2);
61+
`,
62+
errors: [fromError],
63+
output: `
64+
const ab = new ArrayBuffer(10);
65+
const buf = Buffer.from(ab, 0, 2);
66+
`
67+
},
68+
{
69+
code: `
70+
const buf1 = new Buffer('buf');
71+
const buf2 = new Buffer(buf1);
72+
`,
73+
errors: [fromError, fromError],
74+
output: `
75+
const buf1 = Buffer.from('buf');
76+
const buf2 = Buffer.from(buf1);
77+
`
78+
}
79+
]
80+
});

0 commit comments

Comments
 (0)