Skip to content

Commit 629d18c

Browse files
hhugovouillon
authored andcommitted
Compiler: new debug option to emit uniq variable names in pretty mode
1 parent caac132 commit 629d18c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

compiler/lib/js_assign.ml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,19 @@ module Preserve : Strategy = struct
292292
in
293293
t.scopes <- (defs, all) :: t.scopes
294294

295+
let uniq_var = Debug.find "var"
296+
295297
let allocate_variables t ~count:_ =
296298
let names = Array.make t.size "" in
299+
let uniq_var = uniq_var () in
300+
let unamed = ref 0 in
301+
let create_unamed =
302+
if uniq_var
303+
then
304+
fun i ->
305+
"_" ^ Var_printer.Alphabet.to_string Var_printer.Alphabet.javascript i ^ "_"
306+
else fun i -> Var_printer.Alphabet.to_string Var_printer.Alphabet.javascript i
307+
in
297308
List.iter t.scopes ~f:(fun (defs, all) ->
298309
let reserved =
299310
IdentSet.fold
@@ -306,7 +317,7 @@ module Preserve : Strategy = struct
306317
all
307318
StringSet.empty
308319
in
309-
let unamed = ref 0 in
320+
if not uniq_var then unamed := 0;
310321
let _reserved =
311322
S.fold
312323
(fun var reserved ->
@@ -333,16 +344,12 @@ module Preserve : Strategy = struct
333344
Printf.sprintf "%s%d" expected_name !i
334345
| None ->
335346
while
336-
let name =
337-
Var_printer.Alphabet.to_string
338-
Var_printer.Alphabet.javascript
339-
!unamed
340-
in
347+
let name = create_unamed !unamed in
341348
StringSet.mem name reserved || StringSet.mem name Reserved.keyword
342349
do
343350
incr unamed
344351
done;
345-
Var_printer.Alphabet.to_string Var_printer.Alphabet.javascript !unamed
352+
create_unamed !unamed
346353
in
347354
names.(Var.idx var) <- name;
348355
StringSet.add name reserved)

0 commit comments

Comments
 (0)