Skip to content

Commit 829f1a0

Browse files
authored
[ruby] Set lineNumbers to -1 when processing ERB file (#32)
* [ruby] Set lineNumbers to -1 when processing ERB file * [ruby] pass is_erb flag for recursive calls * [ruby] update is_erb naming in ternary
1 parent c6d2429 commit 829f1a0

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

lib/ruby_ast_gen.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ def self.process_directory(dir_path, output_dir, exclude_regex, max_threads = 10
117117
end
118118

119119
def self.parse_file(file_path, relative_input_path)
120+
is_erb = false
120121
code =
121122
if ruby_file?(file_path)
122123
File.read(file_path)
123124
else
125+
is_erb = true
124126
file_content = File.read(file_path)
125127
get_erb_content(file_content)
126128
end
@@ -130,7 +132,7 @@ def self.parse_file(file_path, relative_input_path)
130132
parser = Parser::CurrentRuby.new
131133
ast = parser.parse(buffer)
132134
return unless ast
133-
json_ast = NodeHandling::ast_to_json(ast, code, file_path: relative_input_path)
135+
json_ast = NodeHandling::ast_to_json(ast, code, file_path: relative_input_path, is_erb: is_erb)
134136
json_ast[:file_path] = file_path
135137
json_ast[:rel_file_path] = relative_input_path
136138
json_ast

lib/ruby_ast_gen/erb_to_ruby_transformer.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ def visit(node)
142142
@output << code
143143
end
144144
end
145-
# Using this to determine if we should throw a StandardError for "invalid" ERB
146145
when :newline
147146
else
148147
RubyAstGen::Logger::debug("Invalid node type: #{node}")

lib/ruby_ast_gen/node_handling.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def self.fetch_member(loc, method)
2525
loc.public_send(method) rescue -1
2626
end
2727

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

3131
loc = node.location
3232
meta_data = {
33-
start_line: fetch_member(loc, :line),
34-
start_column: fetch_member(loc, :column),
35-
end_line: fetch_member(loc, :last_line),
36-
end_column: fetch_member(loc, :last_column),
33+
start_line: is_erb ? -1 : fetch_member(loc, :line),
34+
start_column: is_erb ? -1 : fetch_member(loc, :column),
35+
end_line: is_erb ? -1 : fetch_member(loc, :last_line),
36+
end_column: is_erb ? -1 : fetch_member(loc, :last_column),
3737
offset_start: loc&.expression&.begin_pos,
3838
offset_end: loc&.expression&.end_pos,
3939
code: self.extract_code_snippet(loc, code)
@@ -48,7 +48,7 @@ def self.ast_to_json(node, code, current_depth: 0, file_path: nil)
4848
meta_data: meta_data,
4949
children: node.children.map do |child|
5050
if child.is_a?(Parser::AST::Node)
51-
ast_to_json(child, code, current_depth: current_depth + 1, file_path: file_path) # Recursively process child nodes
51+
ast_to_json(child, code, current_depth: current_depth + 1, file_path: file_path, is_erb: is_erb) # Recursively process child nodes
5252
else
5353
child # If it's not a node (e.g., literal), return as-is
5454
end

0 commit comments

Comments
 (0)