https://github.com/mbj/unparser
Turn Ruby AST into semantically equivalent Ruby source
https://github.com/mbj/unparser
Keywords
parser ruby ruby-syntax unparser
Keywords from Contributors
rubocop activerecord static-code-analysis code-formatter activejob mvc rubygems rspec rack feature-flag
Last synced: about 24 hours ago
JSON representation
Repository metadata
Turn Ruby AST into semantically equivalent Ruby source
- Host: GitHub
- URL: https://github.com/mbj/unparser
- Owner: mbj
- License: mit
- Created: 2013-04-08T09:54:20.000Z (almost 13 years ago)
- Default Branch: main
- Last Pushed: 2026-01-06T01:04:24.000Z (about 2 months ago)
- Last Synced: 2026-02-20T06:56:44.858Z (11 days ago)
- Topics: parser, ruby, ruby-syntax, unparser
- Language: Ruby
- Homepage:
- Size: 1.28 MB
- Stars: 326
- Watchers: 9
- Forks: 50
- Open Issues: 6
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE
README.md
unparser
Generate equivalent source for ASTs from parser.
The following constraints apply:
- No support for macruby extensions
- Only support for the modern AST format
- Only support for Ruby >= 3.2
Notable Users:
- mutant - Code review engine via mutation testing.
- ruby-next - Ruby Syntax Backports.
- Many other reverse-dependencies.
(if you want your tool to be mentioned here please PR the addition with a TLDR of your use case).
Public API:
While unparser is in the 0.x versions its public API can change any moment.
I recommend to use ~> 0.x.y style version constraints that should give the best mileage.
Usage
require 'parser/current'
require 'unparser'
ast = Unparser.parse('your(ruby(code))')
Unparser.unparse(ast) # => 'your(ruby(code))'
To preserve the comments from the source:
require 'parser/current'
require 'unparser'
ast, comments = Unparser.parser.parse_with_comments(Unparser.buffer('your(ruby(code)) # with comments'))
Unparser.unparse(ast, comments: comments) # => 'your(ruby(code)) # with comments'
Passing in manually constructed AST:
require 'parser/current'
require 'unparser'
module YourHelper
def s(type, *children)
Parser::AST::Node.new(type, children)
end
end
include YourHelper
node = s(:def,
:foo,
s(:args,
s(:arg, :x)
),
s(:send,
s(:lvar, :x),
:+,
s(:int, 3)
)
)
Unparser.unparse(node) # => "def foo(x)\n x + 3\nend"
Note: DO NOT attempt to pass in nodes generated via AST::Sexp#s, these ones return
API incompatible AST::Node instances, unparser needs Parser::AST::Node instances.
Equivalent vs identical:
require 'unparser'
node = Unparser.parse(<<~'RUBY')
%w[foo bar]
RUBY
generated = Unparser.unparse(node) # ["foo", "bar"], NOT %w[foo bar] !
code == generated # false, not identical code
Unparser.parse(generated) == node # true, but identical AST
Summary: unparser does not reproduce your source! It produces equivalent source.
Ruby Versions:
Unparsers primay reason for existance is mutant and its
supported Ruby-Versions.
Basically: All non EOL MRI releases.
If you need to generate Ruby Syntax outside of this band feel free to contact me (email in gemspec).
Testing:
Unparser currently successfully round trips almost all ruby code around. Using Ruby >= 2.6.
If there is a non round trippable example that is NOT subjected to known Limitations.
please report a bug.
On CI unparser is currently tested against rubyspec with minor excludes.
Limitations:
Source parsed with magic encoding headers other than UTF-8 and that have literal strings.
where parts can be represented in UTF-8 will fail to get reproduced.
A fix is possible as with latest updates the parser gem carries the information.
Example:
Original-Source:
# -*- encoding: binary -*-
"\x98\x76\xAB\xCD\x45\x32\xEF\x01\x01\x23\x45\x67\x89\xAB\xCD\xEF"
Original-AST:
(str "\x98v\xAB\xCDE2\xEF\x01\x01#Eg\x89\xAB\xCD\xEF")
Generated-Source:
"\x98v\xAB\xCDE2\xEF\x01\x01#Eg\x89\xAB\xCD\xEF"
Generated-AST:
(str "\x98v\xAB\xCDE2\xEF\u0001\u0001#Eg\x89\xAB\xCD\xEF")
Diff:
@@ -1,2 +1,2 @@
-(str "\x98v\xAB\xCDE2\xEF\x01\x01#Eg\x89\xAB\xCD\xEF")
+(str "\x98v\xAB\xCDE2\xEF\u0001\u0001#Eg\x89\xAB\xCD\xEF")
Installation
Install the gem unparser via your prefered method.
People
Various people contributed to this repository. See Contributors.
Included Libraries
For dependency reduction reasons unparser ships vendored (and reduced) versions of:
- abstract_type -> Unparser::AbstractType
- adamantium -> Unparser::Adamantium
- anima -> Unparser::Anima
- concord -> Unparser::Concord
- memoizable -> Unparser::Adamantium
- mprelude -> Unparser::Either
Contributing
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a
future version unintentionally. - Commit, do not mess with version
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) - Send me a pull request. Bonus points for topic branches.
Known Users
- RailsRocket - A no-code app builder that creates Rails apps
License
See LICENSE file.
Owner metadata
- Name: Markus Schirp
- Login: mbj
- Email: mbj@schirp-dso.com
- Kind: user
- Description: Master of Disaster.
- Website:
- Location: /dev/urandom
- Twitter: _m_b_j_
- Company: /proc/self
- Icon url: https://avatars.githubusercontent.com/u/117966?u=4f362766f68e589ac0c3c8f799d23e90169668b8&v=4
- Repositories: 163
- Last ynced at: 2025-10-29T04:59:34.125Z
- Profile URL: https://github.com/mbj
GitHub Events
Total
- Delete event: 14
- Pull request event: 64
- Fork event: 4
- Issues event: 11
- Watch event: 8
- Issue comment event: 73
- Push event: 62
- Pull request review comment event: 14
- Pull request review event: 31
- Create event: 20
Last Year
- Delete event: 9
- Pull request event: 55
- Fork event: 3
- Issues event: 9
- Watch event: 5
- Issue comment event: 64
- Push event: 52
- Pull request review event: 30
- Pull request review comment event: 13
- Create event: 16
Committers metadata
Last synced: 8 days ago
Total Commits: 988
Total Committers: 30
Avg Commits per committer: 32.933
Development Distribution Score (DDS): 0.149
Commits in past year: 42
Committers in past year: 4
Avg Commits per committer in past year: 10.5
Development Distribution Score (DDS) in past year: 0.452
| Name | Commits | |
|---|---|---|
| Markus Schirp | m****j@s****m | 841 |
| dependabot[bot] | 4****] | 44 |
| viralpraxis | i****k@g****m | 23 |
| Trent Ogren | t****2@g****m | 21 |
| Vsevolod Romashov | 7@7****u | 11 |
| Erik Berlin | s****k@g****m | 10 |
| Daniel Gollahon | d****n@g****m | 5 |
| Matijs van Zuijlen | m****s@m****t | 4 |
| Bartek Bułat | b****t@g****m | 3 |
| Dan Kubb | d****b@g****m | 3 |
| Nathan Wenneker | n****n@p****m | 2 |
| Sergei Prikhodko | p****a | 2 |
| Vladimir Dementyev | d****m@g****m | 2 |
| Filipp Pirozhkov | f****v@t****m | 1 |
| Adam Hess | a****1@g****m | 1 |
| Alan Wu | X****r | 1 |
| Alex Dowad | a****g@g****m | 1 |
| Guillaume Malette | g****e@s****m | 1 |
| Ivo Wever | i****r@g****m | 1 |
| John Backus | j****s@g****m | 1 |
| Olle Jonsson | o****n@g****m | 1 |
| Piotr Solnica | p****a@g****m | 1 |
| Piotr Szotkowski | c****l@c****t | 1 |
| Rodrigo Andrés Vilina | r****a@g****m | 1 |
| Russell Davis | r****l@s****m | 1 |
| Stan Hu | s****u@g****m | 1 |
| Vidar Hokstad | v****r@h****m | 1 |
| paulodeon | m****l@p****m | 1 |
| sanemat | o****n@g****m | 1 |
| vzvu3k6k | v****k | 1 |
Committer domains:
- paulodeon.com: 1
- hokstad.com: 1
- stripe.com: 1
- chastell.net: 1
- shopify.com: 1
- toptal.com: 1
- preferral.com: 1
- matijs.net: 1
- 7vn.ru: 1
- schirp-dso.com: 1
Issue and Pull Request metadata
Last synced: 7 days ago
Total issues: 24
Total pull requests: 192
Average time to close issues: about 1 year
Average time to close pull requests: 10 days
Total issue authors: 16
Total pull request authors: 10
Average comments per issue: 5.5
Average comments per pull request: 0.61
Merged pull request: 119
Bot issues: 0
Bot pull requests: 67
Past year issues: 2
Past year pull requests: 60
Past year average time to close issues: 2 months
Past year average time to close pull requests: 4 days
Past year issue authors: 2
Past year pull request authors: 5
Past year average comments per issue: 16.0
Past year average comments per pull request: 0.78
Past year merged pull request: 50
Past year bot issues: 0
Past year bot pull requests: 2
Top Issue Authors
- akimd (4)
- dgollahon (3)
- boutil (2)
- marcandre (2)
- prikha (2)
- camertron (1)
- csabahenk (1)
- belene (1)
- backus (1)
- petekinnecom (1)
- vidarh (1)
- neil-ongkingco-apollo (1)
- viralpraxis (1)
- pilipenok (1)
- mbj (1)
Top Pull Request Authors
- mbj (67)
- dependabot[bot] (67)
- viralpraxis (43)
- prikha (7)
- dgollahon (3)
- paulodeon (1)
- camertron (1)
- vidarh (1)
- sferik (1)
- vaporyhumo (1)
Top Issue Labels
- bug (5)
Top Pull Request Labels
- dependencies (67)
- bug (2)
- ruby (2)
Package metadata
- Total packages: 10
-
Total downloads:
- rubygems: 189,097,120 total
- Total docker downloads: 872,427,828
- Total dependent packages: 82 (may contain duplicates)
- Total dependent repositories: 4,254 (may contain duplicates)
- Total versions: 237
- Total maintainers: 3
gem.coop: unparser
Generate equivalent source for parser gem AST nodes
- Homepage: http://github.com/mbj/unparser
- Documentation: http://www.rubydoc.info/gems/unparser/
- Licenses: MIT
- Latest release: 0.8.2 (published 7 days ago)
- Last Synced: 2026-02-24T15:45:33.253Z (7 days ago)
- Versions: 80
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 94,548,560 Total
- Docker Downloads: 436,213,914
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.137%
- Downloads: 0.271%
- Docker downloads count: 0.275%
- Maintainers (3)
-
Funding:
- https://github.com/sponsors/mbj
rubygems.org: unparser
Generate equivalent source for parser gem AST nodes
- Homepage: http://github.com/mbj/unparser
- Documentation: http://www.rubydoc.info/gems/unparser/
- Licenses: MIT
- Latest release: 0.8.2 (published 7 days ago)
- Last Synced: 2026-02-24T15:48:10.319Z (7 days ago)
- Versions: 80
- Dependent Packages: 82
- Dependent Repositories: 4,254
- Downloads: 94,548,560 Total
- Docker Downloads: 436,213,914
-
Rankings:
- Docker downloads count: 0.341%
- Downloads: 0.368%
- Dependent packages count: 0.377%
- Dependent repos count: 0.482%
- Average: 1.411%
- Stargazers count: 3.214%
- Forks count: 3.684%
- Maintainers (3)
-
Funding:
- https://github.com/sponsors/mbj
proxy.golang.org: github.com/mbj/unparser
- Homepage:
- Documentation: https://pkg.go.dev/github.com/mbj/unparser#section-documentation
- Licenses: mit
- Latest release: v0.8.10 (published about 10 years ago)
- Last Synced: 2026-02-23T08:13:17.101Z (8 days ago)
- Versions: 70
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 5.421%
- Average: 5.603%
- Dependent repos count: 5.785%
debian-11: ruby-unparser
- Homepage: http://github.com/mbj/unparser
- Documentation: https://packages.debian.org/bullseye/ruby-unparser
- Licenses:
- Latest release: 0.4.7-2 (published 20 days ago)
- Last Synced: 2026-02-13T08:26:02.928Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-12: ruby-unparser
- Homepage: http://github.com/mbj/unparser
- Documentation: https://packages.debian.org/bookworm/ruby-unparser
- Licenses:
- Latest release: 0.4.7-2 (published 18 days ago)
- Last Synced: 2026-02-12T23:43:34.179Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
Dependencies
- mutant-license >= 0
- ast 2.4.2
- diff-lcs 1.5.0
- mutant 0.11.4
- mutant-license 0.1.1.2.2355046999240944981729280251890364410689.5
- mutant-rspec 0.11.4
- parallel 1.21.0
- parser 3.1.2.0
- rainbow 3.1.1
- regexp_parser 2.2.1
- rexml 3.2.5
- rspec 3.11.0
- rspec-core 3.11.0
- rspec-expectations 3.11.0
- rspec-its 1.3.0
- rspec-mocks 3.11.0
- rspec-support 3.11.0
- rubocop 1.26.0
- rubocop-ast 1.16.0
- rubocop-packaging 0.5.1
- ruby-progressbar 1.11.0
- sorbet-runtime 0.5.9636
- unicode-display_width 2.1.0
- unparser 0.6.5
- mutant ~> 0.11.4 development
- mutant-rspec ~> 0.11.4 development
- rspec ~> 3.9 development
- rspec-core ~> 3.9 development
- rspec-its ~> 1.3.0 development
- rubocop ~> 1.7 development
- rubocop-packaging ~> 0.5 development
- diff-lcs ~> 1.3
- parser >= 3.1.0
- actions/checkout v3 composite
- ruby/setup-ruby v1 composite
Score: 29.98930931646919