@@ -104,6 +104,236 @@ describe("CSSSourceCode", () => {
104104 } ) ;
105105 } ) ;
106106
107+ describe ( "getLocFromIndex()" , ( ) => {
108+ it ( "should convert index to location correctly" , ( ) => {
109+ const file = {
110+ body : "a {\n /*test*/\r\n}" ,
111+ path : "test.css" ,
112+ } ;
113+ const language = new CSSLanguage ( ) ;
114+ const parseResult = language . parse ( file ) ;
115+ const sourceCode = new CSSSourceCode ( {
116+ text : file . body ,
117+ ast : parseResult . ast ,
118+ } ) ;
119+
120+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 0 ) , {
121+ line : 1 ,
122+ column : 1 ,
123+ } ) ;
124+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 1 ) , {
125+ line : 1 ,
126+ column : 2 ,
127+ } ) ;
128+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 2 ) , {
129+ line : 1 ,
130+ column : 3 ,
131+ } ) ;
132+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 3 ) , {
133+ line : 1 ,
134+ column : 4 ,
135+ } ) ;
136+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 4 ) , {
137+ line : 2 ,
138+ column : 1 ,
139+ } ) ;
140+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 5 ) , {
141+ line : 2 ,
142+ column : 2 ,
143+ } ) ;
144+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 6 ) , {
145+ line : 2 ,
146+ column : 3 ,
147+ } ) ;
148+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 7 ) , {
149+ line : 2 ,
150+ column : 4 ,
151+ } ) ;
152+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 8 ) , {
153+ line : 2 ,
154+ column : 5 ,
155+ } ) ;
156+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 9 ) , {
157+ line : 2 ,
158+ column : 6 ,
159+ } ) ;
160+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 10 ) , {
161+ line : 2 ,
162+ column : 7 ,
163+ } ) ;
164+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 11 ) , {
165+ line : 2 ,
166+ column : 8 ,
167+ } ) ;
168+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 12 ) , {
169+ line : 2 ,
170+ column : 9 ,
171+ } ) ;
172+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 13 ) , {
173+ line : 2 ,
174+ column : 10 ,
175+ } ) ;
176+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 14 ) , {
177+ line : 2 ,
178+ column : 11 ,
179+ } ) ;
180+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 15 ) , {
181+ line : 2 ,
182+ column : 12 ,
183+ } ) ;
184+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 16 ) , {
185+ line : 3 ,
186+ column : 1 ,
187+ } ) ;
188+ assert . deepStrictEqual ( sourceCode . getLocFromIndex ( 17 ) , {
189+ line : 3 ,
190+ column : 2 ,
191+ } ) ;
192+ } ) ;
193+ } ) ;
194+
195+ describe ( "getIndexFromLoc()" , ( ) => {
196+ it ( "should convert location to index correctly" , ( ) => {
197+ const file = {
198+ body : "a {\n /*test*/\r\n}" ,
199+ path : "test.css" ,
200+ } ;
201+ const language = new CSSLanguage ( ) ;
202+ const parseResult = language . parse ( file ) ;
203+ const sourceCode = new CSSSourceCode ( {
204+ text : file . body ,
205+ ast : parseResult . ast ,
206+ } ) ;
207+
208+ assert . strictEqual (
209+ sourceCode . getIndexFromLoc ( {
210+ line : 1 ,
211+ column : 1 ,
212+ } ) ,
213+ 0 ,
214+ ) ;
215+ assert . strictEqual (
216+ sourceCode . getIndexFromLoc ( {
217+ line : 1 ,
218+ column : 2 ,
219+ } ) ,
220+ 1 ,
221+ ) ;
222+ assert . strictEqual (
223+ sourceCode . getIndexFromLoc ( {
224+ line : 1 ,
225+ column : 3 ,
226+ } ) ,
227+ 2 ,
228+ ) ;
229+ assert . strictEqual (
230+ sourceCode . getIndexFromLoc ( {
231+ line : 1 ,
232+ column : 4 ,
233+ } ) ,
234+ 3 ,
235+ ) ;
236+ assert . strictEqual (
237+ sourceCode . getIndexFromLoc ( {
238+ line : 2 ,
239+ column : 1 ,
240+ } ) ,
241+ 4 ,
242+ ) ;
243+ assert . strictEqual (
244+ sourceCode . getIndexFromLoc ( {
245+ line : 2 ,
246+ column : 2 ,
247+ } ) ,
248+ 5 ,
249+ ) ;
250+ assert . strictEqual (
251+ sourceCode . getIndexFromLoc ( {
252+ line : 2 ,
253+ column : 3 ,
254+ } ) ,
255+ 6 ,
256+ ) ;
257+ assert . strictEqual (
258+ sourceCode . getIndexFromLoc ( {
259+ line : 2 ,
260+ column : 4 ,
261+ } ) ,
262+ 7 ,
263+ ) ;
264+ assert . strictEqual (
265+ sourceCode . getIndexFromLoc ( {
266+ line : 2 ,
267+ column : 5 ,
268+ } ) ,
269+ 8 ,
270+ ) ;
271+ assert . strictEqual (
272+ sourceCode . getIndexFromLoc ( {
273+ line : 2 ,
274+ column : 6 ,
275+ } ) ,
276+ 9 ,
277+ ) ;
278+ assert . strictEqual (
279+ sourceCode . getIndexFromLoc ( {
280+ line : 2 ,
281+ column : 7 ,
282+ } ) ,
283+ 10 ,
284+ ) ;
285+ assert . strictEqual (
286+ sourceCode . getIndexFromLoc ( {
287+ line : 2 ,
288+ column : 8 ,
289+ } ) ,
290+ 11 ,
291+ ) ;
292+ assert . strictEqual (
293+ sourceCode . getIndexFromLoc ( {
294+ line : 2 ,
295+ column : 9 ,
296+ } ) ,
297+ 12 ,
298+ ) ;
299+ assert . strictEqual (
300+ sourceCode . getIndexFromLoc ( {
301+ line : 2 ,
302+ column : 10 ,
303+ } ) ,
304+ 13 ,
305+ ) ;
306+ assert . strictEqual (
307+ sourceCode . getIndexFromLoc ( {
308+ line : 2 ,
309+ column : 11 ,
310+ } ) ,
311+ 14 ,
312+ ) ;
313+ assert . strictEqual (
314+ sourceCode . getIndexFromLoc ( {
315+ line : 2 ,
316+ column : 12 ,
317+ } ) ,
318+ 15 ,
319+ ) ;
320+ assert . strictEqual (
321+ sourceCode . getIndexFromLoc ( {
322+ line : 3 ,
323+ column : 1 ,
324+ } ) ,
325+ 16 ,
326+ ) ;
327+ assert . strictEqual (
328+ sourceCode . getIndexFromLoc ( {
329+ line : 3 ,
330+ column : 2 ,
331+ } ) ,
332+ 17 ,
333+ ) ;
334+ } ) ;
335+ } ) ;
336+
107337 describe ( "getRange()" , ( ) => {
108338 it ( "should return the range property of a node" , ( ) => {
109339 const loc = {
0 commit comments