npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jchavarri/bs-refmt

v0.0.5

Published

[`refmt`](https://github.com/facebook/reason), the Reason parser / printer, packaged as a BuckleScript library.

Downloads

9

Readme

bs-refmt

refmt, the Reason parser / printer, packaged as a BuckleScript library.

Install

In your BuckleScript project:

yarn add @jchavarri/bs-refmt

Then include the library in your bsconfig.json:

"bs-dependencies": ["@jchavarri/bs-refmt"],

Example usage

let reasonCode = "let t = Some(2)->Belt.Option.map(a => a * 2)";
let astAndComments =
  Lexing.from_string(reasonCode)
  |> Refmt_api.RE.implementation_with_comments;

Js.log("AST for: " ++ reasonCode);
Js.log(astAndComments);

let firstItem = structure->Belt.List.head;

let () =
  switch (firstItem) {
  | Some({pstr_desc, pstr_loc:_}) =>
    switch (pstr_desc) {
    | Pstr_eval(_) => Js.log("Pstr_eval")
    | Pstr_value(_) => Js.log("Pstr_value")
    | Pstr_primitive(_) => Js.log("Pstr_primitive")
    | Pstr_type(_) => Js.log("Pstr_type")
    | Pstr_typext(_) => Js.log("Pstr_typext")
    | Pstr_exception(_) => Js.log("Pstr_exception")
    | Pstr_module(_) => Js.log("Pstr_module")
    | Pstr_recmodule(_) => Js.log("Pstr_recmodule")
    | Pstr_modtype(_) => Js.log("Pstr_modtype")
    | Pstr_open(_) => Js.log("Pstr_open")
    | Pstr_class(_) => Js.log("Pstr_class")
    | Pstr_class_type(_) => Js.log("Pstr_class_type")
    | Pstr_include(_) => Js.log("Pstr_include")
    | Pstr_attribute(_) => Js.log("Pstr_attribute")
    | Pstr_extension(_) => Js.log("Pstr_extension")
    }
  | None => ()
  };

/* Convert Reason back to OCaml syntax. */
Refmt_api.ML.print_implementation_with_comments(
  Format.str_formatter,
  astAndComments,
);
let ocamlCode = Format.flush_str_formatter();
Js.log(ocamlCode);

Check another example file in the example folder.

FAQ

  • What can this library be used for?

It could help to experiment or build applications that require to enter some text, and parse it into a "fully fleshed" OCaml AST (abstract syntax tree) to be processed, printed, displayed, manipulated visually or otherwise.

Printing code text from an AST also works. As well as "in-browser" ppxs.

  • How is the file Reason_toolchain_packed.ml generated?

It's taken from Reason repo. Steps to generate it:

cd .. # one folder up from root folder
git clone https://github.com/jchavarri/reason/
git checkout bs-refmt
cd bspacks
./bspacks.js
cp output/4061/refmt.ml ../../bs-refmt/src/Reason_toolchain_packed.ml
  • I don't see module X

For now, only a couple of modules are exposed (see Refmt_api.ml) for simplicity. To access all modules reach for the bspacked module Reason_toolchain_packed.

Previous work

  • refmt.js / npm reason package?

refmt.js (see docs) is a packaging of refmt compiled with js_of_ocaml into JavaScript.

glennsl/bs-refmt are the BuckleScript bindings to the JavaScript API exposed from refmt.js. It is a BuckleScript library like this one, but the internals of the AST are opaque, one can only get types for the data that is explicitly typed in the bindings.

The main difference between previous approaches and this iteration is that now there is full access to the types of the resulting parsed tree.

Thanks

This library is a very thin wrapper, building on top of the effort done by the projects mentioned above, as well as the shoulders of giants below: