Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ruby_ast_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ def self.process_directory(dir_path, output_dir, exclude_regex, max_threads = 10
end

def self.parse_file(file_path, relative_input_path)
is_erb = false
code =
if ruby_file?(file_path)
File.read(file_path)
else
is_erb = true
file_content = File.read(file_path)
get_erb_content(file_content)
end
Expand All @@ -130,7 +132,7 @@ def self.parse_file(file_path, relative_input_path)
parser = Parser::CurrentRuby.new
ast = parser.parse(buffer)
return unless ast
json_ast = NodeHandling::ast_to_json(ast, code, file_path: relative_input_path)
json_ast = NodeHandling::ast_to_json(ast, code, file_path: relative_input_path, is_erb: is_erb)
json_ast[:file_path] = file_path
json_ast[:rel_file_path] = relative_input_path
json_ast
Expand Down
1 change: 0 additions & 1 deletion lib/ruby_ast_gen/erb_to_ruby_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def visit(node)
@output << code
end
end
# Using this to determine if we should throw a StandardError for "invalid" ERB
when :newline
else
RubyAstGen::Logger::debug("Invalid node type: #{node}")
Expand Down
12 changes: 6 additions & 6 deletions lib/ruby_ast_gen/node_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def self.fetch_member(loc, method)
loc.public_send(method) rescue -1
end

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

loc = node.location
meta_data = {
start_line: fetch_member(loc, :line),
start_column: fetch_member(loc, :column),
end_line: fetch_member(loc, :last_line),
end_column: fetch_member(loc, :last_column),
start_line: is_erb ? -1 : fetch_member(loc, :line),
start_column: is_erb ? -1 : fetch_member(loc, :column),
end_line: is_erb ? -1 : fetch_member(loc, :last_line),
end_column: is_erb ? -1 : fetch_member(loc, :last_column),
offset_start: loc&.expression&.begin_pos,
offset_end: loc&.expression&.end_pos,
code: self.extract_code_snippet(loc, code)
Expand All @@ -48,7 +48,7 @@ def self.ast_to_json(node, code, current_depth: 0, file_path: nil)
meta_data: meta_data,
children: node.children.map do |child|
if child.is_a?(Parser::AST::Node)
ast_to_json(child, code, current_depth: current_depth + 1, file_path: file_path) # Recursively process child nodes
ast_to_json(child, code, current_depth: current_depth + 1, file_path: file_path, is_erb: is_erb) # Recursively process child nodes
else
child # If it's not a node (e.g., literal), return as-is
end
Expand Down