forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_dump_property.ml
89 lines (83 loc) · 2.87 KB
/
js_dump_property.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
module P = Ext_pp
module L = Js_dump_lit
(**
https://stackoverflow.com/questions/9367572/rules-for-unquoted-javascript-object-literal-keys
https://mathiasbynens.be/notes/javascript-properties
https://mathiasbynens.be/notes/javascript-identifiers
Let's not do smart things
{[
{ 003 : 1}
]}
becomes
{[
{ 3 : 1}
]}
*)
(** used in printing keys
{[
{"x" : x};;
{x : x }
{"50x" : 2 } GPR #1943
]}
Note we can not treat it in the same way when printing
[x.id] vs [{id : xx}]
for example, id can be number in object literal
*)
let obj_property_no_need_quot s =
let len = String.length s in
if len > 0 then
match String.unsafe_get s 0 with
| '$' | '_' | 'a' .. 'z' | 'A' .. 'Z' ->
Ext_string.for_all_from s 1 (function
| 'a' .. 'z' | 'A' .. 'Z' | '$' | '_' | '0' .. '9' -> true
| _ -> false)
| _ -> false
else false
(** used in property access
{[
f.x ;;
f["x"];;
]}
*)
let property_access f s =
if obj_property_no_need_quot s then (
P.string f L.dot;
P.string f s)
else
P.bracket_group f 1 (fun _ ->
(* avoid cases like
"0123", "123_456"
*)
match string_of_int (int_of_string s) with
| s0 when s0 = s -> P.string f s
| _ -> Js_dump_string.pp_string f s
| exception _ -> Js_dump_string.pp_string f s)
let property_key (s : J.property_name) : string =
match s with
| Lit s ->
if obj_property_no_need_quot s then s
else Js_dump_string.escape_to_string s
| Symbol_name -> {|[Symbol.for("name")]|}