-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: goldstein: printer: add ability to print goldstein: try
- Loading branch information
1 parent
c634f55
commit 4079f96
Showing
6 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |