Skip to content

Conversation

@pyramation
Copy link
Contributor

feat: add rego-deparser package for OPA Rego AST to text conversion

Summary

Adds a new rego-deparser package to the dev-utils monorepo, positioned alongside cel-proto-parser. This package provides TypeScript types for OPA's Rego Abstract Syntax Tree and a deparser that converts AST back to valid Rego source code.

The package supports:

  • All major Rego constructs: packages, imports, rules (complete, partial, default), else clauses
  • Expressions with infix operators (comparison, arithmetic, assignment, unification)
  • Data structures: arrays, sets, objects, references
  • Comprehensions: array, set, and object comprehensions
  • With modifiers and negation

This is part of the broader effort to add OPA capabilities to the stack for a unified policy engine.

Review & Testing Checklist for Human

  • Verify AST types match actual OPA output: The types in src/types.ts were created from OPA documentation research. Test with real OPA-generated AST JSON (via opa parse --format json) to confirm the types are accurate
  • Check infix operator mappings: The InfixOperators map in src/types.ts assumes specific operator names from OPA (e.g., assign:=, eq=). Verify these match OPA's actual AST representation
  • Review for missing Rego constructs: Some features may not be covered (e.g., some keyword declarations, raw strings, certain every expression patterns)
  • Consider adding to CI test matrix: The package has 47 passing tests but is not yet in .github/workflows/ci.yml

Suggested test plan:

  1. Install OPA CLI locally
  2. Create a sample Rego policy with various constructs
  3. Run opa parse --format json policy.rego to get AST
  4. Use the deparser to convert back to Rego
  5. Compare output with original (or run through opa fmt for canonical comparison)

Notes

  • Lint command fails due to existing ESLint 9 config format issue in the repo (not introduced by this PR)
  • Build and all 47 tests pass locally

Link to Devin run: https://app.devin.ai/sessions/c053d7e3447a47959bc24118acd8b30c
Requested by: Dan Lynch (@pyramation)

- 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
@devin-ai-integration
Copy link

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@pyramation pyramation merged commit a9b8225 into main Jan 6, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants