Skip to content

Commit

Permalink
feature: goldstein: printer: add ability to print goldstein: try
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 3, 2023
1 parent c634f55 commit 4079f96
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/keyword-try/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,20 @@ export default function keywordTry(Parser) {
expression.callee,
...expression.arguments,
],
goldstein: createGoldsteinNode({
await: false,
callee: expression.callee,
arguments: expression.arguments,
}),
};
else if (isAwaitExpression(expression))
node.expression = {
type: 'AwaitExpression',
goldstein: createGoldsteinNode({
await: true,
callee: expression.argument.callee,
arguments: expression.argument.arguments,
}),
argument: {
type: 'CallExpression',
callee: {
Expand Down Expand Up @@ -100,3 +110,15 @@ export default function keywordTry(Parser) {
};
}

function createGoldsteinNode(args) {
return {
type: 'TryStatement',
expression: true,
await: args.await,
argument: {
type: 'CallExpression',
callee: args.callee,
arguments: args.arguments,
},
};
}
18 changes: 18 additions & 0 deletions packages/printer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {print as printJS} from '@putout/printer';

import {AwaitExpression} from './visitors/await-expression.js';
import {CallExpression} from './visitors/call-expression.js';
import {TryStatement} from './visitors/try-statement.js';
import {fixEmpty} from '../goldstein/index.js';

export const print = (ast) => {
const code = printJS(ast, {
visitors: {
CallExpression,
TryStatement,
AwaitExpression,
},
});

return fixEmpty(code);
};
31 changes: 31 additions & 0 deletions packages/printer/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {test} from 'supertape';
import montag from 'montag';
import estreeToBabel from 'estree-to-babel';

import {print} from './index.js';
import {parse} from '../goldstein/index.js';

test('goldstein: printer: try: await', (t) => {
const source = `const a = try await f('hello')`;
const ast = estreeToBabel(parse(source));
const result = print(ast);
const expected = montag`
const a = try await f('hello');\n
`;

t.equal(result, expected);
t.end();
});

test('goldstein: printer: try', (t) => {
const source = `const a = try f('hello')`;
const ast = estreeToBabel(parse(source));
const result = print(ast);
const expected = montag`
const a = try f('hello');\n
`;

t.equal(result, expected);
t.end();
});

10 changes: 10 additions & 0 deletions packages/printer/visitors/await-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {visitors as v} from '@putout/printer';

export const AwaitExpression = (path, printer, semantics) => {
const {print} = printer;

if (!path.node.goldstein)
return v.AwaitExpression(path, printer, semantics);

print('__goldstein');
};
10 changes: 10 additions & 0 deletions packages/printer/visitors/call-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {visitors as v} from '@putout/printer';

export const CallExpression = (path, printer, semantics) => {
const {print} = printer;

if (!path.node.goldstein)
return v.CallExpression(path, printer, semantics);

print('__goldstein');
};
12 changes: 12 additions & 0 deletions packages/printer/visitors/try-statement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {visitors as v} from '@putout/printer';
export const TryStatement = (path, printer, semantics) => {
const {maybe, print} = printer;
const {node} = path;

if (!node.expression)
return v.TryStatement(path, printer, semantics);

print('try ');
maybe.print(node.await, 'await ');
print('__argument');
};

0 comments on commit 4079f96

Please sign in to comment.