forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-encoding-identifier-case.js
107 lines (93 loc) · 2.61 KB
/
text-encoding-identifier-case.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
'use strict';
const {replaceStringLiteral} = require('./fix/index.js');
const MESSAGE_ID_ERROR = 'text-encoding-identifier/error';
const MESSAGE_ID_SUGGESTION = 'text-encoding-identifier/suggestion';
const messages = {
[MESSAGE_ID_ERROR]: 'Prefer `{{replacement}}` over `{{value}}`.',
[MESSAGE_ID_SUGGESTION]: 'Replace `{{value}}` with `{{replacement}}`.',
};
const getReplacement = encoding => {
switch (encoding.toLowerCase()) {
case 'utf8':
case 'utf-8':
return 'utf8';
case 'ascii':
return 'ascii';
// No default
}
};
// `fs.{readFile,readFileSync}()`
const isFsReadFileEncoding = node =>
node.parent.type === 'CallExpression'
&& !node.parent.optional
&& node.parent.arguments[1] === node
&& node.parent.arguments[0]
&& node.parent.arguments[0].type !== 'SpreadElement'
&& node.parent.callee.type === 'MemberExpression'
&& !node.parent.callee.optional
&& !node.parent.callee.computed
&& node.parent.callee.property.type === 'Identifier'
&& (node.parent.callee.property.name === 'readFile' || node.parent.callee.property.name === 'readFileSync');
/** @param {import('eslint').Rule.RuleContext} context */
const create = () => ({
Literal(node) {
if (typeof node.value !== 'string') {
return;
}
if (
node.value === 'utf-8'
&& node.parent.type === 'JSXAttribute'
&& node.parent.value === node
&& node.parent.name.type === 'JSXIdentifier'
&& node.parent.name.name.toLowerCase() === 'charset'
&& node.parent.parent.type === 'JSXOpeningElement'
&& node.parent.parent.attributes.includes(node.parent)
&& node.parent.parent.name.type === 'JSXIdentifier'
&& node.parent.parent.name.name.toLowerCase() === 'meta'
) {
return;
}
const {raw} = node;
const value = raw.slice(1, -1);
const replacement = getReplacement(value);
if (!replacement || replacement === value) {
return;
}
const messageData = {
value,
replacement,
};
/** @param {import('eslint').Rule.RuleFixer} fixer */
const fix = fixer => replaceStringLiteral(fixer, node, replacement);
const problem = {
node,
messageId: MESSAGE_ID_ERROR,
data: messageData,
};
if (isFsReadFileEncoding(node)) {
problem.fix = fix;
return problem;
}
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
data: messageData,
fix: fixer => replaceStringLiteral(fixer, node, replacement),
},
];
return problem;
},
});
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Enforce consistent case for text encoding identifiers.',
},
fixable: 'code',
hasSuggestions: true,
messages,
},
};