Skip to content

Commit

Permalink
feature: goldstein: parse: type
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 3, 2023
1 parent db6bcbe commit c55a3d1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/goldstein/index.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
14 changes: 13 additions & 1 deletion packages/goldstein/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
compile,
keywords,
parse,
print,
} from './index.js';

test('goldstein: compile', (t) => {
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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();
});
9 changes: 8 additions & 1 deletion packages/goldstein/parser.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
};

0 comments on commit c55a3d1

Please sign in to comment.