Browse Source

Merge branch 'unused-vars-fix' of https://github.com/irccloud/erlydtl into irccloud-unused-vars-fix

Andreas Stenius 11 years ago
parent
commit
b04b23861f
2 changed files with 17 additions and 7 deletions
  1. 10 7
      src/erlydtl_beam_compiler.erl
  2. 7 0
      test/erlydtl_test_defs.erl

+ 10 - 7
src/erlydtl_beam_compiler.erl

@@ -1255,7 +1255,7 @@ with_ast(ArgList, Contents, TreeWalker) ->
 
 
     NewScope = lists:map(
     NewScope = lists:map(
                  fun ({{identifier, _, LocalVarName}, _Value}) ->
                  fun ({{identifier, _, LocalVarName}, _Value}) ->
-                         {LocalVarName, merl:var(lists:concat(["Var_", LocalVarName]))}
+                         {LocalVarName, varify(LocalVarName)}
                  end,
                  end,
                  ArgList),
                  ArgList),
 
 
@@ -1271,7 +1271,7 @@ with_ast(ArgList, Contents, TreeWalker) ->
 
 
 scope_as(VarName, Contents, TreeWalker) ->
 scope_as(VarName, Contents, TreeWalker) ->
     {{ContentsAst, ContentsInfo}, TreeWalker1} = body_ast(Contents, TreeWalker),
     {{ContentsAst, ContentsInfo}, TreeWalker1} = body_ast(Contents, TreeWalker),
-    VarAst = merl:var(lists:concat(["Var_", VarName])),
+    VarAst = varify(VarName),
     {Id, TreeWalker2} = begin_scope(
     {Id, TreeWalker2} = begin_scope(
                           {[{VarName, VarAst}],
                           {[{VarName, VarAst}],
                            [?Q("_@VarAst = _@ContentsAst")]},
                            [?Q("_@VarAst = _@ContentsAst")]},
@@ -1280,7 +1280,7 @@ scope_as(VarName, Contents, TreeWalker) ->
 
 
 regroup_ast(ListVariable, GrouperVariable, LocalVarName, TreeWalker) ->
 regroup_ast(ListVariable, GrouperVariable, LocalVarName, TreeWalker) ->
     {{ListAst, ListInfo}, TreeWalker1} = value_ast(ListVariable, false, true, TreeWalker),
     {{ListAst, ListInfo}, TreeWalker1} = value_ast(ListVariable, false, true, TreeWalker),
-    LocalVarAst = merl:var(lists:concat(["Var_", LocalVarName])),
+    LocalVarAst = varify(LocalVarName),
 
 
     {Id, TreeWalker2} = begin_scope(
     {Id, TreeWalker2} = begin_scope(
                           {[{LocalVarName, LocalVarAst}],
                           {[{LocalVarName, LocalVarAst}],
@@ -1319,10 +1319,8 @@ for_loop_ast(IteratorList, LoopValue, IsReversed, Contents,
     %% setup
     %% setup
     VarScope = lists:map(
     VarScope = lists:map(
                  fun({identifier, {R, C}, Iterator}) ->
                  fun({identifier, {R, C}, Iterator}) ->
-                         {Iterator, merl:var(
-                                      lists:concat(["Var_", Iterator,
-                                                    "/", Level, "_", R, ":", C
-                                                   ]))}
+                         {Iterator, varify(lists:concat([
+                                    Iterator,"/", Level, "_", R, ":", C]))}
                  end, IteratorList),
                  end, IteratorList),
     {Iterators, IteratorVars} = lists:unzip(VarScope),
     {Iterators, IteratorVars} = lists:unzip(VarScope),
     IteratorCount = length(IteratorVars),
     IteratorCount = length(IteratorVars),
@@ -1533,3 +1531,8 @@ create_scope(Vars, VarScope) ->
 create_scope(Vars, {Row, Col}, #treewalker{ context=Context }) ->
 create_scope(Vars, {Row, Col}, #treewalker{ context=Context }) ->
     Level = length(Context#dtl_context.local_scopes),
     Level = length(Context#dtl_context.local_scopes),
     create_scope(Vars, lists:concat(["/", Level, "_", Row, ":", Col])).
     create_scope(Vars, lists:concat(["/", Level, "_", Row, ":", Col])).
+
+varify([$_|VarName]) ->
+    merl:var(lists:concat(["_Var__", VarName]));
+varify(VarName) ->
+    merl:var(lists:concat(["Var_", VarName])).

+ 7 - 0
test/erlydtl_test_defs.erl

@@ -311,6 +311,13 @@ all_test_defs() ->
         <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
         <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
         [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
         [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
         <<"Al\nAlbert\nJo\nJoseph\n">>},
         <<"Al\nAlbert\nJo\nJoseph\n">>},
+       {"Unused variable in foreach proplist",
+        <<"{% for k,v in plist %}{{v}}{% endfor %}">>,
+        [{'plist',[{1,"one"},{2,"two"}]}], [], [], <<"onetwo">>,
+        [error_info([{0, erl_lint, {unused_var, 'Var_k/1_1:8'}}])]},
+       {"Unused variable in foreach proplist, prefixed with underscore",
+        <<"{% for _k,v in plist %}{{v}}{% endfor %}">>,
+        [{'plist',[{1,"one"},{2,"two"}]}], [], [], <<"onetwo">>},
        {"Access parent loop counters",
        {"Access parent loop counters",
         <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
         <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
         [{'list', [["One", "two"], ["One", "two"]]}], [], [], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>,
         [{'list', [["One", "two"], ["One", "two"]]}], [], [], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>,