Class: Solidarity::AstProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/solidarity/ast_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAstProcessor

Returns a new instance of AstProcessor.



9
10
11
12
13
14
# File 'lib/solidarity/ast_processor.rb', line 9

def initialize
  @classes = []
  @modules = []
  @inheritances = [] # Format: [['ChildClass', 'ParentClass']]
  @includes = []     # Format: [['ClassOrModule', 'IncludedModule']]
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



7
8
9
# File 'lib/solidarity/ast_processor.rb', line 7

def classes
  @classes
end

#includesObject (readonly)

Returns the value of attribute includes.



7
8
9
# File 'lib/solidarity/ast_processor.rb', line 7

def includes
  @includes
end

#inheritancesObject (readonly)

Returns the value of attribute inheritances.



7
8
9
# File 'lib/solidarity/ast_processor.rb', line 7

def inheritances
  @inheritances
end

#modulesObject (readonly)

Returns the value of attribute modules.



7
8
9
# File 'lib/solidarity/ast_processor.rb', line 7

def modules
  @modules
end

Instance Method Details

#process_file(file_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/solidarity/ast_processor.rb', line 16

def process_file(file_path)
  content = File.read(file_path, encoding: 'UTF-8')
  begin
    ast = Parser::CurrentRuby.parse(content)
    traverse_ast(ast, nil) # Pass initial context as nil
  rescue Parser::SyntaxError => e
    warn "Warning: Could not parse #{file_path} due to syntax error: #{e.message}"
    nil # Return nil to indicate parsing failed
  rescue EncodingError => e
    warn "Warning: Could not read #{file_path} due to encoding error: #{e.message}. Attempting to force encoding to UTF-8."
    begin
      content.force_encoding('UTF-8')
      ast = Parser::CurrentRuby.parse(content)
      traverse_ast(ast, nil)
    rescue Parser::SyntaxError => e
      warn "Warning: Could not parse #{file_path} after forcing encoding due to syntax error: #{e.message}"
      nil
    rescue EncodingError => e
      warn "Warning: Could not read #{file_path} after forcing encoding due to encoding error: #{e.message}"
      nil
    end
  end
end