Skip to content

Commit c9c38d5

Browse files
authored
parser: check for using comptime $veb.html()/$vweb.html(), without importing veb or vweb (#21957)
1 parent 5e435a7 commit c9c38d5

8 files changed

+43
-4
lines changed

examples/vweb/veb_example.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ pub fn (mut app App) index(mut ctx Context) veb.Result {
4646
show := true
4747
hello := 'Hello world from veb, request number: ${c}'
4848
numbers := [1, 2, 3]
49-
return $vweb.html()
49+
return $veb.html()
5050
}
5151

5252
pub fn (mut app App) custom_template(mut ctx Context) veb.Result {
53-
return $vweb.html('custom.html')
53+
return $veb.html('custom.html')
5454
}
5555

5656
pub fn (mut app App) show_text(mut ctx Context) veb.Result {

vlib/v/ast/ast.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,7 @@ pub:
19381938
method_pos token.Pos
19391939
scope &Scope = unsafe { nil }
19401940
is_vweb bool
1941+
is_veb bool
19411942
is_embed bool // $embed_file(...)
19421943
is_env bool // $env(...) // TODO: deprecate after $d() is stable
19431944
is_compile_value bool // $d(...)

vlib/v/fmt/fmt.v

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,11 +2153,19 @@ pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) {
21532153
if node.is_vweb {
21542154
if node.method_name == 'html' {
21552155
if node.args.len == 1 && node.args[0].expr is ast.StringLiteral {
2156-
f.write('\$vweb.html(')
2156+
if node.is_veb {
2157+
f.write('\$veb.html(')
2158+
} else {
2159+
f.write('\$vweb.html(')
2160+
}
21572161
f.expr(node.args[0].expr)
21582162
f.write(')')
21592163
} else {
2160-
f.write('\$vweb.html()')
2164+
if node.is_veb {
2165+
f.write('\$veb.html()')
2166+
} else {
2167+
f.write('\$vweb.html()')
2168+
}
21612169
}
21622170
} else {
21632171
f.write('\$tmpl(')

vlib/v/parser/comptime.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,25 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
106106
}
107107
start_pos := p.tok.pos()
108108
p.check(.dollar)
109+
mut is_veb := false
109110
error_msg := 'only `\$tmpl()`, `\$env()`, `\$embed_file()`, `\$pkgconfig()`, `\$vweb.html()`, `\$compile_error()`, `\$compile_warn()`, `\$d()` and `\$res()` comptime functions are supported right now'
110111
if p.peek_tok.kind == .dot {
111112
name := p.check_name() // skip `vweb.html()` TODO
112113
if name != 'vweb' && name != 'veb' {
113114
p.error(error_msg)
114115
return err_node
115116
}
117+
import_mods := p.ast_imports.map(it.mod)
118+
if name == 'vweb' && 'vweb' !in import_mods && 'x.vweb' !in import_mods {
119+
p.error_with_pos('`\$vweb` cannot be used without importing vweb', start_pos.extend(p.prev_tok.pos()))
120+
return err_node
121+
} else if name == 'veb' {
122+
if 'veb' !in import_mods {
123+
p.error_with_pos('`\$veb` cannot be used without importing veb', start_pos.extend(p.prev_tok.pos()))
124+
return err_node
125+
}
126+
is_veb = true
127+
}
116128
p.check(.dot)
117129
}
118130
method_name := p.check_name()
@@ -265,6 +277,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
265277
return ast.ComptimeCall{
266278
scope: unsafe { nil }
267279
is_vweb: true
280+
is_veb: is_veb
268281
method_name: method_name
269282
args_var: literal_string_param
270283
args: [arg]
@@ -306,6 +319,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
306319
return ast.ComptimeCall{
307320
scope: unsafe { nil }
308321
is_vweb: true
322+
is_veb: is_veb
309323
vweb_tmpl: file
310324
method_name: method_name
311325
args_var: literal_string_param
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/parser/tests/comptime_veb_without_import_veb_err.vv:2:2: error: `$veb` cannot be used without importing veb
2+
1 | fn main() {
3+
2 | $veb.html('index.html')
4+
| ~~~~
5+
3 | }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
$veb.html('index.html')
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.vv:2:2: error: `$vweb` cannot be used without importing vweb
2+
1 | fn main() {
3+
2 | $vweb.html('index.html')
4+
| ~~~~~
5+
3 | }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
$vweb.html('index.html')
3+
}

0 commit comments

Comments
 (0)