Skip to content

Commit 96f13d1

Browse files
committed
[rail_inspector] Prism for Visitor::HashToString
1 parent ec7b1ca commit 96f13d1

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

tools/rail_inspector/lib/rail_inspector/visitor/hash_to_string.rb

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,74 @@
11
# frozen_string_literal: true
22

3-
require "syntax_tree"
3+
require "prism"
44

55
module RailInspector
66
module Visitor
7-
class HashToString < SyntaxTree::Visitor
7+
class HashToString < Prism::Visitor
88
attr_reader :to_s
99

1010
def initialize
1111
@to_s = +""
1212
end
1313

14-
visit_methods do
15-
def visit_assoc(node)
16-
@to_s << " "
17-
visit(node.key)
14+
def visit_hash_node(node)
15+
@to_s << "{"
1816

19-
case node.key
20-
when SyntaxTree::StringLiteral
21-
@to_s << " => "
22-
end
17+
if node.elements.length > 0
18+
visit(node.elements[0])
2319

24-
visit(node.value)
20+
if node.elements.length > 1
21+
node.elements[1..-1].each do |a|
22+
@to_s << ","
23+
visit(a)
24+
end
25+
end
26+
@to_s << " "
2527
end
2628

27-
def visit_hash(node)
28-
@to_s << "{"
29+
@to_s << "}"
30+
end
2931

30-
if node.assocs.length > 0
31-
visit(node.assocs[0])
32+
def visit_assoc_node(node)
33+
@to_s << " "
3234

33-
if node.assocs.length > 1
34-
node.assocs[1..-1].each do |a|
35-
@to_s << ","
36-
visit(a)
37-
end
38-
end
39-
@to_s << " "
40-
end
35+
visit(node.key)
4136

42-
@to_s << "}"
37+
case node.key
38+
in Prism::SymbolNode
39+
@to_s << ": "
40+
in Prism::StringNode
41+
@to_s << " => "
4342
end
4443

45-
def visit_int(node)
46-
@to_s << node.value
44+
case node.value
45+
when Prism::SymbolNode
46+
@to_s << ":"
4747
end
4848

49-
def visit_kw(node)
50-
@to_s << node.value
51-
end
49+
visit(node.value)
50+
end
5251

53-
def visit_label(node)
54-
@to_s << node.value
55-
@to_s << " "
56-
end
52+
def visit_integer_node(node)
53+
@to_s << node.value.to_s
54+
end
5755

58-
def visit_tstring_content(node)
59-
@to_s << '"'
60-
@to_s << node.value
61-
@to_s << '"'
62-
end
56+
def visit_string_node(node)
57+
@to_s << '"'
58+
@to_s << node.unescaped
59+
@to_s << '"'
60+
end
61+
62+
def visit_symbol_node(node)
63+
@to_s << node.unescaped
64+
end
65+
66+
def visit_true_node(node)
67+
@to_s << "true"
68+
end
69+
70+
def visit_false_node(node)
71+
@to_s << "false"
6372
end
6473
end
6574
end

tools/rail_inspector/test/rail_inspector/visitor/hash_to_string_test.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "syntax_tree"
4-
3+
require "test_helper"
54
require "rail_inspector/visitor/hash_to_string"
65

76
class HashToStringTest < Minitest::Test
@@ -26,7 +25,7 @@ def test_string_keys_to_s
2625

2726
private
2827
def string_for(hash_as_string)
29-
ast = SyntaxTree.parse(hash_as_string)
28+
ast = Prism.parse(hash_as_string).value
3029
visitor = RailInspector::Visitor::HashToString.new
3130
visitor.visit(ast)
3231
visitor.to_s

0 commit comments

Comments
 (0)