Skip to content

Commit

Permalink
Improved logging consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBakerEffendi committed Oct 7, 2024
1 parent 89f2438 commit 6216d7a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion exe/ruby_ast_gen
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ begin
opts = Slop.parse do |o|
o.string '-i', '--input', 'The input file or directory', required: true
o.string '-o', '--output', 'The output directory', default: '.ast'
o.string '-e', '--exclude', 'The exclusion regex', default: '(^|\\\\)(test|tests|vendor|spec)($$|\\\\)'
o.string '-e', '--exclude', 'The exclusion regex', default: '^(tests?|vendor|spec)'
o.string '-l', '--log', 'The logging level', default: 'warn'
o.on '--version', 'Print the version' do
puts RubyAstGen::VERSION
Expand Down
6 changes: 3 additions & 3 deletions lib/ruby_ast_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def self.process_file(file_path, output_dir, exclude_regex, base_dir)
return unless ruby_file?(file_path) # Skip if it's not a Ruby-related file

begin
ast = parse_file(file_path)
ast = parse_file(file_path, relative_input_path)
return unless ast

output_path = File.join(output_dir, "#{relative_path}.json")
Expand Down Expand Up @@ -80,14 +80,14 @@ def self.process_directory(dir_path, output_dir, exclude_regex)
end
end

def self.parse_file(file_path)
def self.parse_file(file_path, relative_input_path)
code = File.read(file_path)
buffer = Parser::Source::Buffer.new(file_path)
buffer.source = code
parser = Parser::CurrentRuby.new
ast = parser.parse(buffer)
return unless ast
NodeHandling::ast_to_json(ast, code)
NodeHandling::ast_to_json(ast, code, file_path: relative_input_path)
rescue Parser::SyntaxError => e
@logger.error "Failed to parse #{file_path}: #{e.message}"
nil
Expand Down
10 changes: 5 additions & 5 deletions lib/ruby_ast_gen/node_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.fetch_member(loc, method)
loc.public_send(method) rescue -1
end

def self.ast_to_json(node, code, current_depth = 0)
def self.ast_to_json(node, code, current_depth: 0, file_path: nil)
return unless node.is_a?(Parser::AST::Node)

loc = node.location
Expand All @@ -45,13 +45,13 @@ def self.ast_to_json(node, code, current_depth = 0)
meta_data: meta_data,
children: node.children.map do |child|
if child.is_a?(Parser::AST::Node)
ast_to_json(child, code, current_depth + 1) # Recursively process child nodes
ast_to_json(child, code, current_depth: current_depth + 1, file_path: file_path) # Recursively process child nodes
else
child # If it's not a node (e.g., literal), return as-is
end
end
}
add_node_properties(node.type, base_hash)
add_node_properties(node.type, base_hash, file_path)
return base_hash
end

Expand All @@ -71,7 +71,7 @@ def self.extract_code_snippet(location, source_code)
self.truncate_string(snippet.strip, 60)
end

def self.add_node_properties(node_type, base_map)
def self.add_node_properties(node_type, base_map, file_path)
children = base_map.delete(:children)

case node_type
Expand Down Expand Up @@ -203,7 +203,7 @@ def self.add_node_properties(node_type, base_map)
base_map[:children] = children

else
RubyAstGen::logger.warn "Unhandled AST node type: #{node_type}"
RubyAstGen::logger.warn "Unhandled AST node type: #{node_type} - #{file_path}"
base_map[:children] = children
end
end
Expand Down
13 changes: 9 additions & 4 deletions spec/ruby_ast_gen_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
require 'tempfile'

RSpec.describe RubyAstGen do
let(:temp_file) { Tempfile.new('test_ruby_code') }
temp_name = ""
let(:temp_file) {
file = Tempfile.new('test_ruby_code')
temp_name = File.basename(file.path)
file
}

after(:each) do
temp_file.close
Expand All @@ -21,7 +26,7 @@ class Foo
CONST = 1
end
CODE
ast = RubyAstGen::parse_file(temp_file.path)
ast = RubyAstGen::parse_file(temp_file.path, temp_name)
expect(ast).not_to be_nil
end

Expand All @@ -33,7 +38,7 @@ class Foo
multiple lines using heredoc.
TEXT
CODE
ast = RubyAstGen::parse_file(temp_file.path)
ast = RubyAstGen::parse_file(temp_file.path, temp_name)
expect(ast).not_to be_nil
end

Expand All @@ -48,7 +53,7 @@ class Foo
It also spans multiple lines.
ARG2
CODE
ast = RubyAstGen::parse_file(temp_file.path)
ast = RubyAstGen::parse_file(temp_file.path, temp_name)
expect(ast).not_to be_nil
end
end

0 comments on commit 6216d7a

Please sign in to comment.