From 3664bea912457c5f3800d59a46159708a59adee1 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Wed, 10 Jun 2020 02:47:51 +1000 Subject: [PATCH] vweb/checker: show tmpl var errors --- vlib/v/checker/checker.v | 4 ++++ vlib/v/gen/comptime.v | 2 +- vlib/v/parser/comptime.v | 15 ++++++++++----- vlib/v/parser/parser.v | 4 ++-- vlib/vweb/tmpl/tmpl.v | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 365bb95940..100649c728 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1853,6 +1853,10 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { if it.is_vweb { mut c2 := new_checker(c.table, c.pref) c2.check(it.vweb_tmpl) + c.warnings << c2.warnings + c.errors << c2.errors + c.nr_warnings += c2.nr_warnings + c.nr_errors += c2.nr_errors } return table.void_type } diff --git a/vlib/v/gen/comptime.v b/vlib/v/gen/comptime.v index 5d28cff79c..f9c4a32af8 100644 --- a/vlib/v/gen/comptime.v +++ b/vlib/v/gen/comptime.v @@ -18,7 +18,7 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) { } } } - g.writeln('vweb__Context_html(&app->vweb, _tmpl_res)') + g.writeln('vweb__Context_html(&app->vweb, tmpl_res_$g.fn_decl.name)') return } g.writeln('// $' + 'method call. sym="$node.sym.name"') diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 5c2e76cb66..424086db24 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -105,7 +105,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall { start_pos: 0 parent: p.global_scope } - file := parse_text(v_code, p.table, scope, p.global_scope) + file := parse_text(v_code, p.table, p.pref, scope, p.global_scope) if p.pref.is_verbose { println('\n\n') println('>>> vweb template for ${path}:') @@ -117,12 +117,17 @@ fn (mut p Parser) vweb() ast.ComptimeCall { for stmt in file.stmts { if stmt is ast.FnDecl { fn_decl := stmt as ast.FnDecl - if fn_decl.name.starts_with('vweb_tmpl') { - body_scope := file.scope.innermost(fn_decl.body_pos.pos) + if fn_decl.name == 'vweb_tmpl_$p.cur_fn_name' { + tmpl_scope := file.scope.innermost(fn_decl.body_pos.pos) for _, obj in p.scope.objects { if obj is ast.Var { - v := obj as ast.Var - body_scope.register(v.name, *v) + mut v := obj as ast.Var + tmpl_scope.register(v.name, *v) + // TODO: this is yuck, track idents in parser + // or defer unused var logic to checker + if v_code.contains(v.name) { + v.is_used = true + } } } break diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index eafe14b337..059839a084 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -75,12 +75,12 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt { return p.stmt() } -pub fn parse_text(text string, b_table &table.Table, scope, global_scope &ast.Scope) ast.File { +pub fn parse_text(text string, b_table &table.Table, pref &pref.Preferences, scope, global_scope &ast.Scope) ast.File { s := scanner.new_scanner(text, .skip_comments) mut p := Parser{ scanner: s table: b_table - pref: &pref.Preferences{} + pref: pref scope: scope errors: []errors.Error{} warnings: []errors.Warning{} diff --git a/vlib/vweb/tmpl/tmpl.v b/vlib/vweb/tmpl/tmpl.v index b0bffbb395..f65749d6cb 100644 --- a/vlib/vweb/tmpl/tmpl.v +++ b/vlib/vweb/tmpl/tmpl.v @@ -100,7 +100,7 @@ pub fn compile_template(content, fn_name string) string { } } s.writeln(str_end) - s.writeln('_tmpl_res := sb.str() ') + s.writeln('tmpl_res_$fn_name := sb.str() ') s.writeln('}') s.writeln('// === end of vweb html template ===') return s.str()