/
identical-keys.js
222 lines (202 loc) · 5.3 KB
/
identical-keys.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const requireNoCache = require('./util/require-no-cache');
const compareTranslationsStructure = require('./util/compare-translations-structure');
const getTranslationFileSource = require('./util/get-translation-file-source');
const noDifferenceRegex = /Compared\s+values\s+have\s+no\s+visual\s+difference/i;
// suffix match each key in the mapping with the current source file path.
// pick the first match.
const getKeyStructureFromMap = (filePathMap, sourceFilePath) => {
// do a suffix match
const match = Object.keys(filePathMap)
.filter(filePath => sourceFilePath.endsWith(filePath))
.pop();
if (match) {
try {
const filepath = filePathMap[match];
return requireNoCache(filepath);
} catch (e) {
throw new Error(`\n Error parsing or retrieving key structure comparison file based on "filePath" mapping\n\n "${match}" => "${filePathMap[match]}".\n\n Check the "filePath" option for this rule. \n ${e}`);
}
}
throw new Error('\n Current translation file does not have a matching entry in the "filePath" map.\n Check the "filePath" option for this rule.\n');
};
/*
comparisonOptions : {
filePath = (string | Function | Object)
If it's a string, then it can be a file to require in order to compare
it's key structure with the current translation file.
- If the required value is a function, then the function is called
with the sourceFilePath and parsed translations to retreive the key structure.
If it's an object , then it should have a mapping b/w file names
and what key structure file to require.
}
*/
const getKeyStructureToMatch = (
options = {},
currentTranslations,
sourceFilePath
) => {
let keyStructure = null;
let { filePath } = options;
if (typeof filePath === 'string') {
filePath = filePath.trim();
}
if (!filePath) {
return {
errors: [
{
message: '"filePath" rule option not specified.',
loc: {
start: {
line: 0,
col: 0
}
}
}
]
};
}
if (typeof filePath === 'string') {
try {
keyStructure = requireNoCache(filePath); //eslint-disable-line
} catch (e) {
return {
errors: [
{
message: `\n Error parsing or retrieving key structure comparison file from\n "${filePath}".\n Check the "filePath" option for this rule.\n ${e}`,
loc: {
start: {
line: 0,
col: 0
}
}
}
]
};
}
if (typeof keyStructure !== 'function') {
return {
keyStructure
};
}
// keyStructure exported a function
try {
return {
keyStructure: keyStructure(currentTranslations, sourceFilePath)
};
} catch (e) {
return {
errors: [
{
message: `\n Error when calling custom key structure function from\n "${filePath}".\n Check the "filePath" option for this rule.\n ${e}`,
loc: {
start: {
line: 0,
col: 0
}
}
}
]
};
}
}
// due to eslint rule schema, we can assume the "filePath" option is an object.
// anything else will be caught by the eslint rule schema validator.
try {
return {
keyStructure: getKeyStructureFromMap(filePath, sourceFilePath)
};
} catch (e) {
return {
errors: [
{
message: `${e}`,
loc: {
start: {
line: 0,
col: 0
}
}
}
]
};
}
};
const identicalKeys = (context, source, sourceFilePath) => {
const { options, settings = {} } = context;
const comparisonOptions = options[0];
let currentTranslations = null;
try {
currentTranslations = JSON.parse(source);
} catch (e) {
// don't return any errors
// will be caught with the valid-json rule.
return [];
}
const { errors, keyStructure } = getKeyStructureToMatch(
comparisonOptions,
currentTranslations,
sourceFilePath
);
if (errors) {
// errors generated from trying to get the key structure
return errors;
}
const diffString = compareTranslationsStructure(
settings,
keyStructure,
currentTranslations
);
if (noDifferenceRegex.test(diffString.trim())) {
// success
return [];
}
// mismatch
return [
{
message: `\n${diffString}`,
loc: {
start: {
line: 0,
col: 0
}
}
}
];
};
module.exports = {
meta: {
docs: {
category: 'Consistency',
description:
'Verifies the key structure for the translation file matches the key structure specified in the options',
recommended: false
},
schema: [
{
properties: {
filePath: {
type: ['string', 'object']
}
},
type: 'object'
}
]
},
create(context) {
return {
Program(node) {
const { valid, source, sourceFilePath } = getTranslationFileSource({
context,
node
});
if (!valid) {
return;
}
const errors = identicalKeys(context, source, sourceFilePath);
errors.forEach((error) => {
context.report(error);
});
}
};
}
};