/
no-new-buffer.js
94 lines (88 loc) · 2.19 KB
/
no-new-buffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {outdent} from 'outdent';
import rule from '../rules/no-new-buffer';
const ruleTester = avaRuleTester(test, {
env: {
es6: true
},
parserOptions: {
sourceType: 'module'
}
});
const typescriptRuleTester = avaRuleTester(test, {
parser: require.resolve('@typescript-eslint/parser')
});
const allocError = {
message: '`new Buffer()` is deprecated, use `Buffer.alloc()` instead.'
};
const fromError = {
message: '`new Buffer()` is deprecated, use `Buffer.from()` instead.'
};
ruleTester.run('no-new-buffer', rule, {
valid: [
'const buf = Buffer.from(\'buf\')',
'const buf = Buffer.from(\'7468697320697320612074c3a97374\', \'hex\')',
'const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])',
'const buf = Buffer.alloc(10)'
],
invalid: [
{
code: 'const buf = new Buffer()',
errors: [fromError],
output: 'const buf = Buffer.from()'
},
{
code: 'const buf = new Buffer(\'buf\')',
errors: [fromError],
output: 'const buf = Buffer.from(\'buf\')'
},
{
code: 'const buf = new Buffer(\'7468697320697320612074c3a97374\', \'hex\')',
errors: [fromError],
output: 'const buf = Buffer.from(\'7468697320697320612074c3a97374\', \'hex\')'
},
{
code: 'const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])',
errors: [fromError],
output: 'const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])'
},
{
code: 'const buf = new Buffer(10)',
errors: [allocError],
output: 'const buf = Buffer.alloc(10)'
},
{
code: outdent`
const ab = new ArrayBuffer(10);
const buf = new Buffer(ab, 0, 2);
`,
errors: [fromError],
output: outdent`
const ab = new ArrayBuffer(10);
const buf = Buffer.from(ab, 0, 2);
`
},
{
code: outdent`
const buf1 = new Buffer('buf');
const buf2 = new Buffer(buf1);
`,
errors: [fromError, fromError],
output: outdent`
const buf1 = Buffer.from('buf');
const buf2 = Buffer.from(buf1);
`
}
]
});
typescriptRuleTester.run('no-new-buffer', rule, {
valid: [],
invalid: [
{
code: 'new Buffer(input, encoding);',
errors: [fromError],
output: 'Buffer.from(input, encoding);'
}
]
});