clean_ripper 0.0.1.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/clean_sexp_builder.rb +12 -8
- data/test/clean_ripper_test.rb +69 -6
- metadata +1 -1
data/lib/clean_sexp_builder.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
|
2
2
|
require 'ripper'
|
3
|
-
|
3
|
+
|
4
4
|
class Ripper
|
5
5
|
class CleanSexpBuilder < SexpBuilder
|
6
6
|
|
@@ -97,6 +97,10 @@ class Ripper
|
|
97
97
|
var
|
98
98
|
end
|
99
99
|
|
100
|
+
def on_aref(*args)
|
101
|
+
[:call, args[0], :[], args[1], nil]
|
102
|
+
end
|
103
|
+
|
100
104
|
def on_gvar(*args)
|
101
105
|
[:gvar, args.first.to_sym]
|
102
106
|
end
|
@@ -113,20 +117,20 @@ class Ripper
|
|
113
117
|
[:ident, args.first.to_sym]
|
114
118
|
end
|
115
119
|
|
116
|
-
def
|
117
|
-
|
120
|
+
def on_op(*args)
|
121
|
+
args[0].to_sym
|
118
122
|
end
|
119
123
|
|
120
124
|
def on_method_add_arg(*args)
|
121
|
-
[:call, *args.first, (args[1][1] if args[1]) || [], nil]
|
125
|
+
[:call, *args.first[1..-3], (args[1][1] if args[1]) || [], nil]
|
122
126
|
end
|
123
127
|
|
124
128
|
def on_call(*args)
|
125
|
-
[args[0], args[-1][1]]
|
129
|
+
[:call, args[0], args[-1][1], [], nil]
|
126
130
|
end
|
127
131
|
|
128
132
|
def on_fcall(*args)
|
129
|
-
[nil, args[0][1]]
|
133
|
+
[:call, nil, args[0][1], [], nil]
|
130
134
|
end
|
131
135
|
|
132
136
|
def on_args_add_block(*args)
|
@@ -307,11 +311,11 @@ class Ripper
|
|
307
311
|
end
|
308
312
|
|
309
313
|
def on_field(*args)
|
310
|
-
[:
|
314
|
+
[:call, args[0], args[2][1], [], nil]
|
311
315
|
end
|
312
316
|
|
313
317
|
def on_aref_field(*args)
|
314
|
-
[:
|
318
|
+
[:call, args[0], :[], args[1], nil]
|
315
319
|
end
|
316
320
|
|
317
321
|
def on_mlhs_paren(*args)
|
data/test/clean_ripper_test.rb
CHANGED
@@ -297,10 +297,7 @@ class CleanRipperTest < Test::Unit::TestCase
|
|
297
297
|
[:lvar, :a],
|
298
298
|
[:assign, [:lvar, :b], [:lit, 2]]
|
299
299
|
],
|
300
|
-
[:
|
301
|
-
[:lvar, :a],
|
302
|
-
[:binary, [:lvar, :a], :+, [:lit, 5]]
|
303
|
-
]
|
300
|
+
[:opassign, [:lvar, :a], :"+=", [:lit, 5]]
|
304
301
|
]
|
305
302
|
|
306
303
|
# Ripper output
|
@@ -903,11 +900,11 @@ class CleanRipperTest < Test::Unit::TestCase
|
|
903
900
|
# CleanRipper output
|
904
901
|
clean_ripper = [:program,
|
905
902
|
[:massign,
|
906
|
-
[[:
|
903
|
+
[[:call, [:ident, :a], :x, [], nil], [:ident, :b]],
|
907
904
|
[[:lit, 1], [:lit, 2]]
|
908
905
|
],
|
909
906
|
[:massign,
|
910
|
-
[[:
|
907
|
+
[[:call, [:ident, :a], :[], [[:lit, 2]], nil], [:ident, :b]],
|
911
908
|
[[:lit, 1], [:lit, 2]]
|
912
909
|
]
|
913
910
|
]
|
@@ -1044,5 +1041,71 @@ class CleanRipperTest < Test::Unit::TestCase
|
|
1044
1041
|
assert_equal(clean_ripper, CleanRipper.parse(code))
|
1045
1042
|
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
1046
1043
|
end
|
1044
|
+
def test_021
|
1045
|
+
code = <<-'END'
|
1046
|
+
foo["bar".to_sym]
|
1047
|
+
a[b] += 1
|
1048
|
+
END
|
1049
|
+
|
1050
|
+
# CleanRipper output
|
1051
|
+
clean_ripper = [:program,
|
1052
|
+
[:call,
|
1053
|
+
[:ident, :foo],
|
1054
|
+
:[],
|
1055
|
+
[[:call, [:lit, "bar"], :to_sym, [], nil]],
|
1056
|
+
nil
|
1057
|
+
],
|
1058
|
+
[:opassign,
|
1059
|
+
[:call, [:ident, :a], :[], [[:ident, :b]], nil],
|
1060
|
+
:"+=",
|
1061
|
+
[:lit, 1]
|
1062
|
+
]
|
1063
|
+
]
|
1064
|
+
|
1065
|
+
# Ripper output
|
1066
|
+
ripper = [:program,
|
1067
|
+
[:stmts_add,
|
1068
|
+
[:stmts_add,
|
1069
|
+
[:stmts_new],
|
1070
|
+
[:aref,
|
1071
|
+
[:var_ref, [:@ident, "foo", [1, 4]]],
|
1072
|
+
[:args_add_block,
|
1073
|
+
[:args_add,
|
1074
|
+
[:args_new],
|
1075
|
+
[:call,
|
1076
|
+
[:string_literal,
|
1077
|
+
[:string_add,
|
1078
|
+
[:string_content],
|
1079
|
+
[:@tstring_content, "bar", [1, 9]]
|
1080
|
+
]
|
1081
|
+
],
|
1082
|
+
:".",
|
1083
|
+
[:@ident, "to_sym", [1, 14]]
|
1084
|
+
]
|
1085
|
+
],
|
1086
|
+
false
|
1087
|
+
]
|
1088
|
+
]
|
1089
|
+
],
|
1090
|
+
[:opassign,
|
1091
|
+
[:aref_field,
|
1092
|
+
[:var_ref, [:@ident, "a", [2, 4]]],
|
1093
|
+
[:args_add_block,
|
1094
|
+
[:args_add,
|
1095
|
+
[:args_new],
|
1096
|
+
[:var_ref, [:@ident, "b", [2, 6]]]
|
1097
|
+
],
|
1098
|
+
false
|
1099
|
+
]
|
1100
|
+
],
|
1101
|
+
[:@op, "+=", [2, 9]],
|
1102
|
+
[:@int, "1", [2, 12]]
|
1103
|
+
]
|
1104
|
+
]
|
1105
|
+
]
|
1106
|
+
|
1107
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
1108
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
1109
|
+
end
|
1047
1110
|
|
1048
1111
|
end
|