From 833cc691a29a13094deaf1f49b251437387a38be Mon Sep 17 00:00:00 2001 From: Doxa Maps Date: Sun, 27 Sep 2026 04:54:30 +0000 Subject: [PATCH] The locale warn was flagging the code that honours the locale LOC-ENUS flagged any en-US format literal. Two of regions-dashboard's three warns were the 'en-US' inside a CATCH, whose try formats with `${locale}-u-nu-latn` -- a verbatim clone of DX-life's RegionDetail.vue:16, Latin digits with the reader's grouping. The fallback exists because a bad locale tag throws RangeError in toLocaleString and blanking the page is worse than one wrong separator. So the matrix's locale column said "ignores the locale" about code that honours it, which is the kind of false warn that teaches people to stop reading the column. Measured before changing the rule: of the NINE LOC-ENUS sites in the tree, exactly TWO are such fallbacks. The other seven are genuinely operative and still flagged, so the tree goes 10 warns -> 8 and only those two move. The exemption needs BOTH halves -- a catch AND a locale-derived primary -- because a lone catch must not excuse a format that never consulted the locale. That is the one rule here that can widen by accident, so four adversarial shapes are pinned as a self-test: the real pattern is skipped, while a catch around a hardcoded primary, a catch whose primary never consulted a locale, and a plain operative call are all still caught. Loosening the exemption to a bare catch makes the self-test exit 3. regions-dashboard keeps ONE warn: RegionCountryMap's popup, fixed en-US on purpose, carded and evidenced by DX-life's own convention. I did not add an ALLOW for it -- ALLOW is keyed per BUNDLE, so it would also blind regions to a future operative en-US, and a visible warn on a decision already taken is cheaper than a blind spot. --- internal/check-universal-props.mjs | 45 +++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/internal/check-universal-props.mjs b/internal/check-universal-props.mjs index d1b10ab1d..d9334bdf4 100644 --- a/internal/check-universal-props.mjs +++ b/internal/check-universal-props.mjs @@ -83,7 +83,31 @@ const RULES = [ { id: 'ENG-PIN', prop: 'engine', re: /useMapInstance\s*\(\s*\{[^}]{0,400}?provider\s*:\s*['"](?:mapbox|maplibre)['"]/g }, { id: 'TOK-GLOBAL', prop: 'token', re: /window\.DOXA_TOKEN\b/g, shipped: 'DOXA_TOKEN' }, { id: 'TOK-LITERAL', prop: 'token', re: /['"`]pk\.ey[A-Za-z0-9_-]{20,}/g, shipped: /pk\.eyJ[A-Za-z0-9_-]{20,}/ }, - { id: 'LOC-ENUS', prop: 'locale', warn: true, re: /(?:toLocaleString|NumberFormat|DateTimeFormat)\s*\(\s*['"]en(?:-US)?['"]/g }, + { id: 'LOC-ENUS', prop: 'locale', warn: true, test(src) { + // ★ A CATCH-BLOCK FALLBACK IS NOT A FIXED FORMAT, and the bare regex was WARNING THE CORRECT + // PATTERN. regions formats with `${locale}-u-nu-latn` -- a verbatim clone of DX-life's + // RegionDetail.vue:16, Latin digits with the reader's grouping -- and falls back to 'en-US' only + // inside a catch, because a bad locale tag throws RangeError in toLocaleString and blanking the + // page is worse than one wrong separator. Flagging that told the matrix's locale column to say + // "ignores the locale" about code that honours it, which is the kind of false warn that teaches + // people to stop reading the column. + // MEASURED before changing the rule: of the NINE LOC-ENUS sites in the tree, exactly TWO are + // such fallbacks (region-dashboards.vue, regions-list.vue). The other seven are genuinely + // operative fixed en-US and are still flagged, including RegionCountryMap's popup, which is a + // HELD Driver decision with its own card rather than an oversight. + const out = [] + const re = /(?:toLocaleString|NumberFormat|DateTimeFormat)\s*\(\s*['"]en(?:-US)?['"]/g + let m + while ((m = re.exec(src))) { + const before = src.slice(src.lastIndexOf('\n', m.index) + 1, m.index) + // both halves required: a catch, AND a locale-DERIVED format call it is the fallback for. + // A lone `catch` nearby must not excuse a hardcoded format that never consulted the locale. + const isFallback = /\bcatch\b/.test(before) + && /(?:toLocaleString|NumberFormat|DateTimeFormat)\s*\(\s*`[^`]*\$\{/.test(before) + if (!isFallback) out.push(m.index) + } + return out + } }, // A whole-file rule: the fetch and the helper need not be on the same line, only in the same file. { id: 'LIST-FETCH', prop: 'list', test(src) { const m = /\/api\/people-groups\/list|endpoints\?\.bulk|doxa-people-groups\.json/.exec(src) @@ -111,10 +135,29 @@ const CLEAN = `map.setStyle(resolveNamedStyle('ink', p).style || 'mapbox://style useMapInstance({ container: el, style: cfg.style ?? 'mapbox://styles/mapbox/light-v11' }) // new mapboxgl.Popup() in a comment is not code n.toLocaleString(locale.value) + try { return v.toLocaleString(\`\${locale.value || 'en'}-u-nu-latn\`) } catch (_) { return v.toLocaleString('en-US') } const r2 = await applyPeopleGroupList((await fetch(base + '/api/people-groups/list')).posts, cfg) const dsm2 = new DataSourceManager({ locale: 'en', profileConfig }) const INHERITED_PAGE_KEYS = ['locale', 'provider', 'list']` const falsePos = RULES.filter((r) => run(r, stripComments(CLEAN)).length).map((r) => r.id) + +// ── LOC-ENUS discrimination, tested on purpose ────────────────────────────────────────────────── +// The fallback exemption is the one rule here that can WIDEN by accident: loosen it and a hardcoded +// format with a catch anywhere near it stops being seen. So the three adversarial shapes are pinned. +const locRule = RULES.find((r) => r.id === 'LOC-ENUS') +const LOC_CASES = [ + [false, "try { return v.toLocaleString(`${locale.value || 'en'}-u-nu-latn`) } catch (_) { return v.toLocaleString('en-US') }"], + [true, "try { return v.toLocaleString('en-US') } catch (_) { return '' }"], + [true, "try { return fmt(v) } catch (_) { return v.toLocaleString('en-US') }"], + [true, "const pop = Number(p.population).toLocaleString('en-US')"], +] +const locWrong = LOC_CASES.filter(([shouldFlag, line]) => (run(locRule, line).length > 0) !== shouldFlag) +if (locWrong.length) { + console.error(`UNIVERSAL-PROPS: SELF-TEST FAILED — LOC-ENUS misjudged ${locWrong.length} case(s); ` + + 'the catch-fallback exemption must skip ONLY a locale-derived primary: ' + + locWrong.map(([f, l]) => `${f ? 'missed' : 'false-flagged'}: ${l.slice(0, 56)}`).join(' | ')) + process.exit(3) +} if (missed.length || falsePos.length) { console.error(`UNIVERSAL-PROPS: SELF-TEST FAILED — ${missed.length ? 'rules blind to a planted bypass: ' + missed.join(', ') : ''}${falsePos.length ? ' false positives on clean code: ' + falsePos.join(', ') : ''}`) process.exit(3)