Skip to content

Commit f712c14

Browse files
authored
feat: add Ranges to IfExpression and ElseIfExpressionparser nodes (#1248)
1 parent 93d4a5c commit f712c14

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

parser/v2/ifexpressionparser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func (ifExpressionParser) Parse(pi *parse.Input) (n Node, matched bool, err erro
6767
return r, true, parse.Error("if: expected closing brace", pi.Position())
6868
}
6969

70+
r.Range = NewRange(pi.PositionAt(start), pi.Position())
7071
return r, true, nil
7172
}
7273

@@ -108,6 +109,7 @@ func (elseIfExpressionParser) Parse(pi *parse.Input) (r ElseIfExpression, matche
108109
}
109110
r.Then = thenNodes.Nodes
110111

112+
r.Range = NewRange(pi.PositionAt(start), pi.Position())
111113
return r, true, nil
112114
}
113115

parser/v2/ifexpressionparser_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func TestIfExpression(t *testing.T) {
7474
},
7575
},
7676
},
77+
Range: Range{
78+
From: Position{Index: 0, Line: 0, Col: 0},
79+
To: Position{Index: 49, Line: 4, Col: 1},
80+
},
7781
},
7882
},
7983
{
@@ -140,6 +144,10 @@ func TestIfExpression(t *testing.T) {
140144
TrailingSpace: SpaceVertical,
141145
},
142146
},
147+
Range: Range{
148+
From: Position{Index: 0, Line: 0, Col: 0},
149+
To: Position{Index: 37, Line: 4, Col: 1},
150+
},
143151
},
144152
},
145153
{
@@ -175,6 +183,10 @@ func TestIfExpression(t *testing.T) {
175183
TrailingSpace: SpaceVertical,
176184
},
177185
},
186+
Range: Range{
187+
From: Position{Index: 0, Line: 0, Col: 0},
188+
To: Position{Index: 21, Line: 2, Col: 1},
189+
},
178190
},
179191
},
180192
{
@@ -238,6 +250,10 @@ func TestIfExpression(t *testing.T) {
238250
},
239251
},
240252
},
253+
Range: Range{
254+
From: Position{Index: 0, Line: 0, Col: 0},
255+
To: Position{Index: 49, Line: 4, Col: 1},
256+
},
241257
},
242258
},
243259
{
@@ -304,6 +320,10 @@ func TestIfExpression(t *testing.T) {
304320
TrailingSpace: SpaceVertical,
305321
},
306322
},
323+
Range: Range{
324+
From: Position{Index: 0, Line: 0, Col: 0},
325+
To: Position{Index: 36, Line: 4, Col: 1},
326+
},
307327
},
308328
},
309329
{
@@ -382,9 +402,17 @@ func TestIfExpression(t *testing.T) {
382402
},
383403
},
384404
},
405+
Range: Range{
406+
From: Position{Index: 14, Line: 1, Col: 5},
407+
To: Position{Index: 54, Line: 3, Col: 6},
408+
},
385409
},
386410
&Whitespace{Value: "\n\t\t\t\t"},
387411
},
412+
Range: Range{
413+
From: Position{Index: 0, Line: 0, Col: 0},
414+
To: Position{Index: 60, Line: 4, Col: 5},
415+
},
388416
},
389417
},
390418
{
@@ -437,8 +465,16 @@ func TestIfExpression(t *testing.T) {
437465
TrailingSpace: SpaceVertical,
438466
},
439467
},
468+
Range: Range{
469+
From: Position{Index: 18, Line: 2, Col: 0},
470+
To: Position{Index: 43, Line: 4, Col: 0},
471+
},
440472
},
441473
},
474+
Range: Range{
475+
From: Position{Index: 0, Line: 0, Col: 0},
476+
To: Position{Index: 44, Line: 4, Col: 1},
477+
},
442478
},
443479
},
444480
{
@@ -493,6 +529,10 @@ func TestIfExpression(t *testing.T) {
493529
TrailingSpace: SpaceVertical,
494530
},
495531
},
532+
Range: Range{
533+
From: Position{Index: 18, Line: 2, Col: 0},
534+
To: Position{Index: 43, Line: 4, Col: 0},
535+
},
496536
},
497537
{
498538
Expression: Expression{
@@ -515,8 +555,16 @@ func TestIfExpression(t *testing.T) {
515555
TrailingSpace: SpaceVertical,
516556
},
517557
},
558+
Range: Range{
559+
From: Position{Index: 43, Line: 4, Col: 0},
560+
To: Position{Index: 68, Line: 6, Col: 0},
561+
},
518562
},
519563
},
564+
Range: Range{
565+
From: Position{Index: 0, Line: 0, Col: 0},
566+
To: Position{Index: 69, Line: 6, Col: 1},
567+
},
520568
},
521569
},
522570
{
@@ -573,6 +621,10 @@ func TestIfExpression(t *testing.T) {
573621
TrailingSpace: SpaceVertical,
574622
},
575623
},
624+
Range: Range{
625+
From: Position{Index: 18, Line: 2, Col: 0},
626+
To: Position{Index: 43, Line: 4, Col: 0},
627+
},
576628
},
577629
{
578630
Expression: Expression{
@@ -595,6 +647,10 @@ func TestIfExpression(t *testing.T) {
595647
TrailingSpace: SpaceVertical,
596648
},
597649
},
650+
Range: Range{
651+
From: Position{Index: 43, Line: 4, Col: 0},
652+
To: Position{Index: 68, Line: 6, Col: 0},
653+
},
598654
},
599655
},
600656
Else: []Node{
@@ -609,6 +665,10 @@ func TestIfExpression(t *testing.T) {
609665
TrailingSpace: SpaceVertical,
610666
},
611667
},
668+
Range: Range{
669+
From: Position{Index: 0, Line: 0, Col: 0},
670+
To: Position{Index: 87, Line: 8, Col: 1},
671+
},
612672
},
613673
},
614674
{
@@ -666,6 +726,10 @@ func TestIfExpression(t *testing.T) {
666726
TrailingSpace: SpaceVertical,
667727
},
668728
},
729+
Range: Range{
730+
From: Position{Index: 0, Line: 0, Col: 0},
731+
To: Position{Index: 54, Line: 4, Col: 2},
732+
},
669733
},
670734
},
671735
}

parser/v2/templateparser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ func TestTemplateParser(t *testing.T) {
450450
},
451451
},
452452
},
453+
Range: Range{
454+
From: Position{Index: 27, Line: 1, Col: 1},
455+
To: Position{Index: 82, Line: 5, Col: 2},
456+
},
453457
},
454458
&Whitespace{
455459
Value: "\n",

parser/v2/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,11 +1260,13 @@ type IfExpression struct {
12601260
Then []Node
12611261
ElseIfs []ElseIfExpression
12621262
Else []Node
1263+
Range Range
12631264
}
12641265

12651266
type ElseIfExpression struct {
12661267
Expression Expression
12671268
Then []Node
1269+
Range Range
12681270
}
12691271

12701272
func (n IfExpression) ChildNodes() []Node {

0 commit comments

Comments
 (0)