@@ -86,6 +86,208 @@ describe("JSONSourceCode", () => {
8686 } ) ;
8787 } ) ;
8888
89+ describe ( "getLocFromIndex()" , ( ) => {
90+ it ( "should convert index to location correctly" , ( ) => {
91+ const file = { body : '{\n "a": "b"\r\n}' , path : "test.json" } ;
92+ const language = new JSONLanguage ( { mode : "json" } ) ;
93+ const parseResult = language . parse ( file ) ;
94+ const sourceCode = new JSONSourceCode ( {
95+ text : file . body ,
96+ ast : parseResult . ast ,
97+ } ) ;
98+
99+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 0 ) , {
100+ line : 1 ,
101+ column : 1 ,
102+ } ) ;
103+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 1 ) , {
104+ line : 1 ,
105+ column : 2 ,
106+ } ) ;
107+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 2 ) , {
108+ line : 2 ,
109+ column : 1 ,
110+ } ) ;
111+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 3 ) , {
112+ line : 2 ,
113+ column : 2 ,
114+ } ) ;
115+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 4 ) , {
116+ line : 2 ,
117+ column : 3 ,
118+ } ) ;
119+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 5 ) , {
120+ line : 2 ,
121+ column : 4 ,
122+ } ) ;
123+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 6 ) , {
124+ line : 2 ,
125+ column : 5 ,
126+ } ) ;
127+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 7 ) , {
128+ line : 2 ,
129+ column : 6 ,
130+ } ) ;
131+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 8 ) , {
132+ line : 2 ,
133+ column : 7 ,
134+ } ) ;
135+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 9 ) , {
136+ line : 2 ,
137+ column : 8 ,
138+ } ) ;
139+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 10 ) , {
140+ line : 2 ,
141+ column : 9 ,
142+ } ) ;
143+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 11 ) , {
144+ line : 2 ,
145+ column : 10 ,
146+ } ) ;
147+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 12 ) , {
148+ line : 2 ,
149+ column : 11 ,
150+ } ) ;
151+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 13 ) , {
152+ line : 2 ,
153+ column : 12 ,
154+ } ) ;
155+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 14 ) , {
156+ line : 3 ,
157+ column : 1 ,
158+ } ) ;
159+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 15 ) , {
160+ line : 3 ,
161+ column : 2 ,
162+ } ) ;
163+ } ) ;
164+ } ) ;
165+
166+ describe ( "getIndexFromLoc()" , ( ) => {
167+ it ( "should convert location to index correctly" , ( ) => {
168+ const file = { body : '{\n "a": "b"\r\n}' , path : "test.json" } ;
169+ const language = new JSONLanguage ( { mode : "json" } ) ;
170+ const parseResult = language . parse ( file ) ;
171+ const sourceCode = new JSONSourceCode ( {
172+ text : file . body ,
173+ ast : parseResult . ast ,
174+ } ) ;
175+
176+ assert . strictEqual (
177+ sourceCode . getIndexFromLoc ( {
178+ line : 1 ,
179+ column : 1 ,
180+ } ) ,
181+ 0 ,
182+ ) ;
183+ assert . strictEqual (
184+ sourceCode . getIndexFromLoc ( {
185+ line : 1 ,
186+ column : 2 ,
187+ } ) ,
188+ 1 ,
189+ ) ;
190+ assert . strictEqual (
191+ sourceCode . getIndexFromLoc ( {
192+ line : 2 ,
193+ column : 1 ,
194+ } ) ,
195+ 2 ,
196+ ) ;
197+ assert . strictEqual (
198+ sourceCode . getIndexFromLoc ( {
199+ line : 2 ,
200+ column : 2 ,
201+ } ) ,
202+ 3 ,
203+ ) ;
204+ assert . strictEqual (
205+ sourceCode . getIndexFromLoc ( {
206+ line : 2 ,
207+ column : 3 ,
208+ } ) ,
209+ 4 ,
210+ ) ;
211+ assert . strictEqual (
212+ sourceCode . getIndexFromLoc ( {
213+ line : 2 ,
214+ column : 4 ,
215+ } ) ,
216+ 5 ,
217+ ) ;
218+ assert . strictEqual (
219+ sourceCode . getIndexFromLoc ( {
220+ line : 2 ,
221+ column : 5 ,
222+ } ) ,
223+ 6 ,
224+ ) ;
225+ assert . strictEqual (
226+ sourceCode . getIndexFromLoc ( {
227+ line : 2 ,
228+ column : 6 ,
229+ } ) ,
230+ 7 ,
231+ ) ;
232+ assert . strictEqual (
233+ sourceCode . getIndexFromLoc ( {
234+ line : 2 ,
235+ column : 7 ,
236+ } ) ,
237+ 8 ,
238+ ) ;
239+ assert . strictEqual (
240+ sourceCode . getIndexFromLoc ( {
241+ line : 2 ,
242+ column : 8 ,
243+ } ) ,
244+ 9 ,
245+ ) ;
246+ assert . strictEqual (
247+ sourceCode . getIndexFromLoc ( {
248+ line : 2 ,
249+ column : 9 ,
250+ } ) ,
251+ 10 ,
252+ ) ;
253+ assert . strictEqual (
254+ sourceCode . getIndexFromLoc ( {
255+ line : 2 ,
256+ column : 10 ,
257+ } ) ,
258+ 11 ,
259+ ) ;
260+ assert . strictEqual (
261+ sourceCode . getIndexFromLoc ( {
262+ line : 2 ,
263+ column : 11 ,
264+ } ) ,
265+ 12 ,
266+ ) ;
267+ assert . strictEqual (
268+ sourceCode . getIndexFromLoc ( {
269+ line : 2 ,
270+ column : 12 ,
271+ } ) ,
272+ 13 ,
273+ ) ;
274+ assert . strictEqual (
275+ sourceCode . getIndexFromLoc ( {
276+ line : 3 ,
277+ column : 1 ,
278+ } ) ,
279+ 14 ,
280+ ) ;
281+ assert . strictEqual (
282+ sourceCode . getIndexFromLoc ( {
283+ line : 3 ,
284+ column : 2 ,
285+ } ) ,
286+ 15 ,
287+ ) ;
288+ } ) ;
289+ } ) ;
290+
89291 describe ( "getRange()" , ( ) => {
90292 it ( "should return the range property of a node" , ( ) => {
91293 const range = [ 0 , 1 ] ;
0 commit comments