@@ -915,6 +915,32 @@ test("Minify", function()
915915 [[ for e,t in ipairs(global)do globalFunc1(t);for e=e,#global do globalFunc2(e);end end ]]
916916 )
917917
918+ -- Make sure we minify multi-name declaration-likes as much as possible without shadowing too much.
919+ testMinify (
920+ [[ local x, y = 1, 2 ; return x ]] ,
921+ [[ local e,t=1,2;return e; ]]
922+ )
923+ testMinify (
924+ [[ local x, y = 1, 2 ; return y ]] ,
925+ [[ local e,e=1,2;return e; ]]
926+ )
927+ testMinify (
928+ [[ for x, y in 1, 2 do return x end ]] ,
929+ [[ for e,t in 1,2 do return e;end ]]
930+ )
931+ testMinify (
932+ [[ for x, y in 1, 2 do return y end ]] ,
933+ [[ for e,e in 1,2 do return e;end ]]
934+ )
935+ testMinify (
936+ [[ function f(x, y) return x end ]] ,
937+ [[ function f(e,t)return e;end ]]
938+ )
939+ testMinify (
940+ [[ function f(x, y) return y end ]] ,
941+ [[ function f(e,e)return e;end ]]
942+ )
943+
918944 if _VERSION >= " Lua 5.2" then
919945 testMinify (
920946 [[
@@ -926,6 +952,39 @@ test("Minify", function()
926952 [[ local _ENV=_ENV;local function e()return _ENV;end ]]
927953 )
928954 end
955+
956+ -- Check that declaration references don't change.
957+ for _ , filename in ipairs {" test.lua" ," dumbParser.lua" } do
958+ local ast = assert (parser .parseFile (filename ))
959+ local names = {}
960+ local decls = {}
961+
962+ parser .updateReferences (ast )
963+
964+ parser .traverseTree (ast , function (node )
965+ if node .type == " identifier" then
966+ names [node ] = node .name
967+ decls [node ] = node .declaration
968+ end
969+ end )
970+
971+ parser .minify (ast )
972+ parser .updateReferences (ast )
973+ -- print("----------------------------------------------------------------") ; print(parser.toLua(ast, true))
974+
975+ local ok = true
976+
977+ parser .traverseTree (ast , function (node )
978+ if node .type == " identifier" and node .declaration ~= decls [node ] then
979+ ok = false
980+ print (parser .formatMessage (node , " Declaration changed for '%s' (is '%s')." , names [node ], node .name ))
981+ print (decls [node ] and parser .formatMessage (decls [node ] , " from..." ):gsub (" [^\n ]+" , " %0" ) or " from... nil" )
982+ print (node .declaration and parser .formatMessage (node .declaration , " to..." ):gsub (" [^\n ]+" , " %0" ) or " to... nil" )
983+ end
984+ end )
985+
986+ assert (ok , " Declarations changed!" )
987+ end
929988end )
930989
931990
0 commit comments