Skip to content

Commit 7b66b10

Browse files
committed
Infer mapping declaration value types from parsed data
Assisted-By: GPT-6
1 parent 516c94e commit 7b66b10

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎scripts/utils.mjs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,14 @@ const writeFiles = (options) => {
209209
}
210210
} else {
211211
const symbols = new Map();
212+
const valueTypes = new Set();
212213
for (let [from, to] of codePoints) {
213214
from = String.fromCodePoint(from);
214215
if (Array.isArray(to)) {
216+
valueTypes.add('number[]');
215217
to = String.fromCodePoint.apply(null, to);
216218
} else {
219+
valueTypes.add('number');
217220
to = String.fromCodePoint(to);
218221
}
219222
symbols.set(from, to);
@@ -225,15 +228,7 @@ const writeFiles = (options) => {
225228
codePointsFileContent = `import { gunzipSync } from 'node:zlib';\n\nexport default ${gzipInline(codePoints)};\n`;
226229
symbolsFileContent = `import { gunzipSync } from 'node:zlib';\n\nexport default ${gzipInline(symbols)};\n`;
227230
}
228-
if (
229-
(type === 'Case_Folding' && item === 'F') ||
230-
type === 'Special_Casing' ||
231-
type === 'Decomposition_Mapping'
232-
) {
233-
codePointsType = 'Map<number, number[]>';
234-
} else {
235-
codePointsType = 'Map<number, number>';
236-
}
231+
codePointsType = `Map<number, ${[...valueTypes].sort().join(' | ') || 'number'}>`;
237232
symbolsType = 'Map<string, string>';
238233
}
239234
writeFileIfChanged(

‎tests/parsers.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { test } from 'node:test';
2+
import fs from 'node:fs';
3+
import parseCaseFolding from '../scripts/parse-case-folding.mjs';
4+
import utils from '../scripts/utils.mjs';
25
import parseDecompositionMapping from '../scripts/parse-decomposition-mapping.mjs';
36
import parseIndicConjunctBreak from '../scripts/parse-indic-conjunct-break.mjs';
47
import parseJoiningType from '../scripts/parse-joining-type.mjs';
@@ -22,6 +25,21 @@ test('Hangul has pairwise canonical decomposition mappings', async (t) => {
2225
t.assert.equal(await parseDecompositionMapping('1.1.5'), undefined);
2326
});
2427

28+
test('Case folding declarations describe scalar and expansion values', async (t) => {
29+
const map = await parseCaseFolding('3.0.1');
30+
t.assert.equal(map.E.get(0xb5), 0x3bc);
31+
t.assert.deepEqual(map.E.get(0xdf), [0x73, 0x73]);
32+
utils.writeFiles({ version: 'parser-types-test', type: 'Case_Folding', map });
33+
const declaration = fs.readFileSync(
34+
new URL(
35+
'../output/unicode-parser-types-test/Case_Folding/E/code-points.d.mts',
36+
import.meta.url,
37+
),
38+
'utf8',
39+
);
40+
t.assert.match(declaration, /Map<number, number \| number\[\]>/);
41+
});
42+
2543
test('ArabicShaping fallback derives implicit joining types', async (t) => {
2644
for (const version of ['3.2.0', '4.0.0', '4.0.1']) {
2745
const map = await parseJoiningType(version);

0 commit comments

Comments
 (0)