Skip to content

Commit

Permalink
fix Printer.s_class_field string concat, output final vars properly…
Browse files Browse the repository at this point in the history
…, output question mark instead of @:optional (closes HaxeFoundation#7956)
  • Loading branch information
nadako committed Apr 5, 2020
1 parent 4c3ff6e commit 1a5a785
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/core/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -824,15 +824,35 @@ module Printer = struct
| CTExtend (tl, fl) -> "{> " ^ String.concat " >, " (List.map (s_complex_type_path tabs) tl) ^ ", " ^ String.concat ", " (List.map (s_class_field tabs) fl) ^ " }"
| CTIntersection tl -> String.concat "&" (List.map (fun (t,_) -> s_complex_type tabs t) tl)
and s_class_field tabs f =
match f.cff_doc with
| Some d -> "/**\n\t" ^ tabs ^ (gen_doc_text d) ^ "\n**/\n"
| None -> "" ^
if List.length f.cff_meta > 0 then String.concat ("\n" ^ tabs) (List.map (s_metadata tabs) f.cff_meta) else "" ^
if List.length f.cff_access > 0 then String.concat " " (List.map s_placed_access f.cff_access) else "" ^
match f.cff_kind with
| FVar (t,e) -> "var " ^ (fst f.cff_name) ^ s_opt_type_hint tabs t " : " ^ s_opt_expr tabs e " = "
| FProp ((get,_),(set,_),t,e) -> "var " ^ (fst f.cff_name) ^ "(" ^ get ^ "," ^ set ^ ")" ^ s_opt_type_hint tabs t " : " ^ s_opt_expr tabs e " = "
| FFun func -> "function " ^ (fst f.cff_name) ^ s_func tabs func
let doc = match f.cff_doc with
| Some d -> "/**\n\t" ^ tabs ^ (gen_doc_text d) ^ "\n**/\n"
| None -> ""
in
let s_separated f sep list =
if list <> [] then ((String.concat sep (List.map f list)) ^ sep) else ""
in
let s_meta = s_separated (s_metadata tabs) ("\n" ^ tabs) in
let s_access = s_separated s_placed_access " " in
(match f.cff_kind with
| FVar (t,e) ->
doc ^
let keyword = ref "var " in
let question = ref "" in
let access = List.filter (fun (a,_) -> if a = AFinal then (keyword := "final "; false) else true) f.cff_access in
let meta = List.filter (fun (m,_,_) -> if m = Meta.Optional then (question := "?"; false) else true) f.cff_meta in
s_meta meta ^
s_access access ^
!keyword ^ !question ^ (fst f.cff_name) ^ s_opt_type_hint tabs t " : " ^ s_opt_expr tabs e " = "
| FProp ((get,_),(set,_),t,e) ->
doc ^
s_meta f.cff_meta ^
s_access f.cff_access ^
"var " ^ (fst f.cff_name) ^ "(" ^ get ^ "," ^ set ^ ")" ^ s_opt_type_hint tabs t " : " ^ s_opt_expr tabs e " = "
| FFun func ->
doc ^
s_meta f.cff_meta ^
s_access f.cff_access ^
"function " ^ (fst f.cff_name) ^ s_func tabs func)
and s_metadata tabs (s,e,_) =
"@" ^ Meta.to_string s ^ if List.length e > 0 then "(" ^ s_expr_list tabs e ", " ^ ")" else ""
and s_opt_expr tabs e pre =
Expand Down

0 comments on commit 1a5a785

Please sign in to comment.