Skip to content

Commit d610795

Browse files
authored
Merge pull request #22 from mkantor/unparsing-plz-utilities
Share common utilities across plz unparsers
2 parents 763cd3a + 242df96 commit d610795

File tree

12 files changed

+423
-364
lines changed

12 files changed

+423
-364
lines changed

package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"devDependencies": {
1818
"@types/node": "^22.5.5",
19+
"strip-ansi": "^7.1.0",
1920
"typescript": "^5.6.2"
2021
},
2122
"dependencies": {

src/language/cli/output.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import { parseArgs } from 'util'
33
import { type SyntaxTree } from '../parsing/syntax-tree.js'
4-
import { unparse, type Notation } from '../unparsing.js'
5-
import { prettyJson } from '../unparsing/pretty-json.js'
6-
import { prettyPlz } from '../unparsing/pretty-plz.js'
4+
import { prettyJson, prettyPlz, unparse, type Notation } from '../unparsing.js'
75

86
export const handleOutput = async (
97
process: NodeJS.Process,
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import either from '@matt.kantor/either'
2+
import stripAnsi from 'strip-ansi'
3+
import { testCases } from '../../test-utilities.test.js'
4+
import { type Atom, type Molecule } from '../parsing.js'
5+
import {
6+
inlinePlz,
7+
prettyJson,
8+
prettyPlz,
9+
type Notation,
10+
} from '../unparsing.js'
11+
12+
const unparser = (notation: Notation) => (value: Atom | Molecule) =>
13+
either.map(
14+
typeof value === 'string'
15+
? notation.atom(value)
16+
: notation.molecule(value, notation),
17+
stripAnsi, // terminal styling is not currently tested
18+
)
19+
20+
testCases(
21+
unparser(inlinePlz),
22+
input => `unparsing \`${JSON.stringify(input)}\``,
23+
)('inline plz', [
24+
[{}, either.makeRight('{}')],
25+
['a', either.makeRight('a')],
26+
['Hello, world!', either.makeRight('"Hello, world!"')],
27+
[{ 0: 'a' }, either.makeRight('{ a }')],
28+
[{ 1: 'a' }, either.makeRight('{ 1: a }')],
29+
[
30+
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
31+
either.makeRight('{ a, b, 3: c, somethingElse: d }'),
32+
],
33+
[{ a: { b: { c: 'd' } } }, either.makeRight('{ a: { b: { c: d } } }')],
34+
[
35+
{
36+
identity: {
37+
0: '@function',
38+
parameter: 'a',
39+
body: { 0: '@lookup', 1: 'a' },
40+
},
41+
test: {
42+
0: '@apply',
43+
function: { 0: '@lookup', 1: 'identity' },
44+
argument: 'it works!',
45+
},
46+
},
47+
either.makeRight('{ identity: a => :a, test: :identity("it works!") }'),
48+
],
49+
])
50+
51+
testCases(
52+
unparser(prettyPlz),
53+
input => `unparsing \`${JSON.stringify(input)}\``,
54+
)('pretty plz', [
55+
[{}, either.makeRight('{}')],
56+
['a', either.makeRight('a')],
57+
['Hello, world!', either.makeRight('"Hello, world!"')],
58+
[{ 0: 'a' }, either.makeRight('{\n a\n}')],
59+
[{ 1: 'a' }, either.makeRight('{\n 1: a\n}')],
60+
[
61+
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
62+
either.makeRight('{\n a\n b\n 3: c\n somethingElse: d\n}'),
63+
],
64+
[
65+
{ a: { b: { c: 'd' } } },
66+
either.makeRight('{\n a: {\n b: {\n c: d\n }\n }\n}'),
67+
],
68+
[
69+
{
70+
identity: {
71+
0: '@function',
72+
parameter: 'a',
73+
body: { 0: '@lookup', 1: 'a' },
74+
},
75+
test: {
76+
0: '@apply',
77+
function: { 0: '@lookup', 1: 'identity' },
78+
argument: 'it works!',
79+
},
80+
},
81+
either.makeRight(
82+
'{\n identity: a => :a\n test: :identity("it works!")\n}',
83+
),
84+
],
85+
])
86+
87+
testCases(
88+
unparser(prettyJson),
89+
input => `unparsing \`${JSON.stringify(input)}\``,
90+
)('pretty JSON', [
91+
[{}, either.makeRight('{}')],
92+
['a', either.makeRight('"a"')],
93+
['Hello, world!', either.makeRight('"Hello, world!"')],
94+
[{ 0: 'a' }, either.makeRight('{\n "0": "a"\n}')],
95+
[{ 1: 'a' }, either.makeRight('{\n "1": "a"\n}')],
96+
[
97+
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
98+
either.makeRight(
99+
'{\n "0": "a",\n "1": "b",\n "3": "c",\n "somethingElse": "d"\n}',
100+
),
101+
],
102+
[
103+
{ a: { b: { c: 'd' } } },
104+
either.makeRight('{\n "a": {\n "b": {\n "c": "d"\n }\n }\n}'),
105+
],
106+
[
107+
{
108+
identity: {
109+
0: '@function',
110+
parameter: 'a',
111+
body: { 0: '@lookup', 1: 'a' },
112+
},
113+
test: {
114+
0: '@apply',
115+
function: { 0: '@lookup', 1: 'identity' },
116+
argument: 'it works!',
117+
},
118+
},
119+
either.makeRight(
120+
'{\n "identity": {\n "0": "@function",\n "parameter": "a",\n "body": {\n "0": "@lookup",\n "1": "a"\n }\n },\n "test": {\n "0": "@apply",\n "function": {\n "0": "@lookup",\n "1": "identity"\n },\n "argument": "it works!"\n }\n}',
121+
),
122+
],
123+
])

src/language/runtime/keywords.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
lookupPropertyOfObjectNode,
1919
makeUnelaboratedObjectNode,
2020
} from '../semantics/object-node.js'
21-
import { prettyJson } from '../unparsing/pretty-json.js'
21+
import { prettyJson } from '../unparsing.js'
2222

2323
const unserializableFunction = () =>
2424
either.makeLeft({

src/language/semantics/key-path.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import either from '@matt.kantor/either'
22
import type { Atom, Molecule } from '../parsing.js'
3-
import { unparse } from '../unparsing.js'
4-
import { inlinePlz } from '../unparsing/inline-plz.js'
3+
import { inlinePlz, unparse } from '../unparsing.js'
54

65
export type KeyPath = readonly Atom[]
76

src/language/semantics/semantic-graph.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import type {
77
} from '../errors.js'
88
import type { Atom, Molecule } from '../parsing.js'
99
import type { Canonicalized } from '../parsing/syntax-tree.js'
10-
import { unparse } from '../unparsing.js'
11-
import { inlinePlz } from '../unparsing/inline-plz.js'
10+
import { inlinePlz, unparse } from '../unparsing.js'
1211
import { serializeFunctionNode, type FunctionNode } from './function-node.js'
1312
import { stringifyKeyPathForEndUser, type KeyPath } from './key-path.js'
1413
import {

src/language/unparsing.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import type { UnserializableValueError } from './errors.js'
33
import type { Atom, Molecule } from './parsing.js'
44
import type { Notation } from './unparsing/unparsing-utilities.js'
55

6-
export { type Notation } from './unparsing/unparsing-utilities.js'
6+
export { inlinePlz } from './unparsing/inline-plz.js'
7+
export { prettyJson } from './unparsing/pretty-json.js'
8+
export { prettyPlz } from './unparsing/pretty-plz.js'
9+
export type { Notation } from './unparsing/unparsing-utilities.js'
710

811
export const unparse = (
912
value: Atom | Molecule,

0 commit comments

Comments
 (0)