@@ -104,6 +104,132 @@ describe("MarkdownSourceCode", () => {
104104 } ) ;
105105 } ) ;
106106
107+ describe ( "getLocFromIndex()" , ( ) => {
108+ it ( "should convert index to location correctly" , ( ) => {
109+ const text = "foo\nbar\r\nbaz" ;
110+ const markdownSourceCode = new MarkdownSourceCode ( {
111+ text,
112+ ast : fromMarkdown ( text ) ,
113+ } ) ;
114+
115+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 0 ) , {
116+ line : 1 ,
117+ column : 1 ,
118+ } ) ;
119+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 1 ) , {
120+ line : 1 ,
121+ column : 2 ,
122+ } ) ;
123+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 2 ) , {
124+ line : 1 ,
125+ column : 3 ,
126+ } ) ;
127+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 3 ) , {
128+ line : 1 ,
129+ column : 4 ,
130+ } ) ;
131+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 4 ) , {
132+ line : 2 ,
133+ column : 1 ,
134+ } ) ;
135+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 5 ) , {
136+ line : 2 ,
137+ column : 2 ,
138+ } ) ;
139+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 6 ) , {
140+ line : 2 ,
141+ column : 3 ,
142+ } ) ;
143+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 7 ) , {
144+ line : 2 ,
145+ column : 4 ,
146+ } ) ;
147+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 8 ) , {
148+ line : 2 ,
149+ column : 5 ,
150+ } ) ;
151+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 9 ) , {
152+ line : 3 ,
153+ column : 1 ,
154+ } ) ;
155+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 10 ) , {
156+ line : 3 ,
157+ column : 2 ,
158+ } ) ;
159+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 11 ) , {
160+ line : 3 ,
161+ column : 3 ,
162+ } ) ;
163+ assert . deepStrictEqual ( markdownSourceCode . getLocFromIndex ( 12 ) , {
164+ line : 3 ,
165+ column : 4 ,
166+ } ) ;
167+ } ) ;
168+ } ) ;
169+
170+ describe ( "getIndexFromLoc()" , ( ) => {
171+ it ( "should convert location to index correctly" , ( ) => {
172+ const text = "foo\nbar\r\nbaz" ;
173+ const markdownSourceCode = new MarkdownSourceCode ( {
174+ text,
175+ ast : fromMarkdown ( text ) ,
176+ } ) ;
177+
178+ assert . strictEqual (
179+ markdownSourceCode . getIndexFromLoc ( { line : 1 , column : 1 } ) ,
180+ 0 ,
181+ ) ;
182+ assert . strictEqual (
183+ markdownSourceCode . getIndexFromLoc ( { line : 1 , column : 2 } ) ,
184+ 1 ,
185+ ) ;
186+ assert . strictEqual (
187+ markdownSourceCode . getIndexFromLoc ( { line : 1 , column : 3 } ) ,
188+ 2 ,
189+ ) ;
190+ assert . strictEqual (
191+ markdownSourceCode . getIndexFromLoc ( { line : 1 , column : 4 } ) ,
192+ 3 ,
193+ ) ;
194+ assert . strictEqual (
195+ markdownSourceCode . getIndexFromLoc ( { line : 2 , column : 1 } ) ,
196+ 4 ,
197+ ) ;
198+ assert . strictEqual (
199+ markdownSourceCode . getIndexFromLoc ( { line : 2 , column : 2 } ) ,
200+ 5 ,
201+ ) ;
202+ assert . strictEqual (
203+ markdownSourceCode . getIndexFromLoc ( { line : 2 , column : 3 } ) ,
204+ 6 ,
205+ ) ;
206+ assert . strictEqual (
207+ markdownSourceCode . getIndexFromLoc ( { line : 2 , column : 4 } ) ,
208+ 7 ,
209+ ) ;
210+ assert . strictEqual (
211+ markdownSourceCode . getIndexFromLoc ( { line : 2 , column : 5 } ) ,
212+ 8 ,
213+ ) ;
214+ assert . strictEqual (
215+ markdownSourceCode . getIndexFromLoc ( { line : 3 , column : 1 } ) ,
216+ 9 ,
217+ ) ;
218+ assert . strictEqual (
219+ markdownSourceCode . getIndexFromLoc ( { line : 3 , column : 2 } ) ,
220+ 10 ,
221+ ) ;
222+ assert . strictEqual (
223+ markdownSourceCode . getIndexFromLoc ( { line : 3 , column : 3 } ) ,
224+ 11 ,
225+ ) ;
226+ assert . strictEqual (
227+ markdownSourceCode . getIndexFromLoc ( { line : 3 , column : 4 } ) ,
228+ 12 ,
229+ ) ;
230+ } ) ;
231+ } ) ;
232+
107233 describe ( "getInlineConfigNodes()" , ( ) => {
108234 it ( "should return the inline config nodes" , ( ) => {
109235 const nodes = sourceCode . getInlineConfigNodes ( ) ;
0 commit comments