@@ -7,6 +7,14 @@ async function t(comment: string) {
77 return annotations [ 0 ]
88}
99
10+ function getBlockRange ( annotation : { ranges : any [ ] } , index = 0 ) {
11+ const range = annotation . ranges [ index ]
12+ if ( ! ( "fromLineNumber" in range ) || ! ( "toLineNumber" in range ) ) {
13+ throw new Error ( "Expected block range" )
14+ }
15+ return range
16+ }
17+
1018test ( "extracts basic annotation name" , async ( ) => {
1119 const annotation = await t ( "!foo bar" )
1220 expect ( annotation . name ) . toEqual ( "foo" )
@@ -80,7 +88,7 @@ test("start/end creates block annotation spanning the range", async () => {
8088 const a = annotations [ 0 ]
8189 expect ( a . name ) . toEqual ( "focus" )
8290 expect ( a . ranges ) . toHaveLength ( 1 )
83- const range = a . ranges [ 0 ]
91+ const range = getBlockRange ( a )
8492 // after removing 2 comment lines, the code is 4 lines
8593 // "let b = 2" is line 2, "let c = 3" is line 3
8694 expect ( range . fromLineNumber ) . toEqual ( 2 )
@@ -123,8 +131,9 @@ test("start/end works with other annotations", async () => {
123131 const focus = annotations . find ( ( a ) => a . name === "focus" )
124132 expect ( mark ) . toBeDefined ( )
125133 expect ( focus ) . toBeDefined ( )
126- expect ( focus ! . ranges [ 0 ] . fromLineNumber ) . toBeDefined ( )
127- expect ( focus ! . ranges [ 0 ] . toLineNumber ) . toBeDefined ( )
134+ const range = getBlockRange ( focus ! )
135+ expect ( range . fromLineNumber ) . toBeDefined ( )
136+ expect ( range . toLineNumber ) . toBeDefined ( )
128137} )
129138
130139test ( "multiple start/end pairs of same name" , async ( ) => {
@@ -148,8 +157,8 @@ test("multiple start/end pairs of same name", async () => {
148157 expect ( annotations [ 0 ] . name ) . toEqual ( "focus" )
149158 expect ( annotations [ 1 ] . name ) . toEqual ( "focus" )
150159 // The two ranges should not overlap
151- const r0 = annotations [ 0 ] . ranges [ 0 ]
152- const r1 = annotations [ 1 ] . ranges [ 0 ]
160+ const r0 = getBlockRange ( annotations [ 0 ] )
161+ const r1 = getBlockRange ( annotations [ 1 ] )
153162 expect ( r0 . toLineNumber ) . toBeLessThan ( r1 . fromLineNumber )
154163} )
155164
@@ -262,8 +271,9 @@ test("start/end works with Python comments", async () => {
262271 const { annotations } = await splitAnnotationsAndCode ( code , "python" , "!" )
263272 expect ( annotations ) . toHaveLength ( 1 )
264273 expect ( annotations [ 0 ] . name ) . toEqual ( "focus" )
265- expect ( annotations [ 0 ] . ranges [ 0 ] . fromLineNumber ) . toEqual ( 2 )
266- expect ( annotations [ 0 ] . ranges [ 0 ] . toLineNumber ) . toEqual ( 3 )
274+ const range = getBlockRange ( annotations [ 0 ] )
275+ expect ( range . fromLineNumber ) . toEqual ( 2 )
276+ expect ( range . toLineNumber ) . toEqual ( 3 )
267277} )
268278
269279test ( "start/end works with block comments" , async ( ) => {
0 commit comments