|
| 1 | +# node-syntax-tree |
| 2 | + |
| 3 | +`node-syntax-tree` is a node package for executing Syntax Tree in a WASM environment. It functions by compiling the Ruby interpreter and Syntax Tree source files into a WASM package that is loaded and executed at runtime. |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +If you're using the `npm`: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install --save node-syntax-tree |
| 11 | +``` |
| 12 | + |
| 13 | +Or if you're using `yarn`, then add the plugin by: |
| 14 | + |
| 15 | +```bash |
| 16 | +yarn add node-syntax-tree |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +`node-syntax-tree` provides both CJS and ESM modules distributions. To require through CJS, run: |
| 22 | + |
| 23 | +```js |
| 24 | +const { default: createSyntaxTree } = require("node-syntax-tree"); |
| 25 | +``` |
| 26 | + |
| 27 | +To require through ESM, run: |
| 28 | + |
| 29 | +```js |
| 30 | +import createSyntaxTree from "node-syntax-tree"; |
| 31 | +``` |
| 32 | + |
| 33 | +Once you have the import, you can initial the virtual machine and run the associated functions, as in: |
| 34 | + |
| 35 | +```js |
| 36 | +createSyntaxTree().then((syntaxTree) => { |
| 37 | + console.log(syntaxTree.handlers.ruby.parse("foo")); |
| 38 | + // => (program (statements ((vcall (ident "foo"))))) |
| 39 | + |
| 40 | + console.log(syntaxTree.handlers.ruby.format("1+1")); |
| 41 | + // => 1 + 1 |
| 42 | + |
| 43 | + console.log(syntaxTree.handlers.haml.parse("= foo")); |
| 44 | + // => (root children=[(script text=" foo")]) |
| 45 | + |
| 46 | + console.log(syntaxTree.handlers.haml.format("= foo")); |
| 47 | + // => = foo |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +TypeScript types are provided along with this package, and effectively boil down to: |
| 52 | + |
| 53 | +```js |
| 54 | +type SyntaxTreeHandler = { |
| 55 | + format(source: string): string; |
| 56 | + parse(source: string): string; |
| 57 | + read(filepath: string): string; |
| 58 | +}; |
| 59 | + |
| 60 | +type SyntaxTree = { |
| 61 | + handlers: Record<"haml" | "ruby", SyntaxTreeHandler> |
| 62 | +}; |
| 63 | + |
| 64 | +function createSyntaxTree(): Promise<SyntaxTree>; |
| 65 | +``` |
| 66 | + |
| 67 | +## Contributing |
| 68 | + |
| 69 | +Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/node-syntax-tree. |
| 70 | + |
| 71 | +## License |
| 72 | + |
| 73 | +The package is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
| 74 | + |
| 75 | + |
| 76 | +import { RubyVM } from "ruby-head-wasm-wasi/dist/index"; |
| 77 | +declare type SyntaxTreeHandler = { |
| 78 | + format(source: string): string; |
| 79 | + parse(source: string): string; |
| 80 | + read(filepath: string): string; |
| 81 | +}; |
| 82 | +export declare type SyntaxTree = { |
| 83 | + rubyVM: RubyVM; |
| 84 | + handlers: Record<"haml" | "ruby", SyntaxTreeHandler>; |
| 85 | +}; |
| 86 | +export default function createSyntaxTree(): Promise<SyntaxTree>; |
| 87 | +export {}; |
| 88 | + |
| 89 | + |
| 90 | +test("handlers.haml.parse", () => { |
| 91 | + assert.equal( |
| 92 | + syntaxTree.handlers.haml.parse("= foo"), |
| 93 | + `(root children=[(script text=" foo")])\n` |
| 94 | + ); |
| 95 | +}); |
0 commit comments