Skip to content

Commit 168e3d2

Browse files
committed
feat: add rego-deparser package for OPA Rego AST to text conversion
- Add TypeScript types for Rego AST nodes (Module, Rule, Expr, Term, etc.) - Implement deparser that converts Rego AST to valid Rego source code - Support all major Rego constructs: packages, imports, rules, expressions - Handle infix operators, comprehensions, references, and literals - Add comprehensive test suite with 47 tests - Follow cel-proto-parser patterns for consistency
1 parent 362a5e9 commit 168e3d2

File tree

10 files changed

+4393
-5221
lines changed

10 files changed

+4393
-5221
lines changed

packages/rego-deparser/README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# rego-deparser
2+
3+
Rego (OPA policy language) AST types and deparser for TypeScript.
4+
5+
This package provides TypeScript types for OPA's Rego Abstract Syntax Tree (AST) and a deparser that converts AST back to valid Rego source code.
6+
7+
## Installation
8+
9+
```bash
10+
npm install rego-deparser
11+
```
12+
13+
## Usage
14+
15+
```typescript
16+
import { deparse, Module, TermType } from 'rego-deparser';
17+
18+
// Create a Rego module AST
19+
const module: Module = {
20+
package: {
21+
path: [{ type: TermType.VAR, value: 'example' }]
22+
},
23+
rules: [
24+
{
25+
default: true,
26+
head: {
27+
name: 'allow',
28+
value: { type: TermType.BOOLEAN, value: false }
29+
}
30+
},
31+
{
32+
head: {
33+
name: 'allow',
34+
value: { type: TermType.BOOLEAN, value: true }
35+
},
36+
body: [
37+
{
38+
terms: [
39+
{ type: TermType.VAR, value: 'equal' },
40+
{
41+
type: TermType.REF,
42+
value: [
43+
{ type: TermType.VAR, value: 'input' },
44+
{ type: TermType.STRING, value: 'user' }
45+
]
46+
},
47+
{ type: TermType.STRING, value: 'admin' }
48+
]
49+
}
50+
]
51+
}
52+
]
53+
};
54+
55+
// Convert AST to Rego source code
56+
const rego = deparse(module);
57+
console.log(rego);
58+
// Output:
59+
// package example
60+
//
61+
// default allow = false
62+
// allow = true if {
63+
// input.user == "admin"
64+
// }
65+
```
66+
67+
## API
68+
69+
### `deparse(module: Module, options?: DeparserOptions): string`
70+
71+
Converts a Rego Module AST to Rego source code.
72+
73+
#### Options
74+
75+
- `indent`: String to use for indentation (default: `'\t'`)
76+
- `newline`: String to use for newlines (default: `'\n'`)
77+
- `spaces`: Whether to add spaces around operators (default: `true`)
78+
79+
### Types
80+
81+
The package exports TypeScript types for all Rego AST node types:
82+
83+
- `Module` - A Rego module (policy file)
84+
- `Package` - Package declaration
85+
- `Import` - Import statement
86+
- `Rule` - A Rego rule
87+
- `Head` - Rule head
88+
- `Body` - Rule body (array of expressions)
89+
- `Expr` - An expression in a rule body
90+
- `Term` - A term in an expression
91+
- `With` - With modifier
92+
- `Else` - Else clause for rules
93+
94+
### Term Types
95+
96+
The `TermType` constant provides all supported term types:
97+
98+
- `NULL` - null value
99+
- `BOOLEAN` - boolean value
100+
- `NUMBER` - numeric value
101+
- `STRING` - string value
102+
- `VAR` - variable
103+
- `REF` - reference (e.g., `data.users`)
104+
- `CALL` - function call
105+
- `ARRAY` - array literal
106+
- `SET` - set literal
107+
- `OBJECT` - object literal
108+
- `ARRAY_COMPREHENSION` - array comprehension
109+
- `SET_COMPREHENSION` - set comprehension
110+
- `OBJECT_COMPREHENSION` - object comprehension
111+
112+
### Operators
113+
114+
The package supports all Rego infix operators:
115+
116+
- Comparison: `==`, `!=`, `<`, `<=`, `>`, `>=`
117+
- Arithmetic: `+`, `-`, `*`, `/`, `%`
118+
- Logical: `&`, `|`
119+
- Assignment: `:=` (local assignment)
120+
- Unification: `=` (unification)
121+
122+
## Obtaining Rego AST
123+
124+
The Rego AST can be obtained from OPA in several ways:
125+
126+
1. **OPA REST API**: `POST /v1/compile`
127+
2. **OPA builtin**: `rego.parse_module(filename, rego_text)`
128+
3. **OPA CLI**: `opa parse --format json policy.rego`
129+
130+
## License
131+
132+
MIT

0 commit comments

Comments
 (0)