diff --git a/README.md b/README.md index 040870e..41e2b76 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,30 @@ parse(` `, options); ``` +### `print(ast)` + +You can make any modifications to **Goldstein AST** and then `print` back to **Goldstein**: + +``` +import {parse, print} from 'goldstein'; + +const ast = parse(`const t = try f('hello')`); +const source = print(ast); +``` + +### `convert(source)` + +You can even convert **JavaScript** to **Goldstein** with: + +``` +import {convert} from 'goldstein'; + +const ast = convert(`const t = tryCatch(f, 'hello');`; + +// returns +`const t = try f('hello')`) +``` + ## Keywords **Goldstein** is absolutely compatible with JavaScript, and it has extensions. diff --git a/packages/convert/index.spec.js b/packages/convert/index.spec.js index f45a821..cc6f9e1 100644 --- a/packages/convert/index.spec.js +++ b/packages/convert/index.spec.js @@ -2,7 +2,7 @@ import {test} from 'supertape'; import montag from 'montag'; import {convert} from './index.js'; -test('goldstein: convert', (t) => { +test('goldstein: convert: tryCatch', (t) => { const source = `const a = await tryToCatch(f, 'hello')`; const result = convert(source); diff --git a/packages/goldstein/index.js b/packages/goldstein/index.js index 957495e..3d59edb 100644 --- a/packages/goldstein/index.js +++ b/packages/goldstein/index.js @@ -7,6 +7,7 @@ import {parse} from './parser.js'; export * from './parser.js'; export {print} from '../printer/index.js'; +export {convert} from '../convert/index.js'; export const compile = (source, options = {}) => { const ast = parse(source, options); diff --git a/packages/goldstein/index.spec.js b/packages/goldstein/index.spec.js index 761ca49..ad07522 100644 --- a/packages/goldstein/index.spec.js +++ b/packages/goldstein/index.spec.js @@ -5,6 +5,7 @@ import { keywords, parse, print, + convert, } from './index.js'; test('goldstein: compile', (t) => { @@ -254,3 +255,13 @@ test('goldstein: print', (t) => { t.equal(result, `${source}\n`); t.end(); }); + +test('goldstein: convert', (t) => { + const source = `const a = tryCatch(f, 'hello');`; + const result = convert(source); + + const expected = `const a = try f('hello');\n`; + + t.equal(result, expected); + t.end(); +});