Module: Refactor::Util

Defined in:
lib/refactor.rb

Overview

Utilities for working with ASTs

Class Method Summary collapse

Class Method Details

.ast_from(string) ⇒ Object

Convert a string into its AST representation



25
26
27
# File 'lib/refactor.rb', line 25

def self.ast_from(string)
  processed_source_from(string).ast
end

.deconstruct_ast(string) ⇒ Object



12
13
14
# File 'lib/refactor.rb', line 12

def self.deconstruct_ast(string)
  deep_deconstruct(ast_from(string))
end

.deep_deconstruct(node) ⇒ Object

Makes it easier to break down an AST into what we’d like to match against eventually.



18
19
20
21
22
# File 'lib/refactor.rb', line 18

def self.deep_deconstruct(node)
  return node unless node.respond_to?(:deconstruct)

  node.deconstruct.map { deep_deconstruct(_1) }
end

.processed_source_from(string) ⇒ Object



29
30
31
# File 'lib/refactor.rb', line 29

def self.processed_source_from(string)
  RuboCop::ProcessedSource.new(string, RUBY_VERSION.to_f)
end