diff --git a/examples/vweb/veb_example.v b/examples/vweb/veb_example.v index 6b6efafa61..b20df58067 100644 --- a/examples/vweb/veb_example.v +++ b/examples/vweb/veb_example.v @@ -46,11 +46,11 @@ pub fn (mut app App) index(mut ctx Context) veb.Result { show := true hello := 'Hello world from veb, request number: ${c}' numbers := [1, 2, 3] - return $vweb.html() + return $veb.html() } pub fn (mut app App) custom_template(mut ctx Context) veb.Result { - return $vweb.html('custom.html') + return $veb.html('custom.html') } pub fn (mut app App) show_text(mut ctx Context) veb.Result { diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index e490bb0061..b43cb419dd 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1938,6 +1938,7 @@ pub: method_pos token.Pos scope &Scope = unsafe { nil } is_vweb bool + is_veb bool is_embed bool // $embed_file(...) is_env bool // $env(...) // TODO: deprecate after $d() is stable is_compile_value bool // $d(...) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 551be0c59e..f582abdb3c 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2153,11 +2153,19 @@ pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) { if node.is_vweb { if node.method_name == 'html' { if node.args.len == 1 && node.args[0].expr is ast.StringLiteral { - f.write('\$vweb.html(') + if node.is_veb { + f.write('\$veb.html(') + } else { + f.write('\$vweb.html(') + } f.expr(node.args[0].expr) f.write(')') } else { - f.write('\$vweb.html()') + if node.is_veb { + f.write('\$veb.html()') + } else { + f.write('\$vweb.html()') + } } } else { f.write('\$tmpl(') diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 367a555e17..1c0f8f2d98 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -106,6 +106,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { } start_pos := p.tok.pos() p.check(.dollar) + mut is_veb := false error_msg := 'only `\$tmpl()`, `\$env()`, `\$embed_file()`, `\$pkgconfig()`, `\$vweb.html()`, `\$compile_error()`, `\$compile_warn()`, `\$d()` and `\$res()` comptime functions are supported right now' if p.peek_tok.kind == .dot { name := p.check_name() // skip `vweb.html()` TODO @@ -113,6 +114,17 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { p.error(error_msg) return err_node } + import_mods := p.ast_imports.map(it.mod) + if name == 'vweb' && 'vweb' !in import_mods && 'x.vweb' !in import_mods { + p.error_with_pos('`\$vweb` cannot be used without importing vweb', start_pos.extend(p.prev_tok.pos())) + return err_node + } else if name == 'veb' { + if 'veb' !in import_mods { + p.error_with_pos('`\$veb` cannot be used without importing veb', start_pos.extend(p.prev_tok.pos())) + return err_node + } + is_veb = true + } p.check(.dot) } method_name := p.check_name() @@ -265,6 +277,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { return ast.ComptimeCall{ scope: unsafe { nil } is_vweb: true + is_veb: is_veb method_name: method_name args_var: literal_string_param args: [arg] @@ -306,6 +319,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { return ast.ComptimeCall{ scope: unsafe { nil } is_vweb: true + is_veb: is_veb vweb_tmpl: file method_name: method_name args_var: literal_string_param diff --git a/vlib/v/parser/tests/comptime_veb_without_import_veb_err.out b/vlib/v/parser/tests/comptime_veb_without_import_veb_err.out new file mode 100644 index 0000000000..9a0c6e1ad6 --- /dev/null +++ b/vlib/v/parser/tests/comptime_veb_without_import_veb_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/comptime_veb_without_import_veb_err.vv:2:2: error: `$veb` cannot be used without importing veb + 1 | fn main() { + 2 | $veb.html('index.html') + | ~~~~ + 3 | } diff --git a/vlib/v/parser/tests/comptime_veb_without_import_veb_err.vv b/vlib/v/parser/tests/comptime_veb_without_import_veb_err.vv new file mode 100644 index 0000000000..2c15f63434 --- /dev/null +++ b/vlib/v/parser/tests/comptime_veb_without_import_veb_err.vv @@ -0,0 +1,3 @@ +fn main() { + $veb.html('index.html') +} diff --git a/vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.out b/vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.out new file mode 100644 index 0000000000..56feeb808a --- /dev/null +++ b/vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.vv:2:2: error: `$vweb` cannot be used without importing vweb + 1 | fn main() { + 2 | $vweb.html('index.html') + | ~~~~~ + 3 | } diff --git a/vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.vv b/vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.vv new file mode 100644 index 0000000000..2819de6d0f --- /dev/null +++ b/vlib/v/parser/tests/comptime_vweb_without_import_vweb_err.vv @@ -0,0 +1,3 @@ +fn main() { + $vweb.html('index.html') +}