Skip to content

Commit d198643

Browse files
author
razvan
committed
fix(blade): address PR #45 review — regex capture, EndLine, log clarity
- Fix regex patterns: changed (.+?) to ([^'"]+) to capture only the first quoted argument in multi-arg directives like @section('title', 'Dashboard') - Add EndLine to blade CodeChunks via new BladeTemplate.TotalLines field - Fix misleading 'Enrich DONE' log that appeared before blade analysis - Strengthen tests with value assertions (section names, view names, TotalLines)
1 parent 059ffee commit d198643

5 files changed

Lines changed: 42 additions & 15 deletions

File tree

pkg/parser/php/laravel/adapter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,19 @@ func (a *Adapter) convertBladeToChunks(templates []BladeTemplate) []php.CodeChun
297297
})
298298
}
299299

300+
// EndLine defaults to TotalLines (whole template), fallback to 1 for empty files
301+
endLine := tpl.TotalLines
302+
if endLine == 0 {
303+
endLine = 1
304+
}
305+
300306
chunk := php.CodeChunk{
301307
Name: tpl.Name,
302308
Type: "blade_template",
303309
Language: "php",
304310
FilePath: tpl.FilePath,
305311
StartLine: 1,
312+
EndLine: endLine,
306313
Signature: sig,
307314
Docstring: docstring,
308315
Metadata: map[string]any{

pkg/parser/php/laravel/blade.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212

1313
// Compiled regex patterns for Blade directives
1414
var (
15-
reExtends = regexp.MustCompile(`@extends\(\s*['"](.+?)['"]\s*\)`)
16-
reSection = regexp.MustCompile(`@section\(\s*['"](.+?)['"]\s*\)`)
17-
reYield = regexp.MustCompile(`@yield\(\s*['"](.+?)['"]\s*\)`)
18-
reInclude = regexp.MustCompile(`@include\(\s*['"](.+?)['"]\s*\)`)
19-
reComponent = regexp.MustCompile(`@component\(\s*['"](.+?)['"]\s*\)`)
20-
reEach = regexp.MustCompile(`@each\(\s*['"](.+?)['"]\s*\)`)
21-
rePushStack = regexp.MustCompile(`@(?:push|stack)\(\s*['"](.+?)['"]\s*\)`)
15+
reExtends = regexp.MustCompile(`@extends\(\s*['"]([^'"]+)['"]`)
16+
reSection = regexp.MustCompile(`@section\(\s*['"]([^'"]+)['"]`)
17+
reYield = regexp.MustCompile(`@yield\(\s*['"]([^'"]+)['"]`)
18+
reInclude = regexp.MustCompile(`@include\(\s*['"]([^'"]+)['"]`)
19+
reComponent = regexp.MustCompile(`@component\(\s*['"]([^'"]+)['"]`)
20+
reEach = regexp.MustCompile(`@each\(\s*['"]([^'"]+)['"]`)
21+
rePushStack = regexp.MustCompile(`@(?:push|stack)\(\s*['"]([^'"]+)['"]`)
2222
reProps = regexp.MustCompile(`@props\(\s*\[(.*?)\]\s*\)`)
2323
)
2424

@@ -128,6 +128,8 @@ func (ba *BladeAnalyzer) analyzeFile(filePath string) (BladeTemplate, error) {
128128
}
129129
}
130130

131+
tpl.TotalLines = lineNum
132+
131133
return tpl, scanner.Err()
132134
}
133135

pkg/parser/php/laravel/blade_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,20 @@ func TestBladeAnalyzer_Includes(t *testing.T) {
105105
}
106106

107107
types := map[string]int{}
108+
viewNames := map[string]bool{}
108109
for _, inc := range tpl.Includes {
109110
types[inc.Type]++
111+
viewNames[inc.ViewName] = true
110112
}
111113
if types["include"] != 1 || types["component"] != 1 || types["each"] != 1 {
112114
t.Errorf("unexpected include types: %v", types)
113115
}
116+
// Verify actual captured view names (not garbage from multi-arg forms)
117+
for _, expected := range []string{"partials.header", "components.alert", "partials.item"} {
118+
if !viewNames[expected] {
119+
t.Errorf("missing expected view name %q, got %v", expected, viewNames)
120+
}
121+
}
114122
}
115123

116124
func TestBladeAnalyzer_PushStack(t *testing.T) {
@@ -218,12 +226,21 @@ func TestBladeAnalyzer_ComplexTemplate(t *testing.T) {
218226
if len(tpl.Sections) != 2 {
219227
t.Errorf("expected 2 sections, got %d", len(tpl.Sections))
220228
}
229+
// Verify section names are correctly captured (not "title', 'Dashboard" from multi-arg)
230+
for _, s := range tpl.Sections {
231+
if s.Name != "title" && s.Name != "content" {
232+
t.Errorf("unexpected section name %q, want 'title' or 'content'", s.Name)
233+
}
234+
}
221235
if len(tpl.Includes) != 3 {
222236
t.Errorf("expected 3 includes (2 include + 1 component), got %d", len(tpl.Includes))
223237
}
224238
if len(tpl.Stacks) != 2 {
225239
t.Errorf("expected 2 stacks (scripts, styles), got %d", len(tpl.Stacks))
226240
}
241+
if tpl.TotalLines != 21 {
242+
t.Errorf("TotalLines = %d, want 21", tpl.TotalLines)
243+
}
227244
}
228245

229246
func TestBladeViewName(t *testing.T) {

pkg/parser/php/laravel/enricher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (e *Enricher) Enrich(ca *php.CodeAnalyzer, packages []*php.PackageInfo, pat
123123
}
124124
}
125125

126-
logger.Instance.Debug("[LARAVEL] Enrich DONE: returning %d total chunks (before blade)", len(chunks))
126+
logger.Instance.Debug("[LARAVEL] Enrich: %d chunks before blade analysis", len(chunks))
127127

128128
// Analyze Blade Templates
129129
bladeFiles := e.adapter.findBladeFiles(paths)

pkg/parser/php/laravel/types.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,14 @@ type Middleware struct {
132132

133133
// BladeTemplate represents a parsed Blade template with its directives
134134
type BladeTemplate struct {
135-
Name string `json:"name"` // dot notation: layouts.app
136-
FilePath string `json:"file_path"`
137-
Extends string `json:"extends,omitempty"` // @extends('...')
138-
Sections []BladeSection `json:"sections,omitempty"`
139-
Includes []BladeInclude `json:"includes,omitempty"`
140-
Stacks []string `json:"stacks,omitempty"` // @push/@stack names
141-
Props []string `json:"props,omitempty"` // @props([...])
135+
Name string `json:"name"` // dot notation: layouts.app
136+
FilePath string `json:"file_path"`
137+
Extends string `json:"extends,omitempty"` // @extends('...')
138+
Sections []BladeSection `json:"sections,omitempty"`
139+
Includes []BladeInclude `json:"includes,omitempty"`
140+
Stacks []string `json:"stacks,omitempty"` // @push/@stack names
141+
Props []string `json:"props,omitempty"` // @props([...])
142+
TotalLines int `json:"total_lines"` // total line count of the file
142143
}
143144

144145
// BladeSection represents a @section or @yield directive

0 commit comments

Comments
 (0)