Class: Json::Merge::FileAnalysis
- Inherits:
-
Object
- Object
- Json::Merge::FileAnalysis
- Includes:
- Ast::Merge::FileAnalyzable
- Defined in:
- lib/json/merge/file_analysis.rb
Overview
Analyzes JSON file structure, extracting statements for merging.
This is the main analysis class that prepares JSON content for merging.
Instance Attribute Summary collapse
-
#ast ⇒ TreeHaver::Tree?
readonly
Parsed AST.
-
#errors ⇒ Array
readonly
Parse errors if any.
Class Method Summary collapse
-
.find_parser_path ⇒ String?
Find the parser library path using TreeHaver::GrammarFinder.
Instance Method Summary collapse
-
#fallthrough_node?(value) ⇒ Boolean
Override to detect tree-sitter nodes for signature generator fallthrough.
-
#initialize(source, signature_generator: nil, parser_path: nil, **options) ⇒ FileAnalysis
constructor
Initialize file analysis.
-
#root_node ⇒ NodeWrapper?
Get the root node of the parse tree.
-
#root_object ⇒ NodeWrapper?
Get the root object if the JSON document is an object.
-
#root_object_close_line ⇒ String?
Get the closing brace line of the root object (the line containing
}). -
#root_object_open_line ⇒ String?
Get the opening brace line of the root object (the line containing
{). -
#root_pairs ⇒ Array<NodeWrapper>
Get key-value pairs from the root object.
-
#valid? ⇒ Boolean
Check if parse was successful.
Constructor Details
#initialize(source, signature_generator: nil, parser_path: nil, **options) ⇒ FileAnalysis
Initialize file analysis
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/json/merge/file_analysis.rb', line 36 def initialize(source, signature_generator: nil, parser_path: nil, **) @source = source @lines = source.lines.map(&:chomp) @signature_generator = signature_generator @parser_path = parser_path || self.class.find_parser_path @errors = [] # **options captures any additional parameters (e.g., freeze_token, node_typing) for forward compatibility # Parse the JSON DebugLogger.time("FileAnalysis#parse_json") { parse_json } @statements = integrate_nodes DebugLogger.debug("FileAnalysis initialized", { signature_generator: signature_generator ? "custom" : "default", statements_count: @statements.size, valid: valid?, }) end |
Instance Attribute Details
#ast ⇒ TreeHaver::Tree? (readonly)
Returns Parsed AST.
16 17 18 |
# File 'lib/json/merge/file_analysis.rb', line 16 def ast @ast end |
#errors ⇒ Array (readonly)
Returns Parse errors if any.
19 20 21 |
# File 'lib/json/merge/file_analysis.rb', line 19 def errors @errors end |
Class Method Details
.find_parser_path ⇒ String?
Find the parser library path using TreeHaver::GrammarFinder
25 26 27 |
# File 'lib/json/merge/file_analysis.rb', line 25 def find_parser_path TreeHaver::GrammarFinder.new(:json).find_library_path end |
Instance Method Details
#fallthrough_node?(value) ⇒ Boolean
Override to detect tree-sitter nodes for signature generator fallthrough
65 66 67 |
# File 'lib/json/merge/file_analysis.rb', line 65 def fallthrough_node?(value) value.is_a?(NodeWrapper) || super end |
#root_node ⇒ NodeWrapper?
Get the root node of the parse tree
71 72 73 74 75 |
# File 'lib/json/merge/file_analysis.rb', line 71 def root_node return unless valid? NodeWrapper.new(@ast.root_node, lines: @lines, source: @source) end |
#root_object ⇒ NodeWrapper?
Get the root object if the JSON document is an object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/json/merge/file_analysis.rb', line 79 def root_object return unless valid? root = @ast.root_node return unless root # JSON root should be a document containing an object or array root.each do |child| if child.type.to_s == "object" return NodeWrapper.new(child, lines: @lines, source: @source) end end nil end |
#root_object_close_line ⇒ String?
Get the closing brace line of the root object (the line containing })
105 106 107 108 109 110 |
# File 'lib/json/merge/file_analysis.rb', line 105 def root_object_close_line obj = root_object return unless obj&.end_line line_at(obj.end_line)&.chomp end |
#root_object_open_line ⇒ String?
Get the opening brace line of the root object (the line containing {)
96 97 98 99 100 101 |
# File 'lib/json/merge/file_analysis.rb', line 96 def root_object_open_line obj = root_object return unless obj&.start_line line_at(obj.start_line)&.chomp end |
#root_pairs ⇒ Array<NodeWrapper>
Get key-value pairs from the root object
114 115 116 117 118 119 |
# File 'lib/json/merge/file_analysis.rb', line 114 def root_pairs obj = root_object return [] unless obj obj.pairs end |
#valid? ⇒ Boolean
Check if parse was successful
58 59 60 |
# File 'lib/json/merge/file_analysis.rb', line 58 def valid? @errors.empty? && !@ast.nil? end |