-
Notifications
You must be signed in to change notification settings - Fork 9
/
eslint-local-rules.js
76 lines (76 loc) · 2.86 KB
/
eslint-local-rules.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
module.exports = {
'disallow-buffer-from-alloc': {
meta: {
type: 'problem', // Define the rule type
docs: {
description: 'Disallow usage of Buffer.from',
category: 'Best Practices',
recommended: false
},
schema: [], // No options for this rule
messages: {
avoidBufferFrom:
'Using Buffer.from is not allowed, please use Uint8Array.from or HexUint.of.bytes instead.',
avoidBufferAlloc:
'Using Buffer.alloc is not allowed, please use Uint8Array methods instead.'
}
},
create(context) {
return {
CallExpression(node) {
const callee = node.callee;
if (
callee.type === 'MemberExpression' && // Check if it's a member expression
callee.object.name === 'Buffer' // Ensure it's `Buffer`
) {
if (callee.property.name === 'from') {
// Ensure it's calling `from`)
context.report({
node,
messageId: 'avoidBufferFrom'
});
} else if (callee.property.name === 'alloc') {
// Ensure it's calling `from`)
context.report({
node,
messageId: 'avoidBufferAlloc'
});
}
}
}
};
}
},
'disallow-instanceof-uint8array': {
meta: {
type: 'problem', // Define the rule type
docs: {
description: 'Disallow usage of instanceof Uint8Array',
category: 'Best Practices',
recommended: false
},
schema: [], // No options for this rule
messages: {
avoidInstanceOfUint8Array:
'Please review if you can avoid using instanceof Uint8Array. If not, please use ArrayBuffer.isView instead.'
}
},
create(context) {
return {
BinaryExpression(node) {
// Check if it's an instanceof expression
if (
node.operator === 'instanceof' &&
node.right.type === 'Identifier' &&
node.right.name === 'Uint8Array'
) {
context.report({
node,
messageId: 'avoidInstanceOfUint8Array'
});
}
}
};
}
}
};