@@ -17,6 +17,7 @@ import {
1717 holesOf ,
1818 PACK_HOOK ,
1919 PACK_NAMESPACE_DIRS ,
20+ PACK_NAMESPACE_FRAMES ,
2021 readVocabulary ,
2122 RESERVED_OPTION_NAMES ,
2223 unresolvableSpellings ,
@@ -302,6 +303,120 @@ describe('the pack-shaped namespaces are part of the en pack (objectstack#30588)
302303 } ) ;
303304} ) ;
304305
306+ describe ( 'a key only a namespace defines is refused outside its frame (objectstack#30645)' , ( ) => {
307+ const NS_EN = `${ PACK_NAMESPACE_DIRS [ 0 ] } en.ts` ;
308+ const FRAME = PACK_NAMESPACE_FRAMES [ PACK_NAMESPACE_DIRS [ 0 ] ] [ 0 ] ;
309+ const NS_PACK =
310+ "const en = { shop: { title: 'Shop', row_one: '{{count}} row', row_other: '{{count}} rows', " +
311+ "status: { open: 'Open', closed: 'Closed' } } } as const;\nexport default en;\n" ;
312+ const RESIDENT = "const en = { shop: { kept: 'Kept', tag: { a: 'A' } } } as const;\nexport default en;\n" ;
313+ const page = ( body : string ) =>
314+ `import { useObjectTranslation } from '${ I18N_PKG } ';\nexport function P({ s }: { s: string }) { const { t } = useObjectTranslation(); return ${ body } ; }\n` ;
315+ const outsideFrame = ( root : string ) => findingsOf ( root , 'namespace-key-outside-frame' ) ;
316+
317+ it ( 'every namespace directory is declared with a non-empty frame (one list, read both ways)' , ( ) => {
318+ expect ( Object . keys ( PACK_NAMESPACE_FRAMES ) ) . toEqual ( [ ...PACK_NAMESPACE_DIRS ] ) ;
319+ for ( const dir of PACK_NAMESPACE_DIRS ) expect ( PACK_NAMESPACE_FRAMES [ dir ] . length ) . toBeGreaterThan ( 0 ) ;
320+ } ) ;
321+
322+ it ( 'turns red on a namespace-only key spelled outside the frame (the positive control)' , ( ) => {
323+ const root = repoWith ( {
324+ 'packages/i18n/src/locales/en.ts' : RESIDENT ,
325+ [ NS_EN ] : NS_PACK ,
326+ 'packages/studio/src/page.tsx' : page ( "t('shop.title')" ) ,
327+ } ) ;
328+ // fixture-address: the finding the gate reports for the fixture page written just above
329+ expect ( outsideFrame ( root ) ) . toEqual ( [ 'shop.title@packages/studio/src/page.tsx:2' ] ) ;
330+ const finding = analyze ( root , { families : [ ] } ) . findings . find (
331+ ( f : { reason : string } ) => f . reason === 'namespace-key-outside-frame' ,
332+ ) ;
333+ expect ( finding ?. expected ) . toBe ( PACK_NAMESPACE_DIRS [ 0 ] ) ;
334+ } ) ;
335+
336+ it ( 'reads the same key inside the frame, and counts it there' , ( ) => {
337+ const root = repoWith ( {
338+ 'packages/i18n/src/locales/en.ts' : RESIDENT ,
339+ [ NS_EN ] : NS_PACK ,
340+ [ `${ FRAME } Page.tsx` ] : page ( "t('shop.title')" ) ,
341+ } ) ;
342+ expect ( outsideFrame ( root ) ) . toEqual ( [ ] ) ;
343+ expect ( analyze ( root , { families : [ ] } ) . framedReads . get ( PACK_NAMESPACE_DIRS [ 0 ] ) ) . toBe ( 1 ) ;
344+ } ) ;
345+
346+ it ( 'stays silent on a resident key outside the frame' , ( ) => {
347+ const root = repoWith ( {
348+ 'packages/i18n/src/locales/en.ts' : RESIDENT ,
349+ [ NS_EN ] : NS_PACK ,
350+ 'packages/studio/src/page.tsx' : page ( "t('shop.kept')" ) ,
351+ } ) ;
352+ expect ( outsideFrame ( root ) ) . toEqual ( [ ] ) ;
353+ expect ( analyze ( root , { families : [ ] } ) . framedReads . get ( PACK_NAMESPACE_DIRS [ 0 ] ) ) . toBe ( 0 ) ;
354+ } ) ;
355+
356+ it ( 'refuses a plural base whose forms live only in the namespace' , ( ) => {
357+ const root = repoWith ( {
358+ 'packages/i18n/src/locales/en.ts' : RESIDENT ,
359+ [ NS_EN ] : NS_PACK ,
360+ 'packages/studio/src/page.tsx' : page ( "t('shop.row', { count: 2 })" ) ,
361+ } ) ;
362+ // fixture-address: the finding the gate reports for the fixture page written just above
363+ expect ( outsideFrame ( root ) ) . toEqual ( [ 'shop.row@packages/studio/src/page.tsx:2' ] ) ;
364+ } ) ;
365+
366+ it ( 'refuses a template head only the namespace answers, and passes one a resident leaf answers' , ( ) => {
367+ const root = repoWith ( {
368+ 'packages/i18n/src/locales/en.ts' : RESIDENT ,
369+ [ NS_EN ] : NS_PACK ,
370+ 'packages/studio/src/a.tsx' : page ( 't(`shop.status.${s}`)' ) ,
371+ 'packages/studio/src/b.tsx' : page ( 't(`shop.tag.${s}`)' ) ,
372+ } ) ;
373+ // fixture-address: the finding the gate reports for the fixture page written just above
374+ expect ( outsideFrame ( root ) ) . toEqual ( [ 'shop.status.*@packages/studio/src/a.tsx:2' ] ) ;
375+ } ) ;
376+
377+ it ( 'refuses a returnObjects subtree that only the namespace fills' , ( ) => {
378+ const root = repoWith ( {
379+ 'packages/i18n/src/locales/en.ts' : RESIDENT ,
380+ [ NS_EN ] : NS_PACK ,
381+ 'packages/studio/src/page.tsx' : page ( "t('shop.status', { returnObjects: true })" ) ,
382+ } ) ;
383+ // fixture-address: the finding the gate reports for the fixture page written just above
384+ expect ( outsideFrame ( root ) ) . toEqual ( [ 'shop.status.*@packages/studio/src/page.tsx:2' ] ) ;
385+ } ) ;
386+
387+ it ( 'a second namespace inherits the rule from its declaration alone' , ( ) => {
388+ const SECOND = 'packages/i18n/src/namespaces/billing/' ;
389+ const frames = { ...PACK_NAMESPACE_FRAMES , [ SECOND ] : [ 'packages/app-shell/src/console/billing/' ] } ;
390+ const root = repoWith ( {
391+ 'packages/i18n/src/locales/en.ts' : RESIDENT ,
392+ [ `${ SECOND } en.ts` ] : "const en = { billing: { title: 'Billing' } } as const;\nexport default en;\n" ,
393+ 'packages/app-shell/src/console/billing/Page.tsx' : page ( "t('billing.title')" ) ,
394+ 'packages/studio/src/page.tsx' : page ( "t('billing.title')" ) ,
395+ } ) ;
396+ const result = analyze ( root , { families : [ ] , frames } ) ;
397+ const refused = result . findings
398+ . filter ( ( f : { reason : string } ) => f . reason === 'namespace-key-outside-frame' )
399+ . map ( ( f : { detail : string ; file : string ; expected : string } ) => `${ f . detail } @${ f . file } <- ${ f . expected } ` ) ;
400+ expect ( refused ) . toEqual ( [ `billing.title@packages/studio/src/page.tsx <- ${ SECOND } ` ] ) ;
401+ expect ( result . framedReads . get ( SECOND ) ) . toBe ( 1 ) ;
402+ } ) ;
403+
404+ it ( 'refuses a namespace declared without a frame' , ( ) => {
405+ const root = repoWith ( { 'packages/i18n/src/locales/en.ts' : RESIDENT } ) ;
406+ expect ( ( ) => analyze ( root , { families : [ ] , frames : { 'packages/i18n/src/namespaces/billing/' : [ ] } } ) ) . toThrow (
407+ / d e c l a r e s n o f r a m e / ,
408+ ) ;
409+ } ) ;
410+
411+ it ( 'on this tree: no namespace key is read outside its frame, and every frame reads some' , ( ) => {
412+ const outside = REPO_ANALYSIS . findings . filter (
413+ ( f : { reason : string } ) => f . reason === 'namespace-key-outside-frame' ,
414+ ) ;
415+ expect ( outside ) . toEqual ( [ ] ) ;
416+ for ( const dir of PACK_NAMESPACE_DIRS ) expect ( REPO_ANALYSIS . framedReads . get ( dir ) ) . toBeGreaterThan ( 0 ) ;
417+ } ) ;
418+ } ) ;
419+
305420describe ( 'the file walk' , ( ) => {
306421 it ( 'reads sources but not tests, type declarations or build output' , ( ) => {
307422 const root = repoWith ( {
0 commit comments