parser: fix .${var} used in a template, compiled by $tmpl() (fix #22231) (#22270)

This commit is contained in:
Carlos Esquerdo Bernat 2024-09-22 07:22:24 +02:00 committed by GitHub
parent 59d5115483
commit 3115796756
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 3 deletions

View file

@ -77,9 +77,8 @@ fn insert_template_code(fn_name string, tmpl_str_start string, line string) stri
// HTML, may include `@var`
// escaped by cgen, unless it's a `vweb.RawHtml` string
trailing_bs := tmpl_str_end + 'sb_${fn_name}.write_u8(92)\n' + tmpl_str_start
round1 := ['\\', '\\\\', r"'", "\\'", r'@', r'$']
round2 := [r'$$', r'\@', r'.$', r'.@']
mut rline := line.replace_each(round1).replace_each(round2)
replace_pairs := ['\\', '\\\\', r"'", "\\'", r'@', r'$', r'$$', r'\@']
mut rline := line.replace_each(replace_pairs)
comptime_call_str := rline.find_between('\${', '}')
if comptime_call_str.contains("\\'") {
rline = rline.replace(comptime_call_str, comptime_call_str.replace("\\'", r"'"))

View file

@ -0,0 +1,2 @@
.@one
.${two}

View file

@ -0,0 +1,11 @@
fn template() string {
one := 1
two := 2
return $tmpl('tmpl/dot_var.txt')
}
fn test_tmpl_with_dot_var() {
assert template() == '.1
.2
'
}