@@ -483,6 +483,22 @@ test('rangeBy() returns range for word', () => {
483483 } )
484484} )
485485
486+ test ( 'rangeBy() returns range for word when offsets are missing' , ( ) => {
487+ let css = parse ( 'a { one: X }' )
488+ let a = css . first as Rule
489+ let one = a . first as Declaration
490+
491+ // @ts -expect-error
492+ if ( one . source ?. start ) delete one . source . start . offset ;
493+ // @ts -expect-error
494+ if ( one . source ?. end ) delete one . source . end . offset ;
495+
496+ equal ( one . rangeBy ( { word : 'one' } ) , {
497+ end : { column : 9 , line : 1 } ,
498+ start : { column : 6 , line : 1 }
499+ } )
500+ } )
501+
486502test ( 'rangeBy() returns range for word even after AST mutations' , ( ) => {
487503 let css = parse ( 'a {\n\tone: 1;\n\ttwo: 2;}' )
488504 let a = css . first as Rule
@@ -510,6 +526,42 @@ test('rangeBy() returns range for word even after AST mutations', () => {
510526 } )
511527} )
512528
529+ test ( 'rangeBy() returns range for word even after AST mutations when offsets are missing' , ( ) => {
530+ let css = parse ( 'a {\n\tone: 1;\n\ttwo: 2;}' )
531+ let a = css . first as Rule
532+ let one = a . first as Declaration
533+ let two = one . next ( ) as Declaration
534+
535+ // @ts -expect-error
536+ if ( a . source ?. start ) delete a . source . start . offset ;
537+ // @ts -expect-error
538+ if ( a . source ?. end ) delete a . source . end . offset ;
539+ // @ts -expect-error
540+ if ( two . source ?. start ) delete two . source . start . offset ;
541+ // @ts -expect-error
542+ if ( two . source ?. end ) delete two . source . end . offset ;
543+
544+ equal ( a . rangeBy ( { word : 'two' } ) , {
545+ end : { column : 5 , line : 3 } ,
546+ start : { column : 2 , line : 3 }
547+ } )
548+ equal ( two . rangeBy ( { word : 'two' } ) , {
549+ end : { column : 5 , line : 3 } ,
550+ start : { column : 2 , line : 3 }
551+ } )
552+
553+ one . remove ( )
554+
555+ equal ( a . rangeBy ( { word : 'two' } ) , {
556+ end : { column : 5 , line : 3 } ,
557+ start : { column : 2 , line : 3 }
558+ } )
559+ equal ( two . rangeBy ( { word : 'two' } ) , {
560+ end : { column : 5 , line : 3 } ,
561+ start : { column : 2 , line : 3 }
562+ } )
563+ } )
564+
513565test ( 'rangeBy() returns range for index and endIndex' , ( ) => {
514566 let css = parse ( 'a { one: X }' )
515567 let a = css . first as Rule
@@ -520,6 +572,22 @@ test('rangeBy() returns range for index and endIndex', () => {
520572 } )
521573} )
522574
575+ test ( 'rangeBy() returns range for index and endIndex when offsets are missing' , ( ) => {
576+ let css = parse ( 'a { one: X }' )
577+ let a = css . first as Rule
578+ let one = a . first as Declaration
579+
580+ // @ts -expect-error
581+ if ( one . source ?. start ) delete one . source . start . offset ;
582+ // @ts -expect-error
583+ if ( one . source ?. end ) delete one . source . end . offset ;
584+
585+ equal ( one . rangeBy ( { endIndex : 3 , index : 1 } ) , {
586+ end : { column : 9 , line : 1 } ,
587+ start : { column : 7 , line : 1 }
588+ } )
589+ } )
590+
523591test ( 'rangeBy() returns range for index and endIndex after AST mutations' , ( ) => {
524592 let css = parse ( 'a {\n\tone: 1;\n\ttwo: 2;}' )
525593 let a = css . first as Rule
0 commit comments