Skip to content

Commit 17a66dd

Browse files
committed
fix(i18n): --missing summary counts the baseline identity, not raw draws
The tail line printed `entries.length` (63 on main) while `--missing --json` and `--write` print and pin `baselineOf(entries)` (61), so a reader checking the count against the baseline file saw a false difference of 2. Compute the identity once and use it for the JSON, the write and the summary; the walk still lists every draw, and two attributes drawing one sentence at one position stay one pin. Fixes #3017
1 parent 0826c1f commit 17a66dd

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎core/test/i18n-missing-entries.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
// pin is a sentence that already existed on main untranslated, and each leaves the baseline when
2424
// its zh entry lands — never by editing the baseline. A new untranslated draw still fails.
2525
import assert from 'node:assert/strict'
26+
import { spawnSync } from 'node:child_process'
2627
import { readFileSync } from 'node:fs'
2728
import { test } from 'node:test'
29+
import { fileURLToPath } from 'node:url'
2830
import { baselineOf, missingCopy, scanMissing } from '../../scripts/i18n-usage.mjs'
2931

3032
const baselineFile = new URL('./i18n-missing-entries.baseline.json', import.meta.url)
@@ -104,3 +106,24 @@ test('the widened scan names every copy attribute and skips placeholder examples
104106
['https://example.org/room/42'],
105107
)
106108
})
109+
110+
// The same sentence drawn twice at one position — title: and aria-label: on one line — is one pin,
111+
// because `baselineOf` keys a draw by `file:line:key`. The tail line is what a reader compares with
112+
// the baseline file, so it must report that identity, the same number `--missing --json` prints and
113+
// `--write` pins, not the raw draw array. On main the raw count reads 63 while the JSON and the
114+
// file hold 61 (#3017).
115+
test('--missing prints the identity count, not the raw draw count', () => {
116+
const script = fileURLToPath(new URL('../../scripts/i18n-usage.mjs', import.meta.url))
117+
const run = (...args) => {
118+
const result = spawnSync(process.execPath, [script, ...args], { encoding: 'utf8' })
119+
assert.equal(result.status, 0, result.stderr)
120+
return result.stdout
121+
}
122+
const counted = JSON.parse(run('--missing', '--json')).entries.length
123+
const tail = run('--missing').trimEnd().split('\n').at(-1)
124+
assert.equal(Number.parseInt(tail, 10), counted, `the summary line must report what --json holds: ${tail}`)
125+
// Two draws, one pin: that is why the raw array and the identity disagree on main.
126+
const draws = missingCopy("h(Button, { title: 'A new untranslated line.', 'aria-label': 'A new untranslated line.' })", 'plugins/sample/client.js')
127+
assert.equal(draws.length, 2)
128+
assert.equal(baselineOf(draws).length, 1)
129+
})

‎patches/ds-bot/files/test/i18n-missing-entries.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
// pin is a sentence that already existed on main untranslated, and each leaves the baseline when
2424
// its zh entry lands — never by editing the baseline. A new untranslated draw still fails.
2525
import assert from 'node:assert/strict'
26+
import { spawnSync } from 'node:child_process'
2627
import { readFileSync } from 'node:fs'
2728
import { test } from 'node:test'
29+
import { fileURLToPath } from 'node:url'
2830
import { baselineOf, missingCopy, scanMissing } from '../../scripts/i18n-usage.mjs'
2931

3032
const baselineFile = new URL('./i18n-missing-entries.baseline.json', import.meta.url)
@@ -104,3 +106,24 @@ test('the widened scan names every copy attribute and skips placeholder examples
104106
['https://example.org/room/42'],
105107
)
106108
})
109+
110+
// The same sentence drawn twice at one position — title: and aria-label: on one line — is one pin,
111+
// because `baselineOf` keys a draw by `file:line:key`. The tail line is what a reader compares with
112+
// the baseline file, so it must report that identity, the same number `--missing --json` prints and
113+
// `--write` pins, not the raw draw array. On main the raw count reads 63 while the JSON and the
114+
// file hold 61 (#3017).
115+
test('--missing prints the identity count, not the raw draw count', () => {
116+
const script = fileURLToPath(new URL('../../scripts/i18n-usage.mjs', import.meta.url))
117+
const run = (...args) => {
118+
const result = spawnSync(process.execPath, [script, ...args], { encoding: 'utf8' })
119+
assert.equal(result.status, 0, result.stderr)
120+
return result.stdout
121+
}
122+
const counted = JSON.parse(run('--missing', '--json')).entries.length
123+
const tail = run('--missing').trimEnd().split('\n').at(-1)
124+
assert.equal(Number.parseInt(tail, 10), counted, `the summary line must report what --json holds: ${tail}`)
125+
// Two draws, one pin: that is why the raw array and the identity disagree on main.
126+
const draws = missingCopy("h(Button, { title: 'A new untranslated line.', 'aria-label': 'A new untranslated line.' })", 'plugins/sample/client.js')
127+
assert.equal(draws.length, 2)
128+
assert.equal(baselineOf(draws).length, 1)
129+
})

‎scripts/i18n-usage.mjs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,14 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
466466
const missing = process.argv.includes('--missing')
467467
const entries = missing ? scanMissing() : scanPlugins()
468468
const identity = missing ? baselineOf : drawBaselineOf
469+
// One identity for every reader of this run: `--json` prints it, `--write` pins it and the tail
470+
// line counts it, so the count can never disagree with the file a reader compares it with. The
471+
// walk still lists each draw (two attributes on one line are two draws); the count is pins, and a
472+
// `file:line:key` is pinned once (#3017).
473+
const counted = identity(entries)
469474
const write = flagValue('--write')
470475
if (process.argv.includes('--json')) {
471-
console.log(JSON.stringify({ version: 1, entries: identity(entries) }, null, 2))
476+
console.log(JSON.stringify({ version: 1, entries: counted }, null, 2))
472477
} else if (write) {
473478
const result = writeBaseline(write, entries, { force: process.argv.includes('--force'), identity })
474479
if (!result.written) {
@@ -482,6 +487,6 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
482487
} else {
483488
for (const entry of entries) console.log(`${entry.file}:${entry.line}: ${JSON.stringify(entry.key)}`)
484489
const what = missing ? 'drawn copy strings with no zh entry' : 'bare draws'
485-
console.log(`${entries.length} ${what} in ${new Set(entries.map(entry => entry.file)).size} files`)
490+
console.log(`${counted.length} ${what} in ${new Set(entries.map(entry => entry.file)).size} files`)
486491
}
487492
}

0 commit comments

Comments
 (0)