|
19 | 19 |
|
20 | 20 | open! Stdlib
|
21 | 21 |
|
| 22 | +module Source_content = struct |
| 23 | + type t = Sc_as_Stringlit of string |
| 24 | + |
| 25 | + let create s = Sc_as_Stringlit (Yojson.Safe.to_string (`String s)) |
| 26 | + |
| 27 | + let of_stringlit (`Stringlit s) = Sc_as_Stringlit s |
| 28 | + |
| 29 | + let to_json (Sc_as_Stringlit s) = `Stringlit s |
| 30 | +end |
| 31 | + |
22 | 32 | type map =
|
23 | 33 | | Gen of
|
24 | 34 | { gen_line : int
|
@@ -47,7 +57,7 @@ type t =
|
47 | 57 | ; file : string
|
48 | 58 | ; sourceroot : string option
|
49 | 59 | ; sources : string list
|
50 |
| - ; sources_content : string option list option |
| 60 | + ; sources_content : Source_content.t option list option |
51 | 61 | ; names : string list
|
52 | 62 | ; mappings : mapping
|
53 | 63 | }
|
@@ -310,97 +320,118 @@ let json ?replace_mappings t =
|
310 | 320 | | Some map -> Build_path_prefix_map.rewrite map path
|
311 | 321 | | None -> path
|
312 | 322 | in
|
| 323 | + let stringlit s = `Stringlit (Yojson.Safe.to_string (`String s)) in |
313 | 324 | `Assoc
|
314 |
| - [ "version", `Float (float_of_int t.version) |
315 |
| - ; "file", `String (rewrite_path t.file) |
| 325 | + [ "version", `Intlit (string_of_int t.version) |
| 326 | + ; "file", stringlit (rewrite_path t.file) |
316 | 327 | ; ( "sourceRoot"
|
317 |
| - , `String |
| 328 | + , stringlit |
318 | 329 | (match t.sourceroot with
|
319 | 330 | | None -> ""
|
320 | 331 | | Some s -> rewrite_path s) )
|
321 |
| - ; "names", `List (List.map t.names ~f:(fun s -> `String s)) |
322 |
| - ; "sources", `List (List.map t.sources ~f:(fun s -> `String (rewrite_path s))) |
323 |
| - ; ( "mappings" |
324 |
| - , `String (Option.value ~default:(string_of_mapping t.mappings) replace_mappings) ) |
| 332 | + ; "names", `List (List.map t.names ~f:(fun s -> stringlit s)) |
| 333 | + ; "sources", `List (List.map t.sources ~f:(fun s -> stringlit (rewrite_path s))) |
| 334 | + ; "mappings", stringlit (Option.value ~default:(string_of_mapping t.mappings) replace_mappings) |
325 | 335 | ; ( "sourcesContent"
|
326 | 336 | , `List
|
327 | 337 | (match t.sources_content with
|
328 | 338 | | None -> []
|
329 | 339 | | Some l ->
|
330 | 340 | List.map l ~f:(function
|
331 | 341 | | None -> `Null
|
332 |
| - | Some s -> `String s)) ) |
| 342 | + | Some x -> Source_content.to_json x)) ) |
333 | 343 | ]
|
334 | 344 |
|
335 | 345 | let invalid () = invalid_arg "Source_map.of_json"
|
336 | 346 |
|
337 |
| -let string name rest = |
| 347 | +let string_of_stringlit (`Stringlit s) = |
| 348 | + match Yojson.Safe.from_string s with |
| 349 | + | `String s -> s |
| 350 | + | _ -> invalid () |
| 351 | + |
| 352 | +let stringlit name rest : [ `Stringlit of string ] option = |
338 | 353 | try
|
339 | 354 | match List.assoc name rest with
|
340 |
| - | `String s -> Some s |
| 355 | + | `Stringlit _ as s -> Some s |
341 | 356 | | `Null -> None
|
342 | 357 | | _ -> invalid ()
|
343 | 358 | with Not_found -> None
|
344 | 359 |
|
345 |
| -let list_string name rest = |
| 360 | +let list_stringlit name rest = |
346 | 361 | try
|
347 | 362 | match List.assoc name rest with
|
348 | 363 | | `List l ->
|
349 | 364 | Some
|
350 | 365 | (List.map l ~f:(function
|
351 |
| - | `String s -> s |
| 366 | + | `Stringlit _ as s -> s |
352 | 367 | | _ -> invalid ()))
|
353 | 368 | | _ -> invalid ()
|
354 | 369 | with Not_found -> None
|
355 | 370 |
|
356 |
| -let list_string_opt name rest = |
| 371 | +let list_stringlit_opt name rest = |
357 | 372 | try
|
358 | 373 | match List.assoc name rest with
|
359 | 374 | | `List l ->
|
360 | 375 | Some
|
361 | 376 | (List.map l ~f:(function
|
362 |
| - | `String s -> Some s |
| 377 | + | `Stringlit _ as s -> Some s |
363 | 378 | | `Null -> None
|
364 | 379 | | _ -> invalid ()))
|
365 | 380 | | _ -> invalid ()
|
366 | 381 | with Not_found -> None
|
367 | 382 |
|
368 |
| -let of_json ~parse_mappings json = |
369 |
| - let parse ~version rest = |
370 |
| - let def v d = |
371 |
| - match v with |
372 |
| - | None -> d |
373 |
| - | Some v -> v |
| 383 | +let of_json ~parse_mappings (json : Yojson.Raw.t) = |
| 384 | + match json with |
| 385 | + | `Assoc (("version", `Intlit version) :: rest) when int_of_string version = 3 -> |
| 386 | + let string name json = Option.map ~f:string_of_stringlit (stringlit name json) in |
| 387 | + let file = |
| 388 | + match string "file" rest with |
| 389 | + | None -> "" |
| 390 | + | Some s -> s |
374 | 391 | in
|
375 |
| - let file = string "file" rest in |
376 | 392 | let sourceroot = string "sourceRoot" rest in
|
377 |
| - let names = list_string "names" rest in |
378 |
| - let sources = list_string "sources" rest in |
379 |
| - let sources_content = list_string_opt "sourcesContent" rest in |
380 |
| - let mappings = string "mappings" rest in |
381 |
| - ( { version |
382 |
| - ; file = def file "" |
| 393 | + let names = |
| 394 | + match list_stringlit "names" rest with |
| 395 | + | None -> [] |
| 396 | + | Some l -> List.map ~f:string_of_stringlit l |
| 397 | + in |
| 398 | + let sources = |
| 399 | + match list_stringlit "sources" rest with |
| 400 | + | None -> [] |
| 401 | + | Some l -> List.map ~f:string_of_stringlit l |
| 402 | + in |
| 403 | + let sources_content = |
| 404 | + match list_stringlit_opt "sourcesContent" rest with |
| 405 | + | None -> None |
| 406 | + | Some l -> |
| 407 | + Some |
| 408 | + (List.map l ~f:(function |
| 409 | + | None -> None |
| 410 | + | Some s -> Some (Source_content.of_stringlit s))) |
| 411 | + in |
| 412 | + let mappings = |
| 413 | + match string "mappings" rest with |
| 414 | + | None -> mapping_of_string "" |
| 415 | + | Some s -> mapping_of_string s |
| 416 | + in |
| 417 | + ( { version = int_of_float (float_of_string version) |
| 418 | + ; file |
383 | 419 | ; sourceroot
|
384 |
| - ; names = def names [] |
| 420 | + ; names |
385 | 421 | ; sources_content
|
386 |
| - ; sources = def sources [] |
387 |
| - ; mappings = mapping_of_string (def mappings "") |
| 422 | + ; sources |
| 423 | + ; mappings |
388 | 424 | }
|
389 |
| - , if parse_mappings then None else mappings ) |
390 |
| - in |
391 |
| - match json with |
392 |
| - | `Assoc (("version", `Float version) :: rest) when int_of_float version = 3 -> |
393 |
| - parse ~version:3 rest |
394 |
| - | `Assoc (("version", `Int 3) :: rest) -> parse ~version:3 rest |
| 425 | + , if parse_mappings then None else Some mappings ) |
395 | 426 | | _ -> invalid ()
|
396 | 427 |
|
397 |
| -let of_string s = of_json ~parse_mappings:true (Yojson.Basic.from_string s) |> fst |
| 428 | +let of_string s = of_json ~parse_mappings:true (Yojson.Raw.from_string s) |> fst |
398 | 429 |
|
399 |
| -let to_string m = Yojson.Basic.to_string (json m) |
| 430 | +let to_string m = Yojson.Raw.to_string (json m) |
400 | 431 |
|
401 | 432 | let to_file ?mappings m ~file =
|
402 | 433 | let replace_mappings = mappings in
|
403 |
| - Yojson.Basic.to_file file (json ?replace_mappings m) |
| 434 | + Yojson.Raw.to_file file (json ?replace_mappings m) |
404 | 435 |
|
405 | 436 | let of_file_no_mappings filename =
|
406 |
| - of_json ~parse_mappings:false (Yojson.Basic.from_file filename) |
| 437 | + of_json ~parse_mappings:false (Yojson.Raw.from_file filename) |
0 commit comments