@lingual/i18n-check 0.8.8 → 0.8.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/index.js +2 -2
- package/dist/index.js +35 -18
- package/package.json +1 -1
package/dist/bin/index.js
CHANGED
|
@@ -185,8 +185,8 @@ const main = async () => {
|
|
|
185
185
|
console.log(chalk_1.default.green(`\nDone in ${Math.round(((end - start) * 100) / 1000) / 100}s.`));
|
|
186
186
|
if ((result.missingKeys && Object.keys(result.missingKeys).length > 0) ||
|
|
187
187
|
(result.invalidKeys && Object.keys(result.invalidKeys).length > 0) ||
|
|
188
|
-
(unusedKeyResult &&
|
|
189
|
-
(undefinedKeyResult &&
|
|
188
|
+
(unusedKeyResult && hasKeys(unusedKeyResult)) ||
|
|
189
|
+
(undefinedKeyResult && hasKeys(undefinedKeyResult))) {
|
|
190
190
|
(0, node_process_1.exit)(1);
|
|
191
191
|
}
|
|
192
192
|
else {
|
package/dist/index.js
CHANGED
|
@@ -61,14 +61,18 @@ const checkUnusedKeys = async (translationFiles, filesToParse, options = {
|
|
|
61
61
|
if (!options.checks || !options.checks.includes('unused')) {
|
|
62
62
|
return undefined;
|
|
63
63
|
}
|
|
64
|
+
const filteredTranslationFiles = translationFiles.map(({ content, ...rest }) => ({
|
|
65
|
+
...rest,
|
|
66
|
+
content: filterKeys(content, options.ignore),
|
|
67
|
+
}));
|
|
64
68
|
if (options.format === 'react-intl') {
|
|
65
|
-
return findUnusedReactIntlTranslations(
|
|
69
|
+
return findUnusedReactIntlTranslations(filteredTranslationFiles, filesToParse);
|
|
66
70
|
}
|
|
67
71
|
else if (options.format === 'i18next') {
|
|
68
|
-
return findUnusedI18NextTranslations(
|
|
72
|
+
return findUnusedI18NextTranslations(filteredTranslationFiles, filesToParse, componentFunctions);
|
|
69
73
|
}
|
|
70
74
|
else if (options.format === 'next-intl') {
|
|
71
|
-
return findUnusedNextIntlTranslations(
|
|
75
|
+
return findUnusedNextIntlTranslations(filteredTranslationFiles, filesToParse);
|
|
72
76
|
}
|
|
73
77
|
};
|
|
74
78
|
exports.checkUnusedKeys = checkUnusedKeys;
|
|
@@ -162,17 +166,19 @@ const checkUndefinedKeys = async (source, filesToParse, options = {
|
|
|
162
166
|
return undefined;
|
|
163
167
|
}
|
|
164
168
|
if (options.format === 'react-intl') {
|
|
165
|
-
return findUndefinedReactIntlKeys(source, filesToParse);
|
|
169
|
+
return findUndefinedReactIntlKeys(source, filesToParse, options);
|
|
166
170
|
}
|
|
167
171
|
else if (options.format === 'i18next') {
|
|
168
|
-
return findUndefinedI18NextKeys(source, filesToParse, componentFunctions);
|
|
172
|
+
return findUndefinedI18NextKeys(source, filesToParse, options, componentFunctions);
|
|
169
173
|
}
|
|
170
174
|
else if (options.format === 'next-intl') {
|
|
171
|
-
return findUndefinedNextIntlKeys(source, filesToParse);
|
|
175
|
+
return findUndefinedNextIntlKeys(source, filesToParse, options);
|
|
172
176
|
}
|
|
173
177
|
};
|
|
174
178
|
exports.checkUndefinedKeys = checkUndefinedKeys;
|
|
175
|
-
const findUndefinedReactIntlKeys = async (translationFiles, filesToParse
|
|
179
|
+
const findUndefinedReactIntlKeys = async (translationFiles, filesToParse, options = {
|
|
180
|
+
ignore: [],
|
|
181
|
+
}) => {
|
|
176
182
|
const sourceKeys = new Set(translationFiles.flatMap(({ content }) => {
|
|
177
183
|
return Object.keys(content);
|
|
178
184
|
}));
|
|
@@ -181,7 +187,7 @@ const findUndefinedReactIntlKeys = async (translationFiles, filesToParse) => {
|
|
|
181
187
|
});
|
|
182
188
|
const undefinedKeys = {};
|
|
183
189
|
Object.entries(JSON.parse(extractedResult)).forEach(([key, meta]) => {
|
|
184
|
-
if (!sourceKeys.has(key)) {
|
|
190
|
+
if (!sourceKeys.has(key) && !isIgnoredKey(options.ignore ?? [], key)) {
|
|
185
191
|
const data = meta;
|
|
186
192
|
if (!('file' in data) || typeof data.file !== 'string') {
|
|
187
193
|
return;
|
|
@@ -195,7 +201,9 @@ const findUndefinedReactIntlKeys = async (translationFiles, filesToParse) => {
|
|
|
195
201
|
});
|
|
196
202
|
return undefinedKeys;
|
|
197
203
|
};
|
|
198
|
-
const findUndefinedI18NextKeys = async (source, filesToParse,
|
|
204
|
+
const findUndefinedI18NextKeys = async (source, filesToParse, options = {
|
|
205
|
+
ignore: [],
|
|
206
|
+
}, componentFunctions = []) => {
|
|
199
207
|
const { extractedResult, skippableKeys } = await getI18NextKeysInCode(filesToParse, componentFunctions);
|
|
200
208
|
const sourceKeys = new Set(source.flatMap(({ content }) => {
|
|
201
209
|
return Object.keys(content);
|
|
@@ -205,7 +213,9 @@ const findUndefinedI18NextKeys = async (source, filesToParse, componentFunctions
|
|
|
205
213
|
const isSkippable = skippableKeys.find((skippableKey) => {
|
|
206
214
|
return key.includes(skippableKey);
|
|
207
215
|
});
|
|
208
|
-
if (isSkippable === undefined &&
|
|
216
|
+
if (isSkippable === undefined &&
|
|
217
|
+
!sourceKeys.has(key) &&
|
|
218
|
+
!isIgnoredKey(options.ignore ?? [], key)) {
|
|
209
219
|
if (!undefinedKeys[file]) {
|
|
210
220
|
undefinedKeys[file] = [];
|
|
211
221
|
}
|
|
@@ -214,14 +224,18 @@ const findUndefinedI18NextKeys = async (source, filesToParse, componentFunctions
|
|
|
214
224
|
});
|
|
215
225
|
return undefinedKeys;
|
|
216
226
|
};
|
|
217
|
-
const findUndefinedNextIntlKeys = async (translationFiles, filesToParse
|
|
227
|
+
const findUndefinedNextIntlKeys = async (translationFiles, filesToParse, options = {
|
|
228
|
+
ignore: [],
|
|
229
|
+
}) => {
|
|
218
230
|
const sourceKeys = new Set(translationFiles.flatMap(({ content }) => {
|
|
219
231
|
return Object.keys(content);
|
|
220
232
|
}));
|
|
221
233
|
const extractedResult = (0, nextIntlSrcParser_1.extract)(filesToParse);
|
|
222
234
|
const undefinedKeys = {};
|
|
223
235
|
extractedResult.forEach(({ key, meta }) => {
|
|
224
|
-
if (!meta.dynamic &&
|
|
236
|
+
if (!meta.dynamic &&
|
|
237
|
+
!sourceKeys.has(key) &&
|
|
238
|
+
!isIgnoredKey(options.ignore ?? [], key)) {
|
|
225
239
|
const file = meta.file;
|
|
226
240
|
if (!undefinedKeys[file]) {
|
|
227
241
|
undefinedKeys[file] = [];
|
|
@@ -297,12 +311,7 @@ const getI18NextKeysInCode = async (filesToParse, componentFunctions = []) => {
|
|
|
297
311
|
const filterKeys = (content, keysToIgnore = []) => {
|
|
298
312
|
if (keysToIgnore.length > 0) {
|
|
299
313
|
return Object.entries(content).reduce((acc, [key, value]) => {
|
|
300
|
-
if (keysToIgnore
|
|
301
|
-
if (ignoreKey.endsWith('*')) {
|
|
302
|
-
return key.includes(ignoreKey.slice(0, ignoreKey.length - 1));
|
|
303
|
-
}
|
|
304
|
-
return ignoreKey === key;
|
|
305
|
-
})) {
|
|
314
|
+
if (isIgnoredKey(keysToIgnore, key)) {
|
|
306
315
|
return acc;
|
|
307
316
|
}
|
|
308
317
|
acc[key] = value;
|
|
@@ -311,6 +320,14 @@ const filterKeys = (content, keysToIgnore = []) => {
|
|
|
311
320
|
}
|
|
312
321
|
return content;
|
|
313
322
|
};
|
|
323
|
+
const isIgnoredKey = (keysToIgnore, key) => {
|
|
324
|
+
return (keysToIgnore.find((ignoreKey) => {
|
|
325
|
+
if (ignoreKey.endsWith('*')) {
|
|
326
|
+
return key.includes(ignoreKey.slice(0, ignoreKey.length - 1));
|
|
327
|
+
}
|
|
328
|
+
return ignoreKey === key;
|
|
329
|
+
}) !== undefined);
|
|
330
|
+
};
|
|
314
331
|
function _flatten(object, prefix = null, result = {}) {
|
|
315
332
|
for (const key in object) {
|
|
316
333
|
const propName = prefix ? `${prefix}.${key}` : key;
|