Project

ast_utils

0.01
No commit activity in last 3 years
No release in over 3 years
Ruby AST Utility
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 2.7.0
 Project 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:

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.

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.

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.

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.