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
activerecord rubocop activejob mvc code-formatter static-code-analysis rubygem rspec feature-flag rack
Last synced: about 5 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 (over 12 years ago)
- Default Branch: main
- Last Pushed: 2025-11-03T14:20:11.000Z (about 1 month ago)
- Last Synced: 2025-11-27T18:49:21.451Z (14 days ago)
- Topics: parser, ruby, ruby-syntax, unparser
- Language: Ruby
- Homepage:
- Size: 1.28 MB
- Stars: 323
- Watchers: 9
- Forks: 47
- 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
- Issues event: 11
- Watch event: 8
- Delete event: 13
- Issue comment event: 73
- Push event: 56
- Pull request review comment event: 14
- Pull request review event: 30
- Pull request event: 60
- Fork event: 2
- Create event: 18
Last Year
- Issues event: 10
- Watch event: 7
- Delete event: 12
- Issue comment event: 69
- Push event: 56
- Pull request review comment event: 14
- Pull request review event: 30
- Pull request event: 58
- Fork event: 2
- Create event: 17
Committers metadata
Last synced: 7 days ago
Total Commits: 978
Total Committers: 29
Avg Commits per committer: 33.724
Development Distribution Score (DDS): 0.14
Commits in past year: 37
Committers in past year: 3
Avg Commits per committer in past year: 12.333
Development Distribution Score (DDS) in past year: 0.378
| 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 |
| 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: 21 days ago
Total issues: 23
Total pull requests: 190
Average time to close issues: 10 months
Average time to close pull requests: 10 days
Total issue authors: 15
Total pull request authors: 9
Average comments per issue: 5.7
Average comments per pull request: 0.62
Merged pull request: 118
Bot issues: 0
Bot pull requests: 67
Past year issues: 4
Past year pull requests: 63
Past year average time to close issues: 2 months
Past year average time to close pull requests: 3 days
Past year issue authors: 3
Past year pull request authors: 4
Past year average comments per issue: 8.0
Past year average comments per pull request: 0.75
Past year merged pull request: 54
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)
- kddnewton (1)
Top Pull Request Authors
- dependabot[bot] (67)
- mbj (66)
- viralpraxis (43)
- prikha (7)
- dgollahon (3)
- paulodeon (1)
- camertron (1)
- vidarh (1)
- vaporyhumo (1)
Top Issue Labels
- bug (5)
Top Pull Request Labels
- dependencies (67)
- bug (2)
- ruby (2)
Package metadata
- Total packages: 3
-
Total downloads:
- rubygems: 183,471,914 total
- Total docker downloads: 872,427,828
- Total dependent packages: 82 (may contain duplicates)
- Total dependent repositories: 4,254 (may contain duplicates)
- Total versions: 228
- 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.1 (published about 1 month ago)
- Last Synced: 2025-12-08T13:01:49.628Z (4 days ago)
- Versions: 79
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 91,727,694 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.1 (published about 1 month ago)
- Last Synced: 2025-12-08T21:01:49.404Z (3 days ago)
- Versions: 79
- Dependent Packages: 82
- Dependent Repositories: 4,254
- Downloads: 91,744,220 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 almost 10 years ago)
- Last Synced: 2025-12-07T18:02:13.542Z (4 days ago)
- Versions: 70
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 5.421%
- Average: 5.603%
- Dependent repos count: 5.785%
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.941017306292068