Pretty-print in js-of-ocaml

226 Views Asked by At

I have the following OCaml program open Js

let lex s = Compiler.Parse_js.lexer_from_file s
let parse s = lex s |> Compiler.Parse_js.parse

let buffer_pp program = 
  let buf = Buffer.create 10 in
  let pp = Compiler.Pretty_print.to_buffer buf in
  Compiler.Js_output.program pp program;
  Buffer.contents buf |> print_endline

let () = 
  parse "test.js" |> buffer_pp

and the following JavaScript program

function person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
}
person.prototype.name = function() {
  return this.firstName + " " + this.lastName;
};

When running the compiled ocaml code, it prints out

function person(first,last,age,eyecolor)
 {this.firstName=first;this.lastName=last;this.age=age;this.eyeColor=eyecolor}
person.prototype.name=function(){return this.firstName+" "+this.lastName};

Is there a way to do pretty-printing which displays format better?

1

There are 1 best solutions below

0
On BEST ANSWER

You can disable compact mode with

 Compiler.Pretty_print.set_compact pp false;

But, AFAIK, it is on by default.

There're also lots of external tools, that prettifies javascript, that you can use, if you're still not satisfied with the result.