Skip to content

Commit 548b603

Browse files
committed
Lint & format code
1 parent 801e41a commit 548b603

3 files changed

Lines changed: 64 additions & 108 deletions

File tree

src/utils/xmlTranslation.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export function parseXmlResponse(xmlResponse, batch, pluralCount, logger, dictio
258258
const formMatch = block.match(formRegex);
259259

260260
if (formMatch) {
261-
const source = i === 0 ? batch[batchIndex].msgid : (batch[batchIndex].msgid_plural || batch[batchIndex].msgid);
261+
const source = i === 0 ? batch[batchIndex].msgid : batch[batchIndex].msgid_plural || batch[batchIndex].msgid;
262262
const translation = normalizeNbsp(decodeXmlEntities(formMatch[1]), source);
263263

264264
forms[i] = translation;
@@ -348,15 +348,15 @@ export function parseXmlResponse(xmlResponse, batch, pluralCount, logger, dictio
348348
const expectedArr = (expectedPh || '').split(',').filter(Boolean);
349349
const transArr = (transPlaceholders || '').split(',').filter(Boolean);
350350

351-
function buildFreqMap(arr) {
351+
const buildFreqMap = (arr) => {
352352
const freq = {};
353353

354354
for (const p of arr) {
355355
freq[p] = (freq[p] || 0) + 1;
356356
}
357357

358358
return freq;
359-
}
359+
};
360360

361361
const expectedFreq = buildFreqMap(expectedArr);
362362
const transFreq = buildFreqMap(transArr);
@@ -393,10 +393,7 @@ export function parseXmlResponse(xmlResponse, batch, pluralCount, logger, dictio
393393

394394
if (!isValid) {
395395
if (verbosityLevel >= 1) {
396-
logger.warn(
397-
`Placeholder mismatch for "${entry.msgid.substring(0, 50)}..." [form ${fi}] — ` +
398-
`expected [${expectedPh}], got [${transPlaceholders}]. Blanking translation.`,
399-
);
396+
logger.warn(`Placeholder mismatch for "${entry.msgid.substring(0, 50)}..." [form ${fi}] — ` + `expected [${expectedPh}], got [${transPlaceholders}]. Blanking translation.`);
400397
}
401398

402399
result[idx].msgstr[fi] = '';

tests/unit/normalizeNbsp.test.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,15 @@ describe('normalizeNbsp', () => {
146146

147147
describe('plural forms', () => {
148148
it('should normalize nbsp in plural form translations', () => {
149-
const batch = [{
150-
msgid: '%d entry found. ',
151-
msgid_plural: '%d entries found. ',
152-
}];
149+
const batch = [
150+
{
151+
msgid: '%d entry found. ',
152+
msgid_plural: '%d entries found. ',
153+
},
154+
];
153155

154156
const trailing = '\u00A0'.repeat(20);
155-
const forms = [
156-
`%d запись найдена.${trailing}`,
157-
`%d записи найдены.${trailing}`,
158-
`%d записей найдено.${trailing}`,
159-
];
157+
const forms = [`%d запись найдена.${trailing}`, `%d записи найдены.${trailing}`, `%d записей найдено.${trailing}`];
160158

161159
const { translations } = parseXmlResponse(xml([forms]), batch, 3, mockLogger);
162160

tests/unit/placeholderValidation.test.js

Lines changed: 53 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,14 @@ describe('placeholder validation', () => {
180180
describe('correct placeholders (should pass)', () => {
181181
it('should keep translation with identical placeholders', () => {
182182
const batch = [{ msgid: 'Hello %s, you have %d items' }];
183-
const { translations } = parseXmlResponse(
184-
xml(['Bonjour %s, vous avez %d articles']),
185-
batch, 1, mockLogger,
186-
);
183+
const { translations } = parseXmlResponse(xml(['Bonjour %s, vous avez %d articles']), batch, 1, mockLogger);
187184

188185
expect(translations[0].msgstr[0]).toBe('Bonjour %s, vous avez %d articles');
189186
});
190187

191188
it('should keep translation with positional placeholders in different order', () => {
192189
const batch = [{ msgid: '%1$s has %2$d entries in %3$s' }];
193-
const { translations } = parseXmlResponse(
194-
xml(['В %3$s у %1$s есть %2$d записей']),
195-
batch, 1, mockLogger,
196-
);
190+
const { translations } = parseXmlResponse(xml(['В %3$s у %1$s есть %2$d записей']), batch, 1, mockLogger);
197191

198192
expect(translations[0].msgstr[0]).toBe('В %3$s у %1$s есть %2$d записей');
199193
});
@@ -204,10 +198,7 @@ describe('placeholder validation', () => {
204198
const { logger, warnings } = createSpyLogger();
205199
const batch = [{ msgid: 'Hello %s' }];
206200

207-
const { translations } = parseXmlResponse(
208-
xml(['Bonjour %s %s']),
209-
batch, 1, logger,
210-
);
201+
const { translations } = parseXmlResponse(xml(['Bonjour %s %s']), batch, 1, logger);
211202

212203
expect(translations[0].msgstr[0]).toBe('');
213204
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -217,10 +208,7 @@ describe('placeholder validation', () => {
217208
const { logger, warnings } = createSpyLogger();
218209
const batch = [{ msgid: 'Hello %s' }];
219210

220-
const { translations } = parseXmlResponse(
221-
xml(['Bonjour %s (%d)']),
222-
batch, 1, logger,
223-
);
211+
const { translations } = parseXmlResponse(xml(['Bonjour %s (%d)']), batch, 1, logger);
224212

225213
expect(translations[0].msgstr[0]).toBe('');
226214
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -232,10 +220,7 @@ describe('placeholder validation', () => {
232220
const { logger, warnings } = createSpyLogger();
233221
const batch = [{ msgid: '%s has %d entries' }];
234222

235-
const { translations } = parseXmlResponse(
236-
xml(['имеет %d записей']),
237-
batch, 1, logger,
238-
);
223+
const { translations } = parseXmlResponse(xml(['имеет %d записей']), batch, 1, logger);
239224

240225
expect(translations[0].msgstr[0]).toBe('');
241226
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -245,10 +230,7 @@ describe('placeholder validation', () => {
245230
const { logger, warnings } = createSpyLogger();
246231
const batch = [{ msgid: 'View %1$s by %2$s' }];
247232

248-
const { translations } = parseXmlResponse(
249-
xml(['Просмотреть запись автора']),
250-
batch, 1, logger,
251-
);
233+
const { translations } = parseXmlResponse(xml(['Просмотреть запись автора']), batch, 1, logger);
252234

253235
expect(translations[0].msgstr[0]).toBe('');
254236
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -260,10 +242,7 @@ describe('placeholder validation', () => {
260242
const { logger, warnings } = createSpyLogger();
261243
const batch = [{ msgid: 'Hello world' }];
262244

263-
const { translations } = parseXmlResponse(
264-
xml(['Bonjour %s monde']),
265-
batch, 1, logger,
266-
);
245+
const { translations } = parseXmlResponse(xml(['Bonjour %s monde']), batch, 1, logger);
267246

268247
expect(translations[0].msgstr[0]).toBe('');
269248
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -273,10 +252,7 @@ describe('placeholder validation', () => {
273252
const { logger, warnings } = createSpyLogger();
274253
const batch = [{ msgid: 'Settings' }];
275254

276-
const { translations } = parseXmlResponse(
277-
xml(['Настройки %d']),
278-
batch, 1, logger,
279-
);
255+
const { translations } = parseXmlResponse(xml(['Настройки %d']), batch, 1, logger);
280256

281257
expect(translations[0].msgstr[0]).toBe('');
282258
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -291,10 +267,7 @@ describe('placeholder validation', () => {
291267
const { logger, warnings } = createSpyLogger();
292268
const batch = [{ msgid: 'Hello %s' }];
293269

294-
const { translations } = parseXmlResponse(
295-
xml(['Bonjour \uFF05s']),
296-
batch, 1, logger,
297-
);
270+
const { translations } = parseXmlResponse(xml(['Bonjour \uFF05s']), batch, 1, logger);
298271

299272
expect(translations[0].msgstr[0]).toBe('');
300273
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -304,10 +277,7 @@ describe('placeholder validation', () => {
304277
const { logger, warnings } = createSpyLogger();
305278
const batch = [{ msgid: '%1$s by %2$s' }];
306279

307-
const { translations } = parseXmlResponse(
308-
xml(['\uFF051$s от \uFF052$s']),
309-
batch, 1, logger,
310-
);
280+
const { translations } = parseXmlResponse(xml(['\uFF051$s от \uFF052$s']), batch, 1, logger);
311281

312282
expect(translations[0].msgstr[0]).toBe('');
313283
expect(warnings.some((w) => w.includes('Placeholder mismatch'))).toBe(true);
@@ -316,17 +286,15 @@ describe('placeholder validation', () => {
316286

317287
describe('plural forms', () => {
318288
it('should pass when all plural forms have correct placeholders from msgid_plural', () => {
319-
const batch = [{
320-
msgid: '%d entry found',
321-
msgid_plural: '%d entries found',
322-
}];
323-
324-
const forms = [
325-
'%d запись найдена',
326-
'%d записи найдены',
327-
'%d записей найдено',
289+
const batch = [
290+
{
291+
msgid: '%d entry found',
292+
msgid_plural: '%d entries found',
293+
},
328294
];
329295

296+
const forms = ['%d запись найдена', '%d записи найдены', '%d записей найдено'];
297+
330298
const { translations } = parseXmlResponse(xml([forms]), batch, 3, mockLogger);
331299

332300
expect(translations[0].msgstr[0]).toBe('%d запись найдена');
@@ -338,13 +306,15 @@ describe('placeholder validation', () => {
338306
// In Arabic, form 0 is the "zero" form; in Japanese, form 0 is the
339307
// only form. For languages where singular form doesn't use a number,
340308
// the placeholder may be absent.
341-
const batch = [{
342-
msgid: '%d entry',
343-
msgid_plural: '%d entries',
344-
}];
309+
const batch = [
310+
{
311+
msgid: '%d entry',
312+
msgid_plural: '%d entries',
313+
},
314+
];
345315

346316
const forms = [
347-
'запись', // Form 0: no %d (language says "one entry" without number).
317+
'запись', // Form 0: no %d (language says "one entry" without number).
348318
'%d записи',
349319
'%d записей',
350320
];
@@ -359,16 +329,15 @@ describe('placeholder validation', () => {
359329

360330
it('should pass when plural form 0 matches plural placeholders instead of singular', () => {
361331
// Form 0 can also match the plural's placeholders.
362-
const batch = [{
363-
msgid: '%d item in %s',
364-
msgid_plural: '%d items in %s',
365-
}];
366-
367-
const forms = [
368-
'%d элемент в %s',
369-
'%d элемента в %s',
332+
const batch = [
333+
{
334+
msgid: '%d item in %s',
335+
msgid_plural: '%d items in %s',
336+
},
370337
];
371338

339+
const forms = ['%d элемент в %s', '%d элемента в %s'];
340+
372341
const { translations } = parseXmlResponse(xml([forms]), batch, 2, mockLogger);
373342

374343
expect(translations[0].msgstr[0]).toBe('%d элемент в %s');
@@ -377,41 +346,45 @@ describe('placeholder validation', () => {
377346

378347
it('should blank plural form 1+ when placeholder is missing', () => {
379348
const { logger, warnings } = createSpyLogger();
380-
const batch = [{
381-
msgid: '%d entry',
382-
msgid_plural: '%d entries',
383-
}];
349+
const batch = [
350+
{
351+
msgid: '%d entry',
352+
msgid_plural: '%d entries',
353+
},
354+
];
384355

385356
const forms = [
386357
'%d запись',
387-
'записи', // Form 1: missing %d.
358+
'записи', // Form 1: missing %d.
388359
'%d записей',
389360
];
390361

391362
const { translations } = parseXmlResponse(xml([forms]), batch, 3, logger);
392363

393364
expect(translations[0].msgstr[0]).toBe('%d запись');
394-
expect(translations[0].msgstr[1]).toBe(''); // Blanked.
365+
expect(translations[0].msgstr[1]).toBe(''); // Blanked.
395366
expect(translations[0].msgstr[2]).toBe('%d записей');
396367
expect(warnings.some((w) => w.includes('form 1'))).toBe(true);
397368
});
398369

399370
it('should blank plural form 0 when it has extra placeholders', () => {
400-
const { logger, warnings } = createSpyLogger();
401-
const batch = [{
402-
msgid: '%d entry',
403-
msgid_plural: '%d entries',
404-
}];
371+
const { logger } = createSpyLogger();
372+
const batch = [
373+
{
374+
msgid: '%d entry',
375+
msgid_plural: '%d entries',
376+
},
377+
];
405378

406379
const forms = [
407-
'%d %s запись', // Form 0: has extra %s.
380+
'%d %s запись', // Form 0: has extra %s.
408381
'%d записи',
409382
'%d записей',
410383
];
411384

412385
const { translations } = parseXmlResponse(xml([forms]), batch, 3, logger);
413386

414-
expect(translations[0].msgstr[0]).toBe(''); // Blanked.
387+
expect(translations[0].msgstr[0]).toBe(''); // Blanked.
415388
expect(translations[0].msgstr[1]).toBe('%d записи');
416389
expect(translations[0].msgstr[2]).toBe('%d записей');
417390
});
@@ -421,21 +394,15 @@ describe('placeholder validation', () => {
421394
it('should increment stringsWithPluralIssues when placeholder mismatch occurs', () => {
422395
const batch = [{ msgid: 'Hello %s' }];
423396

424-
const { validationStats } = parseXmlResponse(
425-
xml(['Bonjour']),
426-
batch, 1, mockLogger,
427-
);
397+
const { validationStats } = parseXmlResponse(xml(['Bonjour']), batch, 1, mockLogger);
428398

429399
expect(validationStats.stringsWithPluralIssues).toBeGreaterThan(0);
430400
});
431401

432402
it('should not increment stats when placeholders match', () => {
433403
const batch = [{ msgid: 'Hello %s' }];
434404

435-
const { validationStats } = parseXmlResponse(
436-
xml(['Bonjour %s']),
437-
batch, 1, mockLogger,
438-
);
405+
const { validationStats } = parseXmlResponse(xml(['Bonjour %s']), batch, 1, mockLogger);
439406

440407
expect(validationStats.stringsWithPluralIssues).toBe(0);
441408
});
@@ -460,17 +427,11 @@ describe('placeholder validation', () => {
460427
});
461428

462429
it('should handle multiple entries in a batch independently', () => {
463-
const { logger, warnings } = createSpyLogger();
464-
const batch = [
465-
{ msgid: 'Hello %s' },
466-
{ msgid: 'Count: %d' },
467-
];
430+
const { logger } = createSpyLogger();
431+
const batch = [{ msgid: 'Hello %s' }, { msgid: 'Count: %d' }];
468432

469433
// First translation is correct, second is missing %d.
470-
const { translations } = parseXmlResponse(
471-
xml(['Bonjour %s', 'Счет:']),
472-
batch, 1, logger,
473-
);
434+
const { translations } = parseXmlResponse(xml(['Bonjour %s', 'Счет:']), batch, 1, logger);
474435

475436
expect(translations[0].msgstr[0]).toBe('Bonjour %s');
476437
expect(translations[1].msgstr[0]).toBe('');

0 commit comments

Comments
 (0)