ocaml - js-of-ocaml 中的 pretty-print

标签 ocaml pretty-print js-of-ocaml

我有以下 OCaml 程序 打开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

以及下面的 JavaScript 程序

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;
};

运行编译后的ocaml代码时,会打印出

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};

有没有一种方法可以进行 pretty-print ,从而更好地显示格式?

最佳答案

您可以使用

禁用紧凑模式
 Compiler.Pretty_print.set_compact pp false;

但是,据我所知,它默认处于打开状态。

还有很多外部工具可以美化 javascript,如果您对结果仍然不满意,可以使用它们。

关于ocaml - js-of-ocaml 中的 pretty-print ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30434644/

相关文章:

node.js - 如何让 Express 输出格式良好的 HTML?

javascript - 在 jQuery/JavaScript 中将日期时间漂亮地打印为一周中的一天加上时间

javascript - 如何将简单的命令行 OCaml 脚本编译成 Javascript

javascript - 如何调用 js_of_ocaml 生成的函数?

node.js - 如何使用 js_of_ocaml 正确编译 OCaml 程序?

functional-programming - OCaml 中的 `string` 是否支持 UTF-8?

haskell - 使用 OCaml 和 Haskell 制作独立的顶层

ocaml - 如果在 OCaml 中为 false,则语句提前结束函数

compiler-errors - 如何在 BuckleScript 中编译带有接口(interface)的文件?

Python 从上到下打印一个二维列表