File tree Expand file tree Collapse file tree 2 files changed +49
-41
lines changed
lib/rail_inspector/visitor
test/rail_inspector/visitor Expand file tree Collapse file tree 2 files changed +49
-41
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
- require "syntax_tree "
3
+ require "prism "
4
4
5
5
module RailInspector
6
6
module Visitor
7
- class HashToString < SyntaxTree ::Visitor
7
+ class HashToString < Prism ::Visitor
8
8
attr_reader :to_s
9
9
10
10
def initialize
11
11
@to_s = +""
12
12
end
13
13
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 << "{"
18
16
19
- case node . key
20
- when SyntaxTree ::StringLiteral
21
- @to_s << " => "
22
- end
17
+ if node . elements . length > 0
18
+ visit ( node . elements [ 0 ] )
23
19
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 << " "
25
27
end
26
28
27
- def visit_hash ( node )
28
- @to_s << "{"
29
+ @to_s << "}"
30
+ end
29
31
30
- if node . assocs . length > 0
31
- visit ( node . assocs [ 0 ] )
32
+ def visit_assoc_node ( node )
33
+ @to_s << " "
32
34
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 )
41
36
42
- @to_s << "}"
37
+ case node . key
38
+ in Prism ::SymbolNode
39
+ @to_s << ": "
40
+ in Prism ::StringNode
41
+ @to_s << " => "
43
42
end
44
43
45
- def visit_int ( node )
46
- @to_s << node . value
44
+ case node . value
45
+ when Prism ::SymbolNode
46
+ @to_s << ":"
47
47
end
48
48
49
- def visit_kw ( node )
50
- @to_s << node . value
51
- end
49
+ visit ( node . value )
50
+ end
52
51
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
57
55
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"
63
72
end
64
73
end
65
74
end
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
- require "syntax_tree"
4
-
3
+ require "test_helper"
5
4
require "rail_inspector/visitor/hash_to_string"
6
5
7
6
class HashToStringTest < Minitest ::Test
@@ -26,7 +25,7 @@ def test_string_keys_to_s
26
25
27
26
private
28
27
def string_for ( hash_as_string )
29
- ast = SyntaxTree . parse ( hash_as_string )
28
+ ast = Prism . parse ( hash_as_string ) . value
30
29
visitor = RailInspector ::Visitor ::HashToString . new
31
30
visitor . visit ( ast )
32
31
visitor . to_s
You can’t perform that action at this time.
0 commit comments