Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soutaro/ast_utils
Utility over parser gem AST
https://github.com/soutaro/ast_utils
Last synced: 2 months ago
JSON representation
Utility over parser gem AST
- Host: GitHub
- URL: https://github.com/soutaro/ast_utils
- Owner: soutaro
- License: mit
- Created: 2017-08-11T18:27:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-03-19T04:46:43.000Z (over 3 years ago)
- Last Synced: 2024-09-19T15:48:55.840Z (3 months ago)
- Language: Ruby
- Size: 45.9 KB
- Stars: 3
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ruby-ast - ast_utils
README
# ASTUtils
ASTUtils provides some utility over parser gem AST, which aims to help analyzing Ruby programs.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'ast_utils'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install ast_utils
## Usage
### ASTUtils::Labeling
`ASTUTils::Labeling` implements local variables labeling.
In Ruby, local variable scope can be nested, and identification on local variables is not trivial.
This utility give labels to identify local variables.```rb
require "parser/current"
require "ast_utils"node = Parser::CurrentRuby.parse(source)
labeled = ASTUtils::Labeling.translate(node: node)
```### ASTUTils::Navigation
`AST` has `children` but no pointer to its parent.
```rb
require "parser/current"
require "ast_utils"node = Parser::CurrentRuby.parse(source)
navigation = ASTUtils::Navigation.from(node: node)parent = navigation.parent(child_node)
```### ASTUtils::Scope
`Scope` is about *scope* in Ruby.
It associates outer scope and its inner scope.
```rb
require "parser/current"
require "ast_utils"node = Parser::CurrentRuby.parse(source)
labeled = ASTUtils::Labeling.translate(node: node)
scope = ASTUtils::Scope.from(node: labeled)scope.root
scope.children(root)
scope.subs(root)
````children` includes `def` and iterator block, but `subs` only includes blocks.
It also defines `assignments` and `references` to refere assignments and references for local variables in the scope.
`assignments` returns pair of:
* Node which implements assignment
* Labeled local variable name which is assigned## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/soutaro/ast_utils.