@@ -17,6 +17,7 @@ import {
1717 isFileReadable ,
1818 mergeWithDefaults ,
1919 normalizePath ,
20+ numberToPos ,
2021 posToNumber ,
2122 processSrcSetSync ,
2223 resolveHostname ,
@@ -245,6 +246,34 @@ describe('posToNumber', () => {
245246 } )
246247} )
247248
249+ describe ( 'numberToPos' , ( ) => {
250+ test ( 'simple' , ( ) => {
251+ const actual = numberToPos ( 'a\nb' , 2 )
252+ expect ( actual ) . toEqual ( { line : 2 , column : 0 } )
253+ } )
254+ test ( 'pass though pos' , ( ) => {
255+ const actual = numberToPos ( 'a\nb' , { line : 2 , column : 0 } )
256+ expect ( actual ) . toEqual ( { line : 2 , column : 0 } )
257+ } )
258+ test ( 'empty line' , ( ) => {
259+ const actual = numberToPos ( 'a\n\nb' , 3 )
260+ expect ( actual ) . toEqual ( { line : 3 , column : 0 } )
261+ } )
262+ test ( 'middle of line' , ( ) => {
263+ const actual = numberToPos ( 'abc\ndef' , 5 )
264+ expect ( actual ) . toEqual ( { line : 2 , column : 1 } )
265+ } )
266+ test ( 'end of line' , ( ) => {
267+ const actual = numberToPos ( 'abc\ndef' , 3 )
268+ expect ( actual ) . toEqual ( { line : 1 , column : 3 } )
269+ } )
270+ test ( 'out of range' , ( ) => {
271+ expect ( ( ) => numberToPos ( 'a\nb' , 5 ) ) . toThrowError (
272+ 'offset is longer than source length' ,
273+ )
274+ } )
275+ } )
276+
248277describe ( 'generateCodeFrames' , ( ) => {
249278 const source = `
250279import foo from './foo'
0 commit comments