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

parser: fix tmpl using variable or const path argument (fix #16941) (#16943)

This commit is contained in:
yuyi 2023-01-11 19:31:48 +08:00 committed by GitHub
parent 3e9b06031c
commit 3f8aa77990
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View File

@ -127,7 +127,18 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
pos: start_pos.extend(p.prev_tok.pos())
}
}
literal_string_param := if is_html { '' } else { p.tok.lit }
mut literal_string_param := if is_html { '' } else { p.tok.lit }
if p.tok.kind == .name {
if var := p.scope.find_var(p.tok.lit) {
if var.expr is ast.StringLiteral {
literal_string_param = var.expr.val
}
} else if var := p.scope.find_const(p.mod + '.' + p.tok.lit) {
if var.expr is ast.StringLiteral {
literal_string_param = var.expr.val
}
}
}
path_of_literal_string_param := literal_string_param.replace('/', os.path_separator)
mut arg := ast.CallArg{}
if !is_html {

View File

@ -0,0 +1,45 @@
const template_path = 'tmpl/template.in'
pub struct SomeThing {
pub:
m map[string]string
}
fn (s SomeThing) someval(what string) string {
return s.m[what]
}
fn (s SomeThing) template_variable() string {
path := 'tmpl/template.in'
return $tmpl('tmpl/template.in')
}
fn (s SomeThing) template_const() string {
return $tmpl('tmpl/template.in')
}
fn test_tmpl_with_variable_path() {
mut sm := map[string]string{}
sm['huh'] = 'monkey'
sm['hah'] = 'parrot'
s := SomeThing{sm}
result := s.template_variable()
println(result)
assert result.trim_space() == 'someval: monkey
someotherval: parrot'
}
fn test_tmpl_with_const_path() {
mut sm := map[string]string{}
sm['huh'] = 'monkey'
sm['hah'] = 'parrot'
s := SomeThing{sm}
result := s.template_const()
println(result)
assert result.trim_space() == 'someval: monkey
someotherval: parrot'
}