Skip to content

Commit aed18e8

Browse files
authored
feat: add Range to TemplElementExpression (#1276)
1 parent b580171 commit aed18e8

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

parser/v2/templateparser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,10 @@ func TestTemplateParser(t *testing.T) {
668668
},
669669
},
670670
},
671+
Range: Range{
672+
From: Position{Index: 26, Line: 1, Col: 14},
673+
To: Position{Index: 47, Line: 1, Col: 35},
674+
},
671675
},
672676
&Whitespace{Value: " "},
673677
&Text{

parser/v2/templelementparser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
type templElementExpressionParser struct{}
99

1010
func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, matched bool, err error) {
11+
start := pi.Position()
12+
1113
// Check the prefix first.
1214
if _, matched, err = parse.Rune('@').Parse(pi); err != nil || !matched {
1315
return nil, false, nil
@@ -26,6 +28,7 @@ func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, matched bo
2628
return
2729
}
2830
if !hasOpenBrace {
31+
r.Range = NewRange(start, pi.Position())
2932
return r, true, nil
3033
}
3134

@@ -48,6 +51,8 @@ func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, matched bo
4851
return r, true, err
4952
}
5053

54+
r.Range = NewRange(start, pi.Position())
55+
5156
return r, true, nil
5257
}
5358

parser/v2/templelementparser_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
3232
},
3333
},
3434
},
35+
Range: Range{
36+
From: Position{Index: 0, Line: 0, Col: 0},
37+
To: Position{Index: 14, Line: 0, Col: 14},
38+
},
3539
},
3640
},
3741
{
@@ -53,6 +57,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
5357
},
5458
},
5559
},
60+
Range: Range{
61+
From: Position{Index: 0, Line: 0, Col: 0},
62+
To: Position{Index: 24, Line: 0, Col: 24},
63+
},
5664
},
5765
},
5866
{
@@ -80,6 +88,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
8088
},
8189
},
8290
},
91+
Range: Range{
92+
From: Position{Index: 0, Line: 0, Col: 0},
93+
To: Position{Index: 60, Line: 3, Col: 4},
94+
},
8395
},
8496
},
8597
{
@@ -114,6 +126,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
114126
TrailingSpace: SpaceVertical,
115127
},
116128
},
129+
Range: Range{
130+
From: Position{Index: 0, Line: 0, Col: 0},
131+
To: Position{Index: 30, Line: 2, Col: 1},
132+
},
117133
},
118134
},
119135
{
@@ -164,6 +180,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
164180
},
165181
},
166182
},
183+
Range: Range{
184+
From: Position{Index: 0, Line: 0, Col: 0},
185+
To: Position{Index: 43, Line: 2, Col: 3},
186+
},
167187
},
168188
},
169189
{
@@ -197,9 +217,17 @@ func TestTemplElementExpressionParser(t *testing.T) {
197217
To: Position{28, 1, 11},
198218
},
199219
},
220+
Range: Range{
221+
From: Position{Index: 21, Line: 1, Col: 4},
222+
To: Position{Index: 28, Line: 1, Col: 11},
223+
},
200224
},
201225
&Whitespace{Value: "\n\t\t\t"},
202226
},
227+
Range: Range{
228+
From: Position{Index: 0, Line: 0, Col: 0},
229+
To: Position{Index: 33, Line: 2, Col: 4},
230+
},
203231
},
204232
},
205233
{
@@ -222,6 +250,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
222250
},
223251
},
224252
},
253+
Range: Range{
254+
From: Position{Index: 0, Line: 0, Col: 0},
255+
To: Position{Index: 21, Line: 0, Col: 21},
256+
},
225257
},
226258
},
227259
{
@@ -243,6 +275,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
243275
},
244276
},
245277
},
278+
Range: Range{
279+
From: Position{Index: 0, Line: 0, Col: 0},
280+
To: Position{Index: 31, Line: 0, Col: 31},
281+
},
246282
},
247283
},
248284
{
@@ -264,6 +300,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
264300
},
265301
},
266302
},
303+
Range: Range{
304+
From: Position{Index: 0, Line: 0, Col: 0},
305+
To: Position{Index: 31, Line: 0, Col: 31},
306+
},
267307
},
268308
},
269309
{
@@ -285,6 +325,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
285325
},
286326
},
287327
},
328+
Range: Range{
329+
From: Position{Index: 0, Line: 0, Col: 0},
330+
To: Position{Index: 15, Line: 0, Col: 15},
331+
},
288332
},
289333
},
290334
{
@@ -306,6 +350,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
306350
},
307351
},
308352
},
353+
Range: Range{
354+
From: Position{Index: 0, Line: 0, Col: 0},
355+
To: Position{Index: 19, Line: 0, Col: 19},
356+
},
309357
},
310358
},
311359
{
@@ -327,6 +375,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
327375
},
328376
},
329377
},
378+
Range: Range{
379+
From: Position{Index: 0, Line: 0, Col: 0},
380+
To: Position{Index: 30, Line: 0, Col: 30},
381+
},
330382
},
331383
},
332384
{
@@ -348,6 +400,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
348400
},
349401
},
350402
},
403+
Range: Range{
404+
From: Position{Index: 0, Line: 0, Col: 0},
405+
To: Position{Index: 30, Line: 0, Col: 30},
406+
},
351407
},
352408
},
353409
{
@@ -369,6 +425,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
369425
},
370426
},
371427
},
428+
Range: Range{
429+
From: Position{Index: 0, Line: 0, Col: 0},
430+
To: Position{Index: 9, Line: 0, Col: 9},
431+
},
372432
},
373433
},
374434
{
@@ -382,6 +442,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
382442
To: Position{33, 0, 33},
383443
},
384444
},
445+
Range: Range{
446+
From: Position{Index: 0, Line: 0, Col: 0},
447+
To: Position{Index: 33, Line: 0, Col: 33},
448+
},
385449
},
386450
},
387451
{
@@ -421,6 +485,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
421485
},
422486
},
423487
},
488+
Range: Range{
489+
From: Position{Index: 0, Line: 0, Col: 0},
490+
To: Position{Index: 56, Line: 2, Col: 1},
491+
},
424492
},
425493
},
426494
{
@@ -440,6 +508,10 @@ func TestTemplElementExpressionParser(t *testing.T) {
440508
To: Position{50, 3, 2},
441509
},
442510
},
511+
Range: Range{
512+
From: Position{Index: 0, Line: 0, Col: 0},
513+
To: Position{Index: 50, Line: 3, Col: 2},
514+
},
443515
},
444516
},
445517
}

parser/v2/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ type TemplElementExpression struct {
11811181
Expression Expression
11821182
// Children returns the elements in a block element.
11831183
Children []Node
1184+
Range Range
11841185
}
11851186

11861187
func (tee TemplElementExpression) ChildNodes() []Node {

0 commit comments

Comments
 (0)