Skip to content

Commit cf98cdc

Browse files
authored
feat: add Range to StringExpression nodes (#1349)
1 parent 82af17c commit cf98cdc

9 files changed

Lines changed: 153 additions & 0 deletions

parser/v2/cssparser_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func TestExpressionCSSPropertyParser(t *testing.T) {
3434
},
3535
},
3636
},
37+
Range: Range{
38+
From: Position{Index: 18, Line: 0, Col: 18},
39+
To: Position{Index: 47, Line: 0, Col: 47},
40+
},
3741
},
3842
},
3943
},
@@ -58,6 +62,10 @@ func TestExpressionCSSPropertyParser(t *testing.T) {
5862
},
5963
},
6064
},
65+
Range: Range{
66+
From: Position{Index: 19, Line: 1, Col: 0},
67+
To: Position{Index: 48, Line: 1, Col: 29},
68+
},
6169
},
6270
},
6371
},
@@ -262,6 +270,10 @@ background-color: { constants.BackgroundColor };
262270
},
263271
},
264272
},
273+
Range: Range{
274+
From: Position{Index: 31, Line: 1, Col: 18},
275+
To: Position{Index: 60, Line: 1, Col: 47},
276+
},
265277
},
266278
},
267279
},
@@ -312,6 +324,10 @@ background-color: { prop };
312324
},
313325
},
314326
},
327+
Range: Range{
328+
From: Position{Index: 42, Line: 1, Col: 18},
329+
To: Position{Index: 50, Line: 1, Col: 26},
330+
},
315331
},
316332
},
317333
},

parser/v2/elementparser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,10 @@ func TestElementParser(t *testing.T) {
19811981
},
19821982
},
19831983
},
1984+
Range: Range{
1985+
From: Position{Index: 5, Line: 0, Col: 5},
1986+
To: Position{Index: 15, Line: 0, Col: 15},
1987+
},
19841988
},
19851989
},
19861990
Range: Range{

parser/v2/forexpressionparser_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ func TestForExpressionParser(t *testing.T) {
221221
},
222222
},
223223
},
224+
Range: Range{
225+
From: Position{Index: 41, Line: 1, Col: 10},
226+
To: Position{Index: 49, Line: 1, Col: 18},
227+
},
224228
},
225229
},
226230
TrailingSpace: SpaceVertical,
@@ -294,6 +298,10 @@ func TestForExpressionParser(t *testing.T) {
294298
},
295299
},
296300
},
301+
Range: Range{
302+
From: Position{Index: 40, Line: 1, Col: 10},
303+
To: Position{Index: 48, Line: 1, Col: 18},
304+
},
297305
},
298306
},
299307
TrailingSpace: SpaceVertical,

parser/v2/ifexpressionparser_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func TestIfExpression(t *testing.T) {
7676
},
7777
},
7878
TrailingSpace: SpaceVertical,
79+
Range: Range{
80+
From: Position{Index: 21, Line: 2, Col: 2},
81+
To: Position{Index: 40, Line: 3, Col: 0},
82+
},
7983
},
8084
},
8185
IndentChildren: true,
@@ -146,6 +150,10 @@ func TestIfExpression(t *testing.T) {
146150
},
147151
},
148152
TrailingSpace: SpaceVertical,
153+
Range: Range{
154+
From: Position{Index: 10, Line: 1, Col: 1},
155+
To: Position{Index: 18, Line: 2, Col: 0},
156+
},
149157
},
150158
},
151159
Else: []Node{
@@ -166,6 +174,10 @@ func TestIfExpression(t *testing.T) {
166174
},
167175
},
168176
TrailingSpace: SpaceVertical,
177+
Range: Range{
178+
From: Position{Index: 28, Line: 3, Col: 1},
179+
To: Position{Index: 36, Line: 4, Col: 0},
180+
},
169181
},
170182
},
171183
Range: Range{
@@ -288,6 +300,10 @@ func TestIfExpression(t *testing.T) {
288300
},
289301
},
290302
TrailingSpace: SpaceVertical,
303+
Range: Range{
304+
From: Position{Index: 21, Line: 2, Col: 2},
305+
To: Position{Index: 40, Line: 3, Col: 0},
306+
},
291307
},
292308
},
293309
IndentChildren: true,
@@ -358,6 +374,10 @@ func TestIfExpression(t *testing.T) {
358374
},
359375
},
360376
TrailingSpace: SpaceVertical,
377+
Range: Range{
378+
From: Position{Index: 9, Line: 1, Col: 1},
379+
To: Position{Index: 17, Line: 2, Col: 0},
380+
},
361381
},
362382
},
363383
Else: []Node{
@@ -378,6 +398,10 @@ func TestIfExpression(t *testing.T) {
378398
},
379399
},
380400
TrailingSpace: SpaceVertical,
401+
Range: Range{
402+
From: Position{Index: 27, Line: 3, Col: 1},
403+
To: Position{Index: 35, Line: 4, Col: 0},
404+
},
381405
},
382406
},
383407
Range: Range{
@@ -477,6 +501,10 @@ func TestIfExpression(t *testing.T) {
477501
},
478502
},
479503
},
504+
Range: Range{
505+
From: Position{Index: 34, Line: 2, Col: 11},
506+
To: Position{Index: 41, Line: 2, Col: 18},
507+
},
480508
},
481509
},
482510
TrailingSpace: SpaceVertical,
@@ -549,6 +577,10 @@ func TestIfExpression(t *testing.T) {
549577
},
550578
},
551579
TrailingSpace: SpaceVertical,
580+
Range: Range{
581+
From: Position{Index: 10, Line: 1, Col: 1},
582+
To: Position{Index: 18, Line: 2, Col: 0},
583+
},
552584
},
553585
},
554586
ElseIfs: []ElseIfExpression{
@@ -583,6 +615,10 @@ func TestIfExpression(t *testing.T) {
583615
},
584616
},
585617
TrailingSpace: SpaceVertical,
618+
Range: Range{
619+
From: Position{Index: 35, Line: 3, Col: 1},
620+
To: Position{Index: 43, Line: 4, Col: 0},
621+
},
586622
},
587623
},
588624
Range: Range{
@@ -637,6 +673,10 @@ func TestIfExpression(t *testing.T) {
637673
},
638674
},
639675
TrailingSpace: SpaceVertical,
676+
Range: Range{
677+
From: Position{Index: 10, Line: 1, Col: 1},
678+
To: Position{Index: 18, Line: 2, Col: 0},
679+
},
640680
},
641681
},
642682
ElseIfs: []ElseIfExpression{
@@ -671,6 +711,10 @@ func TestIfExpression(t *testing.T) {
671711
},
672712
},
673713
TrailingSpace: SpaceVertical,
714+
Range: Range{
715+
From: Position{Index: 35, Line: 3, Col: 1},
716+
To: Position{Index: 43, Line: 4, Col: 0},
717+
},
674718
},
675719
},
676720
Range: Range{
@@ -709,6 +753,10 @@ func TestIfExpression(t *testing.T) {
709753
},
710754
},
711755
TrailingSpace: SpaceVertical,
756+
Range: Range{
757+
From: Position{Index: 60, Line: 5, Col: 1},
758+
To: Position{Index: 68, Line: 6, Col: 0},
759+
},
712760
},
713761
},
714762
Range: Range{
@@ -765,6 +813,10 @@ func TestIfExpression(t *testing.T) {
765813
},
766814
},
767815
TrailingSpace: SpaceVertical,
816+
Range: Range{
817+
From: Position{Index: 10, Line: 1, Col: 1},
818+
To: Position{Index: 18, Line: 2, Col: 0},
819+
},
768820
},
769821
},
770822
ElseIfs: []ElseIfExpression{
@@ -799,6 +851,10 @@ func TestIfExpression(t *testing.T) {
799851
},
800852
},
801853
TrailingSpace: SpaceVertical,
854+
Range: Range{
855+
From: Position{Index: 35, Line: 3, Col: 1},
856+
To: Position{Index: 43, Line: 4, Col: 0},
857+
},
802858
},
803859
},
804860
Range: Range{
@@ -837,6 +893,10 @@ func TestIfExpression(t *testing.T) {
837893
},
838894
},
839895
TrailingSpace: SpaceVertical,
896+
Range: Range{
897+
From: Position{Index: 60, Line: 5, Col: 1},
898+
To: Position{Index: 68, Line: 6, Col: 0},
899+
},
840900
},
841901
},
842902
Range: Range{
@@ -855,6 +915,10 @@ func TestIfExpression(t *testing.T) {
855915
},
856916
},
857917
TrailingSpace: SpaceVertical,
918+
Range: Range{
919+
From: Position{Index: 78, Line: 7, Col: 1},
920+
To: Position{Index: 86, Line: 8, Col: 0},
921+
},
858922
},
859923
},
860924
Range: Range{
@@ -940,6 +1004,10 @@ func TestIfExpression(t *testing.T) {
9401004
},
9411005
},
9421006
TrailingSpace: SpaceVertical,
1007+
Range: Range{
1008+
From: Position{Index: 44, Line: 3, Col: 2},
1009+
To: Position{Index: 53, Line: 4, Col: 1},
1010+
},
9431011
},
9441012
},
9451013
Range: Range{

parser/v2/stringexpressionparser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
)
66

77
var stringExpression = parse.Func(func(pi *parse.Input) (n Node, matched bool, err error) {
8+
start := pi.Index()
9+
810
// Check the prefix first.
911
if _, matched, err = parse.Or(parse.String("{ "), parse.String("{")).Parse(pi); err != nil || !matched {
1012
return
@@ -36,5 +38,7 @@ var stringExpression = parse.Func(func(pi *parse.Input) (n Node, matched bool, e
3638
return r, true, err
3739
}
3840

41+
r.Range = NewRange(pi.PositionAt(start), pi.Position())
42+
3943
return r, true, nil
4044
})

parser/v2/stringexpressionparser_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func TestStringExpressionParser(t *testing.T) {
3333
},
3434
},
3535
},
36+
Range: Range{
37+
From: Position{Index: 0, Line: 0, Col: 0},
38+
To: Position{Index: 10, Line: 0, Col: 10},
39+
},
3640
},
3741
},
3842
{
@@ -55,6 +59,10 @@ func TestStringExpressionParser(t *testing.T) {
5559
},
5660
},
5761
},
62+
Range: Range{
63+
From: Position{Index: 0, Line: 0, Col: 0},
64+
To: Position{Index: 8, Line: 0, Col: 8},
65+
},
5866
},
5967
},
6068
{
@@ -79,6 +87,10 @@ func TestStringExpressionParser(t *testing.T) {
7987
},
8088
},
8189
},
90+
Range: Range{
91+
From: Position{Index: 0, Line: 0, Col: 0},
92+
To: Position{Index: 29, Line: 2, Col: 7},
93+
},
8294
},
8395
},
8496
}

parser/v2/switchexpressionparser_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ default:
131131
},
132132
},
133133
TrailingSpace: SpaceVertical,
134+
Range: Range{
135+
From: Position{Index: 39, Line: 3, Col: 3},
136+
To: Position{Index: 59, Line: 4, Col: 1},
137+
},
134138
},
135139
},
136140
IndentChildren: true,
@@ -228,6 +232,10 @@ default:
228232
},
229233
},
230234
TrailingSpace: SpaceVertical,
235+
Range: Range{
236+
From: Position{Index: 45, Line: 3, Col: 2},
237+
To: Position{Index: 64, Line: 4, Col: 0},
238+
},
231239
},
232240
},
233241
IndentChildren: true,
@@ -320,6 +328,10 @@ default:
320328
},
321329
},
322330
TrailingSpace: SpaceVertical,
331+
Range: Range{
332+
From: Position{Index: 32, Line: 2, Col: 2},
333+
To: Position{Index: 41, Line: 3, Col: 1},
334+
},
323335
},
324336
},
325337
},
@@ -372,6 +384,10 @@ default:
372384
},
373385
},
374386
TrailingSpace: SpaceVertical,
387+
Range: Range{
388+
From: Position{Index: 53, Line: 4, Col: 2},
389+
To: Position{Index: 61, Line: 5, Col: 0},
390+
},
375391
},
376392
},
377393
},
@@ -504,6 +520,10 @@ default:
504520
},
505521
},
506522
TrailingSpace: SpaceVertical,
523+
Range: Range{
524+
From: Position{Index: 57, Line: 4, Col: 2},
525+
To: Position{Index: 65, Line: 5, Col: 0},
526+
},
507527
},
508528
},
509529
},

0 commit comments

Comments
 (0)