From c55a3d163646a06593bde30b9b61a0f4962cb492 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 3 Oct 2023 18:43:57 +0300 Subject: [PATCH] feature: goldstein: parse: type --- packages/goldstein/index.js | 4 ++-- packages/goldstein/index.spec.js | 14 +++++++++++++- packages/goldstein/parser.js | 9 ++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/goldstein/index.js b/packages/goldstein/index.js index ba81a43..957495e 100644 --- a/packages/goldstein/index.js +++ b/packages/goldstein/index.js @@ -1,14 +1,14 @@ import {transform} from 'putout'; import {print} from '@putout/printer'; -import estreeToBabel from 'estree-to-babel'; import tryCatchPlugin from '@putout/plugin-try-catch'; import declarePlugin from '@putout/plugin-declare'; import logicalExpressionsPlugin from '@putout/plugin-logical-expressions'; import {parse} from './parser.js'; export * from './parser.js'; +export {print} from '../printer/index.js'; export const compile = (source, options = {}) => { - const ast = estreeToBabel(parse(source, options)); + const ast = parse(source, options); transform(ast, source, { rules: { diff --git a/packages/goldstein/index.spec.js b/packages/goldstein/index.spec.js index d18721d..761ca49 100644 --- a/packages/goldstein/index.spec.js +++ b/packages/goldstein/index.spec.js @@ -4,6 +4,7 @@ import { compile, keywords, parse, + print, } from './index.js'; test('goldstein: compile', (t) => { @@ -205,7 +206,9 @@ test('goldstein: compile: options', (t) => { test('goldstein: parse: curry', (t) => { const result = parse(montag` sum~(5); - `); + `, { + type: 'estree', + }); const {expression} = result.body[0]; @@ -242,3 +245,12 @@ test('goldstein: parse: import', (t) => { t.equal(result, expected); t.end(); }); + +test('goldstein: print', (t) => { + const source = `const a = try f('hello');`; + const ast = parse(source); + const result = print(ast); + + t.equal(result, `${source}\n`); + t.end(); +}); diff --git a/packages/goldstein/parser.js b/packages/goldstein/parser.js index ddc550b..17c27ea 100644 --- a/packages/goldstein/parser.js +++ b/packages/goldstein/parser.js @@ -1,3 +1,5 @@ +import estreeToBabel from 'estree-to-babel'; + import {extendParser} from '../parser/index.js'; import keywordFn from '../keyword-fn/index.js'; import keywordGuard from '../keyword-guard/index.js'; @@ -33,5 +35,10 @@ export const parse = (source, options = {}, keywords = defaultKeywords) => { ...keywords, })); - return parse(source); + const ast = parse(source); + + if (options.type === 'estree') + return ast; + + return estreeToBabel(ast); };