-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
prefer-json-parse-buffer.js
156 lines (129 loc) · 3.27 KB
/
prefer-json-parse-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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const {findVariable, getStaticValue, getPropertyName} = require('eslint-utils');
const {methodCallSelector} = require('./selectors/index.js');
const {removeArgument} = require('./fix/index.js');
const MESSAGE_ID = 'prefer-json-parse-buffer';
const messages = {
[MESSAGE_ID]: 'Prefer reading the JSON file as a buffer.',
};
const jsonParseArgumentSelector = [
methodCallSelector({
object: 'JSON',
method: 'parse',
argumentsLength: 1,
}),
' > .arguments:first-child',
].join('');
const getAwaitExpressionArgument = node => {
while (node.type === 'AwaitExpression') {
node = node.argument;
}
return node;
};
function getIdentifierDeclaration(node, scope) {
if (!node) {
return;
}
node = getAwaitExpressionArgument(node);
if (!node || node.type !== 'Identifier') {
return node;
}
const variable = findVariable(scope, node);
if (!variable) {
return;
}
const {identifiers, references} = variable;
if (identifiers.length !== 1 || references.length !== 2) {
return;
}
const [identifier] = identifiers;
if (
identifier.parent.type !== 'VariableDeclarator'
|| identifier.parent.id !== identifier
) {
return;
}
return getIdentifierDeclaration(identifier.parent.init, variable.scope);
}
const isUtf8EncodingStringNode = (node, scope) =>
isUtf8EncodingString(getStaticValue(node, scope)?.value);
const isUtf8EncodingString = value => {
if (typeof value !== 'string') {
return false;
}
value = value.toLowerCase();
// eslint-disable-next-line unicorn/text-encoding-identifier-case
return value === 'utf8' || value === 'utf-8';
};
function isUtf8Encoding(node, scope) {
if (
node.type === 'ObjectExpression'
&& node.properties.length === 1
&& node.properties[0].type === 'Property'
&& getPropertyName(node.properties[0], scope) === 'encoding'
&& isUtf8EncodingStringNode(node.properties[0].value, scope)
) {
return true;
}
if (isUtf8EncodingStringNode(node, scope)) {
return true;
}
const staticValue = getStaticValue(node, scope);
if (!staticValue) {
return false;
}
const {value} = staticValue;
if (
typeof value === 'object'
&& Object.keys(value).length === 1
&& isUtf8EncodingString(value.encoding)
) {
return true;
}
return false;
}
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => ({
[jsonParseArgumentSelector](node) {
const scope = context.getScope();
node = getIdentifierDeclaration(node, scope);
if (
!(
node
&& node.type === 'CallExpression'
&& !node.optional
&& node.arguments.length === 2
&& !node.arguments.some(node => node.type === 'SpreadElement')
&& node.callee.type === 'MemberExpression'
&& !node.callee.optional
)
) {
return;
}
const method = getPropertyName(node.callee, scope);
if (method !== 'readFile' && method !== 'readFileSync') {
return;
}
const [, charsetNode] = node.arguments;
if (!isUtf8Encoding(charsetNode, scope)) {
return;
}
return {
node: charsetNode,
messageId: MESSAGE_ID,
fix: fixer => removeArgument(fixer, charsetNode, context.getSourceCode()),
};
},
});
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Prefer reading a JSON file as a buffer.',
},
fixable: 'code',
messages,
},
};