Skip to content

Loading…

Preserve _ prefixed to variable names when generating erlang #164

Merged
merged 1 commit into from

2 participants

@RJ
RJ commented

Completely backwards compatible, but means we can enable
warnings_as_errors, and name unused variables with an underscore.

Otherwise it's impossible to use warnings_as_errors if you need to for loop over a proplist with keys/vals and you only want the vals. (for example).

See 2 new tests for the details

@RJ RJ Preserve _ prefixed to variable names when generating erlang
Completely backwards compatible, but means we can enable
warnings_as_errors, and name unused variables with an underscore.
(see 2 new tests)
f38e759
@RJ
RJ commented

Other options include:

1) always prefix all variables with _ in the erlydtl_beam_compiler

2) passing nowarn_unused_vars to the erlang compiler, possibly via compiler_options?

@kaos
ErlyDTL member
@kaos
ErlyDTL member

:+1: Looks good. I'll just touch up the name varify a bit..

I prefer this instead of messing around with compiler options. nowarn_unused_vars can be passed using compiler_options already, and can be a workable option for those that don't like to use _ prefixes in their templates.

@kaos kaos merged commit f38e759 into erlydtl:master

1 check passed

Details continuous-integration/travis-ci The Travis CI build passed
@kaos kaos added internal compiler syntax and removed internal labels
@kaos kaos added this to the 0.9.4 milestone
@kaos kaos self-assigned this
@jwheare jwheare deleted the irccloud:unused-vars-fix branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 14, 2014
  1. @RJ

    Preserve _ prefixed to variable names when generating erlang

    RJ committed
    Completely backwards compatible, but means we can enable
    warnings_as_errors, and name unused variables with an underscore.
    (see 2 new tests)
Showing with 17 additions and 7 deletions.
  1. +10 −7 src/erlydtl_beam_compiler.erl
  2. +7 −0 test/erlydtl_test_defs.erl
View
17 src/erlydtl_beam_compiler.erl
@@ -1252,7 +1252,7 @@ with_ast(ArgList, Contents, TreeWalker) ->
NewScope = lists:map(
fun ({{identifier, _, LocalVarName}, _Value}) ->
- {LocalVarName, merl:var(lists:concat(["Var_", LocalVarName]))}
+ {LocalVarName, varify(LocalVarName)}
end,
ArgList),
@@ -1268,7 +1268,7 @@ with_ast(ArgList, Contents, TreeWalker) ->
scope_as(VarName, Contents, TreeWalker) ->
{{ContentsAst, ContentsInfo}, TreeWalker1} = body_ast(Contents, TreeWalker),
- VarAst = merl:var(lists:concat(["Var_", VarName])),
+ VarAst = varify(VarName),
{Id, TreeWalker2} = begin_scope(
{[{VarName, VarAst}],
[?Q("_@VarAst = _@ContentsAst")]},
@@ -1277,7 +1277,7 @@ scope_as(VarName, Contents, TreeWalker) ->
regroup_ast(ListVariable, GrouperVariable, LocalVarName, TreeWalker) ->
{{ListAst, ListInfo}, TreeWalker1} = value_ast(ListVariable, false, true, TreeWalker),
- LocalVarAst = merl:var(lists:concat(["Var_", LocalVarName])),
+ LocalVarAst = varify(LocalVarName),
{Id, TreeWalker2} = begin_scope(
{[{LocalVarName, LocalVarAst}],
@@ -1316,10 +1316,8 @@ for_loop_ast(IteratorList, LoopValue, IsReversed, Contents,
%% setup
VarScope = lists:map(
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),
{Iterators, IteratorVars} = lists:unzip(VarScope),
IteratorCount = length(IteratorVars),
@@ -1530,3 +1528,8 @@ create_scope(Vars, VarScope) ->
create_scope(Vars, {Row, Col}, #treewalker{ context=Context }) ->
Level = length(Context#dtl_context.local_scopes),
create_scope(Vars, lists:concat(["/", Level, "_", Row, ":", Col])).
+
+varify([$_|VarName]) ->
+ merl:var(lists:concat(["_Var__", VarName]));
+varify(VarName) ->
+ merl:var(lists:concat(["Var_", VarName])).
View
7 test/erlydtl_test_defs.erl
@@ -311,6 +311,13 @@ all_test_defs() ->
<<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
[{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
<<"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",
<<"{% 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">>,
Something went wrong with that request. Please try again.