Source file source_map_io.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 1 "compiler/lib/source_map_io.yojson.ml"
open Source_map
let json t =
let rewrite_path path =
if Filename.is_relative path
then path
else
match Build_path_prefix_map.get_build_path_prefix_map () with
| Some map -> Build_path_prefix_map.rewrite map path
| None -> path
in
`Assoc
[ "version", `Float (float_of_int t.version)
; "file", `String (rewrite_path t.file)
; ( "sourceRoot"
, `String
(match t.sourceroot with
| None -> ""
| Some s -> rewrite_path s) )
; "names", `List (List.map (fun s -> `String s) t.names)
; "sources", `List (List.map (fun s -> `String (rewrite_path s)) t.sources)
; "mappings", `String (string_of_mapping t.mappings)
; ( "sourcesContent"
, `List
(match t.sources_content with
| None -> []
| Some l ->
List.map
(function
| None -> `Null
| Some s -> `String s)
l) )
]
let invalid () = invalid_arg "Source_map.of_json"
let string name rest =
try
match List.assoc name rest with
| `String s -> Some s
| `Null -> None
| _ -> invalid ()
with Not_found -> None
let list_string name rest =
try
match List.assoc name rest with
| `List l ->
Some
(List.map
(function
| `String s -> s
| _ -> invalid ())
l)
| _ -> invalid ()
with Not_found -> None
let list_string_opt name rest =
try
match List.assoc name rest with
| `List l ->
Some
(List.map
(function
| `String s -> Some s
| `Null -> None
| _ -> invalid ())
l)
| _ -> invalid ()
with Not_found -> None
let of_json json =
match json with
| `Assoc (("version", `Float version) :: rest) when int_of_float version = 3 ->
let def v d =
match v with
| None -> d
| Some v -> v
in
let file = string "file" rest in
let sourceroot = string "sourceRoot" rest in
let names = list_string "names" rest in
let sources = list_string "sources" rest in
let sources_content = list_string_opt "sourcesContent" rest in
let mappings = string "mappings" rest in
{ version = int_of_float version
; file = def file ""
; sourceroot
; names = def names []
; sources_content
; sources = def sources []
; mappings = mapping_of_string (def mappings "")
}
| _ -> invalid ()
let of_string s = of_json (Yojson.Basic.from_string s)
let to_string m = Yojson.Basic.to_string (json m)
let to_file m file = Yojson.Basic.to_file file (json m)
let enabled = true