Skip to content

Commit

Permalink
improve object literal printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Hongbo Zhang committed Jun 21, 2016
1 parent dbf12b4 commit db73a51
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 268 deletions.
389 changes: 263 additions & 126 deletions jscomp/bin/compiler.ml

Large diffs are not rendered by default.

80 changes: 43 additions & 37 deletions jscomp/ext/ext_ident.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let reserved_words =
"debugger";"default";"delete";"do";
"else";
"finally";"for";"function";
"if"; "in";"instanceof";
"if"; "then"; "in";"instanceof";
"new";
"return";
"switch";
Expand Down Expand Up @@ -194,50 +194,56 @@ let reserved_map =
List.fold_left (fun acc x -> String_set.add x acc) String_set.empty
reserved_words





(* TODO:
check name conflicts with javascript conventions
{[
Ext_ident.convert "^";;
- : string = "$caret"
]}
*)
let convert (name : string) =
let module E = struct exception Not_normal_letter of int end in
let len = String.length name in
if String_set.mem name reserved_map then "$$" ^ name
let convert keyword (name : string) =
if keyword && String_set.mem name reserved_map then "$$" ^ name
else
try
for i = 0 to len - 1 do
let c = String.unsafe_get name i in
if not ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$' ) then
raise (E.Not_normal_letter i)
else ()
done;
name
with E.Not_normal_letter i ->
String.sub name 0 i ^
(let buffer = Buffer.create len in
for j = i to len - 1 do
let c = String.unsafe_get name j in
match c with
| '*' -> Buffer.add_string buffer "$star"
| '\'' -> Buffer.add_string buffer "$prime"
| '!' -> Buffer.add_string buffer "$bang"
| '>' -> Buffer.add_string buffer "$great"
| '<' -> Buffer.add_string buffer "$less"
| '=' -> Buffer.add_string buffer "$eq"
| '+' -> Buffer.add_string buffer "$plus"
| '-' -> Buffer.add_string buffer "$neg"
| '@' -> Buffer.add_string buffer "$at"
| '^' -> Buffer.add_string buffer "$caret"
| '/' -> Buffer.add_string buffer "$slash"
| '|' -> Buffer.add_string buffer "$pipe"
| '.' -> Buffer.add_string buffer "$dot"
| '%' -> Buffer.add_string buffer "$percent"
| '~' -> Buffer.add_string buffer "$tilde"
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
| _ -> Buffer.add_string buffer "$unknown"
done; Buffer.contents buffer)
let module E = struct exception Not_normal_letter of int end in
let len = String.length name in
try
for i = 0 to len - 1 do
let c = String.unsafe_get name i in
if
not ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$' )
then
raise (E.Not_normal_letter i)
done;
name
with E.Not_normal_letter i ->
String.sub name 0 i ^
(let buffer = Buffer.create len in
for j = i to len - 1 do
let c = String.unsafe_get name j in
match c with
| '*' -> Buffer.add_string buffer "$star"
| '\'' -> Buffer.add_string buffer "$prime"
| '!' -> Buffer.add_string buffer "$bang"
| '>' -> Buffer.add_string buffer "$great"
| '<' -> Buffer.add_string buffer "$less"
| '=' -> Buffer.add_string buffer "$eq"
| '+' -> Buffer.add_string buffer "$plus"
| '-' -> Buffer.add_string buffer "$neg"
| '@' -> Buffer.add_string buffer "$at"
| '^' -> Buffer.add_string buffer "$caret"
| '/' -> Buffer.add_string buffer "$slash"
| '|' -> Buffer.add_string buffer "$pipe"
| '.' -> Buffer.add_string buffer "$dot"
| '%' -> Buffer.add_string buffer "$percent"
| '~' -> Buffer.add_string buffer "$tilde"
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
| _ -> Buffer.add_string buffer "$unknown"
done; Buffer.contents buffer)


(* It is currently made a persistent ident to avoid fresh ids
which would result in different signature files
Expand Down
5 changes: 4 additions & 1 deletion jscomp/ext/ext_ident.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ val make_unused : unit -> Ident.t

val is_unused_ident : Ident.t -> bool

val convert : string -> string
(**
if name is not converted, the reference should be equal
*)
val convert : bool -> string -> string
val undefined : Ident.t
val is_js_or_global : Ident.t -> bool
val nil : Ident.t
88 changes: 44 additions & 44 deletions jscomp/js_cmj_datasets.ml

Large diffs are not rendered by default.

31 changes: 13 additions & 18 deletions jscomp/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ let str_of_ident (cxt : Ext_pp_scope.t) (id : Ident.t) =
[Printf.sprintf "%s$%d" name id.stamp] which is
not relevant to the context
*)
let name = Ext_ident.convert id.name in
let name = Ext_ident.convert true id.name in
let i,new_cxt = Ext_pp_scope.add_ident id cxt in
(* Attention:
$$Array.length, due to the fact that global module is
Expand Down Expand Up @@ -237,6 +237,12 @@ let pp_string f ?(quote='"') ?(utf=false) s =
P.string f quote_s
;;

let property_string f s =
let s' = Ext_ident.convert false s in
if s == s' then
P.string f s
else
pp_string f ~utf:true ~quote:(best_string_quote s) s

(* TODO: check utf's correct semantics *)
let pp_quote_string f s =
Expand Down Expand Up @@ -445,7 +451,7 @@ and vident cxt f (v : J.vident) =
| Qualified (id,_, Some name) ->
let cxt = ident cxt f id in
P.string f L.dot;
P.string f (Ext_ident.convert name);
P.string f (Ext_ident.convert true name);
cxt
end

Expand Down Expand Up @@ -519,17 +525,6 @@ and
(Call ({expression_desc = Dot(a,L.bind, true); comment = None }, [b],
{arity = Full; call_info = Call_na}))
end
(* | Tag_ml_obj e -> *)
(* P.group f 1 (fun _ -> *)
(* P.string f "Object.defineProperty"; *)
(* P.paren_group f 1 (fun _ -> *)
(* let cxt = expression 1 cxt f e in *)
(* P.string f L.comma; *)
(* P.space f ; *)
(* P.string f {|"##ml"|}; *)
(* P.string f L.comma; *)
(* P.string f {|{"value" : true, "writable" : false}|} ; *)
(* cxt )) *)

| FlatCall(e,el) ->
P.group f 1 (fun _ ->
Expand Down Expand Up @@ -934,13 +929,13 @@ and
cxt in
if l > 15 then P.paren_group f 1 action else action ()

| Dot (e, nm,normal) ->
| Dot (e, s,normal) ->
if normal then
begin
let action () =
let cxt = expression 15 cxt f e in
P.string f L.dot;
P.string f (Ext_ident.convert nm);
P.string f (Ext_ident.convert true s);
(* See [Js_program_loader.obj_of_exports]
maybe in the ast level we should have
refer and export
Expand All @@ -953,7 +948,7 @@ and
P.group f 1 @@ fun _ ->
let cxt = expression 15 cxt f e in
(P.bracket_group f 1 @@ fun _ ->
pp_string f (* ~utf:(kind = `Utf8) *) ~quote:( best_string_quote nm) nm);
pp_string f (* ~utf:(kind = `Utf8) *) ~quote:( best_string_quote s) s);
cxt
in
if l > 15 then P.paren_group f 1 action else action ()
Expand Down Expand Up @@ -1014,7 +1009,7 @@ and property_name cxt f (s : J.property_name) : unit =
| Tag -> P.string f L.tag
| Length -> P.string f L.length
| Key s ->
pp_string f ~utf:true ~quote:(best_string_quote s) s
property_string f s
| Int_key i -> P.string f (string_of_int i)

and property_name_and_value_list cxt f l : Ext_pp_scope.t =
Expand Down Expand Up @@ -1490,7 +1485,7 @@ and block cxt f b =
let exports cxt f (idents : Ident.t list) =
let outer_cxt, reversed_list, margin =
List.fold_left (fun (cxt, acc, len ) (id : Ident.t) ->
let s = Ext_ident.convert id.name in
let s = Ext_ident.convert true id.name in
let str,cxt = str_of_ident cxt id in
cxt, ( (s,str) :: acc ) , max len (String.length s) )
(cxt, [], 0) idents in
Expand Down
4 changes: 2 additions & 2 deletions jscomp/lam_stats_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let pp = Format.fprintf
let meaningless_names = ["*opt*"; "param";]

let rec dump_ident fmt (id : Ident.t) (arity : Lam_stats.function_arities) =
pp fmt "@[<2>export var %s:@ %a@ ;@]" (Ext_ident.convert id.name ) dump_arity arity
pp fmt "@[<2>export var %s:@ %a@ ;@]" (Ext_ident.convert true id.name ) dump_arity arity

and dump_arity fmt (arity : Lam_stats.function_arities) =
match arity with
Expand All @@ -50,7 +50,7 @@ and dump_arity fmt (arity : Lam_stats.function_arities) =
Format.pp_print_space fmt ();
)
(fun fmt ident -> pp fmt "@[%s@ :@ any@]"
(Ext_ident.convert @@ Ident.name ident))
(Ext_ident.convert true @@ Ident.name ident))
) args


Expand Down
4 changes: 4 additions & 0 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ printf_sim.cmj : ../stdlib/printf.cmi
printf_sim.cmx : ../stdlib/printf.cmx
printf_test.cmj : ../stdlib/printf.cmi mt.cmi ../stdlib/format.cmi
printf_test.cmx : ../stdlib/printf.cmx mt.cmx ../stdlib/format.cmx
promise.cmj : ../runtime/js.cmj
promise.cmx : ../runtime/js.cmx
qcc.cmj : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/printf.cmi \
../stdlib/list.cmi ../stdlib/char.cmi ../stdlib/bytes.cmi \
../stdlib/array.cmi
Expand Down Expand Up @@ -1050,6 +1052,8 @@ printf_sim.cmo : ../stdlib/printf.cmi
printf_sim.cmj : ../stdlib/printf.cmj
printf_test.cmo : ../stdlib/printf.cmi mt.cmi ../stdlib/format.cmi
printf_test.cmj : ../stdlib/printf.cmj mt.cmj ../stdlib/format.cmj
promise.cmo : ../runtime/js.cmo
promise.cmj : ../runtime/js.cmj
qcc.cmo : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/printf.cmi \
../stdlib/list.cmi ../stdlib/char.cmi ../stdlib/bytes.cmi \
../stdlib/array.cmi
Expand Down
4 changes: 3 additions & 1 deletion jscomp/test/test.mllib
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,6 @@ noassert

test_unsafe_cmp

gpr_441
gpr_441

promise
10 changes: 5 additions & 5 deletions lib/js/test/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ function ui_layout(compile, lookup, appContext) {
stackPanel.addChild(button);
var mk_titleRow = function (text) {
return {
"label": {
"text": text
label: {
text: text
}
};
};
var u = {
"width": 200
width: 200
};
grid.minHeight = 300;
grid.titleRows = /* array */[
Expand Down Expand Up @@ -92,8 +92,8 @@ function ui_layout(compile, lookup, appContext) {
var bid = price + 20 * Math.random();
var ask = price + 20 * Math.random();
var result = Curry._1(computeFunction[0], {
"bid": bid,
"ask": ask
bid: bid,
ask: ask
});
return /* array */[
mk_titleRow(param[/* ticker */0]),
Expand Down
4 changes: 2 additions & 2 deletions lib/js/test/demo_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ function f(param) {
}

ReactDom.render(React.createClass({
"render": function () {
render: function () {
return React.DOM.div({
"alt": "pic"
alt: "pic"
}, React.DOM.h1(null, "hello react"), React.DOM.h2(null, "type safe!"));
}
}), document.getElementById("hi"));
Expand Down
4 changes: 2 additions & 2 deletions lib/js/test/guide_for_ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

function mk() {
return {
"text": 32,
"label": "hel"
text: 32,
label: "hel"
};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/js/test/js_obj_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var suites_001 = /* :: */[
return /* Eq */Block.__(0, [
34,
f_js({
"say": function (x) {
say: function (x) {
return x + 2 | 0;
}
})
Expand Down
4 changes: 2 additions & 2 deletions lib/js/test/method_name_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var u = {
};

var h = {
"open": 3,
"end": 32
open: 3,
end: 32
};

function hg(x) {
Expand Down
6 changes: 3 additions & 3 deletions lib/js/test/nested_obj_literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


var structural_obj = {
"x": {
"y": {
"z": 3
x: {
y: {
z: 3
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions lib/js/test/nested_obj_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


var f = {
"x": {
"y": {
"z": 3
x: {
y: {
z: 3
}
}
};
Expand Down
12 changes: 6 additions & 6 deletions lib/js/test/obj_literal_ppx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@


var a = {
"x": 3,
"y": /* int array */[1]
x: 3,
y: /* int array */[1]
};

var b = {
"x": 3,
"y": /* int array */[1],
"z": 3,
"u": function (x, y) {
x: 3,
y: /* int array */[1],
z: 3,
u: function (x, y) {
return x + y | 0;
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/js/test/obj_literal_ppx_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


var a = {
"x": 3,
"y": /* :: */[
x: 3,
y: /* :: */[
1,
/* :: */[
2,
Expand Down
Loading

0 comments on commit db73a51

Please sign in to comment.