@@ -461,6 +461,85 @@ definition user {}
461461 expect ( rightExpr . relationName ) . toEqual ( "d" ) ;
462462 } ) ;
463463
464+ it ( "parses definition with a self permission" , ( ) => {
465+ const schema = `definition foo {
466+ relation viewer: bar
467+ permission view = viewer + self
468+ }` ;
469+ const parsed = parseSchema ( schema ) ;
470+
471+ expect ( parsed ?. definitions . length ) . toEqual ( 1 ) ;
472+
473+ const definition = parsed ?. definitions [ 0 ] ;
474+ assert ( definition ) ;
475+ assert ( definition . kind === "objectDef" ) ;
476+ expect ( definition . permissions ) . toHaveLength ( 1 ) ;
477+
478+ const permission = definition . permissions [ 0 ] ;
479+ assert ( permission ) ;
480+ expect ( permission . name ) . toEqual ( "view" ) ;
481+
482+ const binExpr = permission . expr ;
483+ assert ( binExpr . kind === "binary" ) ;
484+ expect ( binExpr . operator ) . toEqual ( "union" ) ;
485+
486+ const leftExpr = binExpr . left ;
487+ assert ( leftExpr . kind === "relationref" ) ;
488+ expect ( leftExpr . relationName ) . toEqual ( "viewer" ) ;
489+
490+ const rightExpr = binExpr . right ;
491+ assert ( rightExpr . kind === "self" ) ;
492+ } ) ;
493+
494+ it ( "parses definition with a standalone self permission" , ( ) => {
495+ const schema = `definition foo {
496+ permission view = self
497+ }` ;
498+ const parsed = parseSchema ( schema ) ;
499+
500+ expect ( parsed ?. definitions . length ) . toEqual ( 1 ) ;
501+
502+ const definition = parsed ?. definitions [ 0 ] ;
503+ assert ( definition ) ;
504+ assert ( definition . kind === "objectDef" ) ;
505+ expect ( definition . permissions ) . toHaveLength ( 1 ) ;
506+
507+ const permission = definition . permissions [ 0 ] ;
508+ assert ( permission ) ;
509+ expect ( permission . name ) . toEqual ( "view" ) ;
510+
511+ const selfExpr = permission . expr ;
512+ assert ( selfExpr . kind === "self" ) ;
513+ } ) ;
514+
515+ it ( "parses definition with self in a complex expression" , ( ) => {
516+ const schema = `definition foo {
517+ permission first = ((a - b) + self) & d;
518+ }` ;
519+ const parsed = parseSchema ( schema ) ;
520+
521+ expect ( parsed ?. definitions . length ) . toEqual ( 1 ) ;
522+
523+ const definition = parsed ?. definitions [ 0 ] ;
524+ assert ( definition ) ;
525+ assert ( definition . kind === "objectDef" ) ;
526+ expect ( definition . permissions ) . toHaveLength ( 1 ) ;
527+
528+ const permission = definition . permissions [ 0 ] ;
529+ assert ( permission ) ;
530+
531+ const binExpr = permission . expr ;
532+ assert ( binExpr . kind === "binary" ) ;
533+ expect ( binExpr . operator ) . toEqual ( "intersection" ) ;
534+
535+ const leftExpr = binExpr . left ;
536+ assert ( leftExpr . kind === "binary" ) ;
537+ expect ( leftExpr . operator ) . toEqual ( "union" ) ;
538+
539+ const rightLeftExpr = leftExpr . right ;
540+ assert ( rightLeftExpr . kind === "self" ) ;
541+ } ) ;
542+
464543 it ( "parses definition with multiple permissions" , ( ) => {
465544 const schema = `definition foo {
466545 permission first = firstrel
0 commit comments