Permalink
Browse files

Report line numbers of undefined variables

  • Loading branch information...
1 parent ce25e69 commit dda4db043a373368f43d0b37411a37fbfe383224 @evanmiller evanmiller committed Feb 8, 2013
Showing with 25 additions and 7 deletions.
  1. +16 −4 src/erlydtl_compiler.erl
  2. +9 −3 src/erlydtl_runtime.erl
View
@@ -999,16 +999,28 @@ resolve_variable_ast(VarTuple, Context, true) ->
resolve_variable_ast(VarTuple, Context, false) ->
resolve_variable_ast1(VarTuple, Context, 'find_value').
-resolve_variable_ast1({attribute, {{identifier, _, AttrName}, Variable}}, Context, FinderFunction) ->
+resolve_variable_ast1({attribute, {{identifier, {Row, Col}, AttrName}, Variable}}, Context, FinderFunction) ->
{VarAst, VarName} = resolve_variable_ast1(Variable, Context, FinderFunction),
+ FileNameAst = case Context#dtl_context.parse_trail of
+ [] -> erl_syntax:atom(undefined);
+ [H|_] -> erl_syntax:string(H)
+ end,
{erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(FinderFunction),
- [erl_syntax:atom(AttrName), VarAst]), VarName};
+ [erl_syntax:atom(AttrName), VarAst, FileNameAst,
+ erl_syntax:tuple([erl_syntax:integer(Row), erl_syntax:integer(Col)])
+ ]), VarName};
-resolve_variable_ast1({variable, {identifier, _, VarName}}, Context, FinderFunction) ->
+resolve_variable_ast1({variable, {identifier, {Row, Col}, VarName}}, Context, FinderFunction) ->
VarValue = case resolve_scoped_variable_ast(VarName, Context) of
undefined ->
+ FileNameAst = case Context#dtl_context.parse_trail of
+ [] -> erl_syntax:atom(undefined);
+ [H|_] -> erl_syntax:string(H)
+ end,
erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(FinderFunction),
- [erl_syntax:atom(VarName), erl_syntax:variable("_Variables")]);
+ [erl_syntax:atom(VarName), erl_syntax:variable("_Variables"), FileNameAst,
+ erl_syntax:tuple([erl_syntax:integer(Row), erl_syntax:integer(Col)])
+ ]);
Val ->
Val
end,
View
@@ -4,6 +4,9 @@
-define(IFCHANGED_CONTEXT_VARIABLE, erlydtl_ifchanged_context).
+find_value(Key, Data, _, _) ->
+ find_value(Key, Data).
+
find_value(_, undefined) ->
undefined;
find_value(Key, Fun) when is_function(Fun, 1) ->
@@ -56,10 +59,13 @@ find_deep_value([Key|Rest],Item) ->
end;
find_deep_value([],Item) -> Item.
-fetch_value(Key, Data) ->
+fetch_value(Key, Data, FileName, Pos) ->
case find_value(Key, Data) of
undefined ->
- throw({undefined_variable, Key});
+ throw({undefined_variable,
+ [{name, Key},
+ {file, FileName},
+ {line, Pos}]});
Val ->
Val
end.
@@ -262,7 +268,7 @@ ifchanged2({Key, Value}, IfChangedContext) ->
end.
cycle(NamesTuple, Counters) when is_tuple(NamesTuple) ->
- element(fetch_value(counter0, Counters) rem size(NamesTuple) + 1, NamesTuple).
+ element(find_value(counter0, Counters) rem size(NamesTuple) + 1, NamesTuple).
widthratio(Numerator, Denominator, Scale) ->
round(Numerator / Denominator * Scale).

0 comments on commit dda4db0

Please sign in to comment.