From 6216d7a82762cba15b924672793d4db6acfc02e5 Mon Sep 17 00:00:00 2001 From: David Baker Effendi Date: Mon, 7 Oct 2024 13:45:55 +0200 Subject: [PATCH] Improved logging consistency --- exe/ruby_ast_gen | 2 +- lib/ruby_ast_gen.rb | 6 +++--- lib/ruby_ast_gen/node_handling.rb | 10 +++++----- spec/ruby_ast_gen_spec.rb | 13 +++++++++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/exe/ruby_ast_gen b/exe/ruby_ast_gen index 49b78de..0d93b66 100755 --- a/exe/ruby_ast_gen +++ b/exe/ruby_ast_gen @@ -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 diff --git a/lib/ruby_ast_gen.rb b/lib/ruby_ast_gen.rb index bbcf8f8..34bdd52 100644 --- a/lib/ruby_ast_gen.rb +++ b/lib/ruby_ast_gen.rb @@ -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") @@ -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 diff --git a/lib/ruby_ast_gen/node_handling.rb b/lib/ruby_ast_gen/node_handling.rb index e391843..0d2d0aa 100644 --- a/lib/ruby_ast_gen/node_handling.rb +++ b/lib/ruby_ast_gen/node_handling.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/ruby_ast_gen_spec.rb b/spec/ruby_ast_gen_spec.rb index 9b0236c..090b719 100644 --- a/spec/ruby_ast_gen_spec.rb +++ b/spec/ruby_ast_gen_spec.rb @@ -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 @@ -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 @@ -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 @@ -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