1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix $tmpl with single quotes (fix #16154) (#16216)

This commit is contained in:
yuyi 2022-10-27 00:38:08 +08:00 committed by GitHub
parent a19a4ba299
commit 064e35fbc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 1 deletions

View File

@ -80,7 +80,10 @@ fn insert_template_code(fn_name string, tmpl_str_start string, line string) stri
round1 := ['\\', '\\\\', r"'", "\\'", r'@', r'$']
round2 := [r'$$', r'\@', r'.$', r'.@']
mut rline := line.replace_each(round1).replace_each(round2)
comptime_call_str := rline.find_between('\${', '}')
if comptime_call_str.contains("\\'") {
rline = rline.replace(comptime_call_str, comptime_call_str.replace("\\'", r"'"))
}
if rline.ends_with('\\') {
rline = rline[0..rline.len - 2] + trailing_bs
}

View File

@ -0,0 +1,3 @@
someval: @{s.someval('huh')}
someotherval: @{s.m['hah']}

View File

@ -0,0 +1,27 @@
module main
pub struct SomeThing {
pub:
m map[string]string
}
fn (s SomeThing) someval(what string) string {
return s.m[what]
}
fn (s SomeThing) template() string {
return $tmpl('tmpl/template.in')
}
fn test_tmpl_with_single_quotes() {
mut sm := map[string]string{}
sm['huh'] = 'monkey'
sm['hah'] = 'parrot'
s := SomeThing{sm}
result := s.template()
println(result)
assert result.trim_space() == 'someval: monkey
someotherval: parrot'
}