synvert-core 1.4.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +0 -1
  3. data/.gitignore +0 -5
  4. data/CHANGELOG.md +12 -0
  5. data/Gemfile +0 -3
  6. data/Gemfile.lock +101 -0
  7. data/Guardfile +0 -9
  8. data/README.md +31 -13
  9. data/Rakefile +1 -15
  10. data/lib/synvert/core/engine/erb.rb +1 -1
  11. data/lib/synvert/core/engine.rb +1 -1
  12. data/lib/synvert/core/node_ext.rb +0 -639
  13. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +20 -17
  14. data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -5
  15. data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -1
  16. data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -5
  17. data/lib/synvert/core/rewriter/condition.rb +5 -1
  18. data/lib/synvert/core/rewriter/instance.rb +91 -140
  19. data/lib/synvert/core/rewriter/scope/query_scope.rb +8 -6
  20. data/lib/synvert/core/rewriter/scope/within_scope.rb +4 -87
  21. data/lib/synvert/core/rewriter.rb +0 -10
  22. data/lib/synvert/core/version.rb +1 -1
  23. data/lib/synvert/core.rb +4 -6
  24. data/spec/synvert/core/engine/erb_spec.rb +3 -3
  25. data/spec/synvert/core/node_ext_spec.rb +0 -965
  26. data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +21 -1
  27. data/spec/synvert/core/rewriter/instance_spec.rb +64 -131
  28. data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +1 -4
  29. data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +1 -16
  30. data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +22 -13
  31. data/synvert-core-ruby.gemspec +5 -3
  32. metadata +46 -62
  33. data/lib/synvert/core/array_ext.rb +0 -48
  34. data/lib/synvert/core/node_query/compiler/array.rb +0 -34
  35. data/lib/synvert/core/node_query/compiler/attribute.rb +0 -39
  36. data/lib/synvert/core/node_query/compiler/attribute_list.rb +0 -24
  37. data/lib/synvert/core/node_query/compiler/basic_selector.rb +0 -28
  38. data/lib/synvert/core/node_query/compiler/boolean.rb +0 -23
  39. data/lib/synvert/core/node_query/compiler/comparable.rb +0 -86
  40. data/lib/synvert/core/node_query/compiler/dynamic_attribute.rb +0 -51
  41. data/lib/synvert/core/node_query/compiler/expression.rb +0 -41
  42. data/lib/synvert/core/node_query/compiler/float.rb +0 -23
  43. data/lib/synvert/core/node_query/compiler/identifier.rb +0 -41
  44. data/lib/synvert/core/node_query/compiler/integer.rb +0 -23
  45. data/lib/synvert/core/node_query/compiler/invalid_operator_error.rb +0 -7
  46. data/lib/synvert/core/node_query/compiler/nil.rb +0 -23
  47. data/lib/synvert/core/node_query/compiler/parse_error.rb +0 -7
  48. data/lib/synvert/core/node_query/compiler/regexp.rb +0 -37
  49. data/lib/synvert/core/node_query/compiler/selector.rb +0 -113
  50. data/lib/synvert/core/node_query/compiler/string.rb +0 -23
  51. data/lib/synvert/core/node_query/compiler/symbol.rb +0 -23
  52. data/lib/synvert/core/node_query/compiler.rb +0 -25
  53. data/lib/synvert/core/node_query/lexer.rex +0 -99
  54. data/lib/synvert/core/node_query/lexer.rex.rb +0 -299
  55. data/lib/synvert/core/node_query/parser.racc.rb +0 -306
  56. data/lib/synvert/core/node_query/parser.y +0 -60
  57. data/lib/synvert/core/node_query.rb +0 -36
  58. data/lib/synvert/core/rewriter/action/append_action.rb +0 -28
  59. data/lib/synvert/core/rewriter/action/delete_action.rb +0 -34
  60. data/lib/synvert/core/rewriter/action/insert_action.rb +0 -34
  61. data/lib/synvert/core/rewriter/action/insert_after_action.rb +0 -22
  62. data/lib/synvert/core/rewriter/action/prepend_action.rb +0 -44
  63. data/lib/synvert/core/rewriter/action/remove_action.rb +0 -56
  64. data/lib/synvert/core/rewriter/action/replace_action.rb +0 -33
  65. data/lib/synvert/core/rewriter/action/replace_with_action.rb +0 -36
  66. data/lib/synvert/core/rewriter/action/wrap_action.rb +0 -37
  67. data/lib/synvert/core/rewriter/action.rb +0 -102
  68. data/spec/synvert/core/node_query/lexer_spec.rb +0 -580
  69. data/spec/synvert/core/node_query/parser_spec.rb +0 -337
  70. data/spec/synvert/core/rewriter/action/append_action_spec.rb +0 -70
  71. data/spec/synvert/core/rewriter/action/delete_action_spec.rb +0 -26
  72. data/spec/synvert/core/rewriter/action/insert_action_spec.rb +0 -70
  73. data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +0 -26
  74. data/spec/synvert/core/rewriter/action/prepend_action_spec.rb +0 -175
  75. data/spec/synvert/core/rewriter/action/remove_action_spec.rb +0 -26
  76. data/spec/synvert/core/rewriter/action/replace_action_spec.rb +0 -28
  77. data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +0 -59
  78. data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +0 -31
  79. data/spec/synvert/core/rewriter/action_spec.rb +0 -14
@@ -1,306 +0,0 @@
1
- #
2
- # DO NOT MODIFY!!!!
3
- # This file is automatically generated by Racc 1.6.0
4
- # from Racc grammar file "".
5
- #
6
-
7
- require 'racc/parser.rb'
8
- module Synvert
9
- module Core
10
- module NodeQuery
11
- class Parser < Racc::Parser
12
-
13
- def initialize
14
- @lexer = Lexer.new
15
- end
16
-
17
- def parse string
18
- @lexer.parse string
19
- do_parse
20
- end
21
-
22
- def next_token
23
- @lexer.next_token
24
- end
25
- ##### State transition tables begin ###
26
-
27
- racc_action_table = [
28
- 7, 7, 6, 8, 10, 34, 14, 4, 5, 15,
29
- 26, 18, 24, 19, 20, 21, 14, 38, 39, 27,
30
- 28, 29, 30, 31, 32, 33, 7, 41, 7, 6,
31
- nil, 34, nil, 7, 4, 5, 26, nil, 34, 35,
32
- nil, nil, nil, 26, nil, 27, 28, 29, 30, 31,
33
- 32, 33, 27, 28, 29, 30, 31, 32, 33, 7,
34
- 6, 7, 6, 7, 6, 4, 5, 4, 5, 4,
35
- 5 ]
36
-
37
- racc_action_check = [
38
- 21, 0, 0, 1, 4, 21, 7, 0, 0, 8,
39
- 21, 14, 21, 16, 17, 18, 20, 26, 36, 21,
40
- 21, 21, 21, 21, 21, 21, 24, 38, 2, 2,
41
- nil, 24, nil, 37, 2, 2, 24, nil, 37, 24,
42
- nil, nil, nil, 37, nil, 24, 24, 24, 24, 24,
43
- 24, 24, 37, 37, 37, 37, 37, 37, 37, 5,
44
- 5, 6, 6, 10, 10, 5, 5, 6, 6, 10,
45
- 10 ]
46
-
47
- racc_action_pointer = [
48
- -1, 3, 26, nil, -12, 57, 59, -4, 9, nil,
49
- 61, nil, nil, nil, 6, nil, -4, 3, -3, nil,
50
- 6, -2, nil, nil, 24, nil, -3, nil, nil, nil,
51
- nil, nil, nil, nil, nil, nil, 3, 31, 14, nil,
52
- nil, nil ]
53
-
54
- racc_action_default = [
55
- -26, -26, -2, -3, -26, -26, -26, -7, -26, -1,
56
- -26, -5, -6, -8, -26, 42, -26, -26, -26, -4,
57
- -10, -26, -9, -11, -26, -16, -26, -18, -19, -20,
58
- -21, -22, -23, -24, -25, -12, -26, -15, -26, -13,
59
- -14, -17 ]
60
-
61
- racc_goto_table = [
62
- 25, 13, 36, 25, 11, 12, 1, 17, 9, 16,
63
- 23, nil, nil, nil, 22, 40, 25 ]
64
-
65
- racc_goto_check = [
66
- 3, 4, 7, 3, 2, 2, 1, 5, 1, 2,
67
- 6, nil, nil, nil, 4, 7, 3 ]
68
-
69
- racc_goto_pointer = [
70
- nil, 6, -1, -21, -6, -7, -11, -22 ]
71
-
72
- racc_goto_default = [
73
- nil, nil, 2, 3, nil, nil, 37, nil ]
74
-
75
- racc_reduce_table = [
76
- 0, 0, :racc_error,
77
- 2, 29, :_reduce_1,
78
- 1, 29, :_reduce_2,
79
- 1, 30, :_reduce_3,
80
- 4, 30, :_reduce_4,
81
- 2, 30, :_reduce_5,
82
- 2, 30, :_reduce_6,
83
- 1, 31, :_reduce_7,
84
- 2, 31, :_reduce_8,
85
- 4, 32, :_reduce_9,
86
- 3, 32, :_reduce_10,
87
- 3, 33, :_reduce_11,
88
- 4, 33, :_reduce_12,
89
- 5, 33, :_reduce_13,
90
- 2, 35, :_reduce_14,
91
- 1, 35, :_reduce_15,
92
- 1, 34, :_reduce_none,
93
- 3, 34, :_reduce_17,
94
- 1, 34, :_reduce_18,
95
- 1, 34, :_reduce_19,
96
- 1, 34, :_reduce_20,
97
- 1, 34, :_reduce_21,
98
- 1, 34, :_reduce_22,
99
- 1, 34, :_reduce_23,
100
- 1, 34, :_reduce_24,
101
- 1, 34, :_reduce_25 ]
102
-
103
- racc_reduce_n = 26
104
-
105
- racc_shift_n = 42
106
-
107
- racc_token_table = {
108
- false => 0,
109
- :error => 1,
110
- :tNODE_TYPE => 2,
111
- :tGOTO_SCOPE => 3,
112
- :tATTRIBUTE => 4,
113
- :tKEY => 5,
114
- :tIDENTIFIER => 6,
115
- :tIDENTIFIER_VALUE => 7,
116
- :tPSEUDO_CLASS => 8,
117
- :tRELATIONSHIP => 9,
118
- :tOPEN_ATTRIBUTE => 10,
119
- :tCLOSE_ATTRIBUTE => 11,
120
- :tOPEN_DYNAMIC_ATTRIBUTE => 12,
121
- :tCLOSE_DYNAMIC_ATTRIBUTE => 13,
122
- :tOPEN_ARRAY => 14,
123
- :tCLOSE_ARRAY => 15,
124
- :tOPEN_SELECTOR => 16,
125
- :tCLOSE_SELECTOR => 17,
126
- :tOPERATOR => 18,
127
- :tARRAY_VALUE => 19,
128
- :tDYNAMIC_ATTRIBUTE => 20,
129
- :tBOOLEAN => 21,
130
- :tFLOAT => 22,
131
- :tINTEGER => 23,
132
- :tNIL => 24,
133
- :tREGEXP => 25,
134
- :tSTRING => 26,
135
- :tSYMBOL => 27 }
136
-
137
- racc_nt_base = 28
138
-
139
- racc_use_result_var = false
140
-
141
- Racc_arg = [
142
- racc_action_table,
143
- racc_action_check,
144
- racc_action_default,
145
- racc_action_pointer,
146
- racc_goto_table,
147
- racc_goto_check,
148
- racc_goto_default,
149
- racc_goto_pointer,
150
- racc_nt_base,
151
- racc_reduce_table,
152
- racc_token_table,
153
- racc_shift_n,
154
- racc_reduce_n,
155
- racc_use_result_var ]
156
-
157
- Racc_token_to_s_table = [
158
- "$end",
159
- "error",
160
- "tNODE_TYPE",
161
- "tGOTO_SCOPE",
162
- "tATTRIBUTE",
163
- "tKEY",
164
- "tIDENTIFIER",
165
- "tIDENTIFIER_VALUE",
166
- "tPSEUDO_CLASS",
167
- "tRELATIONSHIP",
168
- "tOPEN_ATTRIBUTE",
169
- "tCLOSE_ATTRIBUTE",
170
- "tOPEN_DYNAMIC_ATTRIBUTE",
171
- "tCLOSE_DYNAMIC_ATTRIBUTE",
172
- "tOPEN_ARRAY",
173
- "tCLOSE_ARRAY",
174
- "tOPEN_SELECTOR",
175
- "tCLOSE_SELECTOR",
176
- "tOPERATOR",
177
- "tARRAY_VALUE",
178
- "tDYNAMIC_ATTRIBUTE",
179
- "tBOOLEAN",
180
- "tFLOAT",
181
- "tINTEGER",
182
- "tNIL",
183
- "tREGEXP",
184
- "tSTRING",
185
- "tSYMBOL",
186
- "$start",
187
- "expression",
188
- "selector",
189
- "basic_selector",
190
- "attribute_list",
191
- "attribute",
192
- "value",
193
- "array_value" ]
194
-
195
- Racc_debug_parser = false
196
-
197
- ##### State transition tables end #####
198
-
199
- # reduce 0 omitted
200
-
201
- def _reduce_1(val, _values)
202
- Compiler::Expression.new(selector: val[0], rest: val[1])
203
- end
204
-
205
- def _reduce_2(val, _values)
206
- Compiler::Expression.new(selector: val[0])
207
- end
208
-
209
- def _reduce_3(val, _values)
210
- Compiler::Selector.new(basic_selector: val[0])
211
- end
212
-
213
- def _reduce_4(val, _values)
214
- Compiler::Selector.new(pseudo_class: val[0], pseudo_selector: val[2])
215
- end
216
-
217
- def _reduce_5(val, _values)
218
- Compiler::Selector.new(relationship: val[0], rest: val[1])
219
- end
220
-
221
- def _reduce_6(val, _values)
222
- Compiler::Selector.new(goto_scope: val[0], rest: val[1])
223
- end
224
-
225
- def _reduce_7(val, _values)
226
- Compiler::BasicSelector.new(node_type: val[0])
227
- end
228
-
229
- def _reduce_8(val, _values)
230
- Compiler::BasicSelector.new(node_type: val[0], attribute_list: val[1])
231
- end
232
-
233
- def _reduce_9(val, _values)
234
- Compiler::AttributeList.new(attribute: val[1], rest: val[3])
235
- end
236
-
237
- def _reduce_10(val, _values)
238
- Compiler::AttributeList.new(attribute: val[1])
239
- end
240
-
241
- def _reduce_11(val, _values)
242
- Compiler::Attribute.new(key: val[0], value: val[2], operator: val[1])
243
- end
244
-
245
- def _reduce_12(val, _values)
246
- Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: val[1])
247
- end
248
-
249
- def _reduce_13(val, _values)
250
- Compiler::Attribute.new(key: val[0], value: val[3], operator: val[1])
251
- end
252
-
253
- def _reduce_14(val, _values)
254
- Compiler::Array.new(value: val[0], rest: val[1])
255
- end
256
-
257
- def _reduce_15(val, _values)
258
- Compiler::Array.new(value: val[0])
259
- end
260
-
261
- # reduce 16 omitted
262
-
263
- def _reduce_17(val, _values)
264
- Compiler::DynamicAttribute.new(value: val[1])
265
- end
266
-
267
- def _reduce_18(val, _values)
268
- Compiler::Boolean.new(value: val[0])
269
- end
270
-
271
- def _reduce_19(val, _values)
272
- Compiler::Float.new(value: val[0])
273
- end
274
-
275
- def _reduce_20(val, _values)
276
- Compiler::Integer.new(value: val[0])
277
- end
278
-
279
- def _reduce_21(val, _values)
280
- Compiler::Nil.new(value: val[0])
281
- end
282
-
283
- def _reduce_22(val, _values)
284
- Compiler::Regexp.new(value: val[0])
285
- end
286
-
287
- def _reduce_23(val, _values)
288
- Compiler::String.new(value: val[0])
289
- end
290
-
291
- def _reduce_24(val, _values)
292
- Compiler::Symbol.new(value: val[0])
293
- end
294
-
295
- def _reduce_25(val, _values)
296
- Compiler::Identifier.new(value: val[0])
297
- end
298
-
299
- def _reduce_none(val, _values)
300
- val[0]
301
- end
302
-
303
- end # class Parser
304
- end # module NodeQuery
305
- end # module Core
306
- end # module Synvert
@@ -1,60 +0,0 @@
1
- class Synvert::Core::NodeQuery::Parser
2
- options no_result_var
3
- token tNODE_TYPE tGOTO_SCOPE tATTRIBUTE tKEY tIDENTIFIER tIDENTIFIER_VALUE tPSEUDO_CLASS tRELATIONSHIP
4
- tOPEN_ATTRIBUTE tCLOSE_ATTRIBUTE tOPEN_DYNAMIC_ATTRIBUTE tCLOSE_DYNAMIC_ATTRIBUTE
5
- tOPEN_ARRAY tCLOSE_ARRAY tOPEN_SELECTOR tCLOSE_SELECTOR
6
- tOPERATOR tARRAY_VALUE tDYNAMIC_ATTRIBUTE tBOOLEAN tFLOAT tINTEGER tNIL tREGEXP tSTRING tSYMBOL
7
- rule
8
- expression
9
- : selector expression { Compiler::Expression.new(selector: val[0], rest: val[1]) }
10
- | selector { Compiler::Expression.new(selector: val[0]) }
11
-
12
- selector
13
- : basic_selector { Compiler::Selector.new(basic_selector: val[0]) }
14
- | tPSEUDO_CLASS tOPEN_SELECTOR selector tCLOSE_SELECTOR { Compiler::Selector.new(pseudo_class: val[0], pseudo_selector: val[2]) }
15
- | tRELATIONSHIP selector { Compiler::Selector.new(relationship: val[0], rest: val[1]) }
16
- | tGOTO_SCOPE selector { Compiler::Selector.new(goto_scope: val[0], rest: val[1]) }
17
-
18
- basic_selector
19
- : tNODE_TYPE { Compiler::BasicSelector.new(node_type: val[0]) }
20
- | tNODE_TYPE attribute_list { Compiler::BasicSelector.new(node_type: val[0], attribute_list: val[1]) }
21
-
22
- attribute_list
23
- : tOPEN_ATTRIBUTE attribute tCLOSE_ATTRIBUTE attribute_list { Compiler::AttributeList.new(attribute: val[1], rest: val[3]) }
24
- | tOPEN_ATTRIBUTE attribute tCLOSE_ATTRIBUTE { Compiler::AttributeList.new(attribute: val[1]) }
25
-
26
- attribute
27
- : tKEY tOPERATOR value { Compiler::Attribute.new(key: val[0], value: val[2], operator: val[1]) }
28
- | tKEY tOPERATOR tOPEN_ARRAY tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: val[1]) }
29
- | tKEY tOPERATOR tOPEN_ARRAY array_value tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: val[3], operator: val[1]) }
30
-
31
- array_value
32
- : value array_value { Compiler::Array.new(value: val[0], rest: val[1]) }
33
- | value { Compiler::Array.new(value: val[0]) }
34
-
35
- value
36
- : basic_selector
37
- | tOPEN_DYNAMIC_ATTRIBUTE tDYNAMIC_ATTRIBUTE tCLOSE_DYNAMIC_ATTRIBUTE { Compiler::DynamicAttribute.new(value: val[1]) }
38
- | tBOOLEAN { Compiler::Boolean.new(value: val[0]) }
39
- | tFLOAT { Compiler::Float.new(value: val[0]) }
40
- | tINTEGER { Compiler::Integer.new(value: val[0])}
41
- | tNIL { Compiler::Nil.new(value: val[0]) }
42
- | tREGEXP { Compiler::Regexp.new(value: val[0]) }
43
- | tSTRING { Compiler::String.new(value: val[0]) }
44
- | tSYMBOL { Compiler::Symbol.new(value: val[0]) }
45
- | tIDENTIFIER_VALUE { Compiler::Identifier.new(value: val[0]) }
46
- end
47
-
48
- ---- inner
49
- def initialize
50
- @lexer = Lexer.new
51
- end
52
-
53
- def parse string
54
- @lexer.parse string
55
- do_parse
56
- end
57
-
58
- def next_token
59
- @lexer.next_token
60
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # NodeQuery defines a node query language, which is a css like syntax for matching nodes.
4
- #
5
- # It supports the following selectors:
6
- #
7
- # * AST node type: +.class+, +.send+
8
- # * attribute value: +.send[receiver = nil]+, +.send[message = create]+
9
- # * attribute regex: <code>.send[key=~/\A:([^'"]+)\z/]</code>, <code>.send[key!~/\A:([^'"]+)\z/]</code>
10
- # * attribute conditions: +.send[message != nil]+, +.send[value > 1]+, +.send[value >= 1]+, +.send[value < 1]+, +.send[value <= 1]+
11
- # * nested attribute: +.send[caller.message = map]+, +.send[arguments.size = 2]+
12
- # * descendant: +.class .send+
13
- # * child: +.class > .def+
14
- # * following sibling: <code>.def + .def</code>
15
- # * subsequnt sibling: +.def ~ .def+
16
- # * has: +.class:has(.def)+
17
- #
18
- # It also supports some custom selectors:
19
- #
20
- # * not_has: +.class:not_has(.def)+, it's same as +:not(:has())+ in css, just to make implementation easy.
21
- # * nested selector: +.send[arguments = [size = 2][first = .sym][last = .hash]]+
22
- # * array value: +.send[arguments = (a b)]+
23
- # * IN operator: +.send[message IN (try try!)]+
24
- # * NOT IN operator: +.send[message NOT IN (create build)]+
25
- # * INCLUDES operator: +.send[arguments INCLUDES &block]+
26
- # * dynamic attribute value: +.hash > .pair[key={{value}}]+
27
- # * goto scope: +.class body > .def+
28
- #
29
- # @example
30
- # # it matches methods call nodes, like `puts message` or `p message`
31
- # Synvert::Core::NodeQuery::Parser.parse('.send[receiver = nil][message IN (puts, p)]').query_nodes(node)
32
- module Synvert::Core::NodeQuery
33
- autoload :Compiler, 'synvert/core/node_query/compiler'
34
- autoload :Lexer, 'synvert/core/node_query/lexer.rex'
35
- autoload :Parser, 'synvert/core/node_query/parser.racc'
36
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # AppendAction appends code to the bottom of node body.
5
- class Rewriter::AppendAction < Rewriter::Action
6
- private
7
-
8
- END_LENGTH = "\nend".length
9
-
10
- # Calculate the begin the end positions.
11
- def calculate_position
12
- @begin_pos = :begin == @node.type ? @node.loc.expression.end_pos : @node.loc.expression.end_pos - @node.column - END_LENGTH
13
- @end_pos = @begin_pos
14
- end
15
-
16
- # Indent of the node.
17
- #
18
- # @param node [Parser::AST::Node]
19
- # @return [String] n times whitesphace
20
- def indent(node)
21
- if %i[block class def defs].include? node.type
22
- ' ' * (node.column + DEFAULT_INDENT)
23
- else
24
- ' ' * node.column
25
- end
26
- end
27
- end
28
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # DeleteAction deletes child nodes.
5
- class Rewriter::DeleteAction < Rewriter::Action
6
- # Initialize a DeleteAction.
7
- #
8
- # @param instance [Synvert::Core::Rewriter::Instance]
9
- # @param selectors [Array<Symbol, String>] used to select child nodes
10
- # @option and_comma [Boolean] delete extra comma.
11
- def initialize(instance, *selectors, and_comma: false)
12
- super(instance, nil)
13
- @selectors = selectors
14
- @and_comma = and_comma
15
- end
16
-
17
- # The rewritten code, always empty string.
18
- def rewritten_code
19
- ''
20
- end
21
-
22
- private
23
-
24
- # Calculate the begin and end positions.
25
- def calculate_position
26
- @begin_pos = @selectors.map { |selector| @node.child_node_range(selector) }
27
- .compact.map(&:begin_pos).min
28
- @end_pos = @selectors.map { |selector| @node.child_node_range(selector) }
29
- .compact.map(&:end_pos).max
30
- squeeze_spaces
31
- remove_comma if @and_comma
32
- end
33
- end
34
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # InsertAction to add code to the node.
5
- class Rewriter::InsertAction < Rewriter::Action
6
- # Initialize an InsertAction.
7
- #
8
- # @param instance [Synvert::Core::Rewriter::Instance]
9
- # @param code [String] to be inserted
10
- # @param at [String] position to insert, beginning or end
11
- # @param to [<Symbol|String>] name of child node
12
- def initialize(instance, code, at: 'end', to: nil)
13
- super(instance, code)
14
- @at = at
15
- @to = to
16
- end
17
-
18
- # The rewritten source code.
19
- #
20
- # @return [String] rewritten code.
21
- def rewritten_code
22
- rewritten_source
23
- end
24
-
25
- private
26
-
27
- # Calculate the begin and end positions.
28
- def calculate_position
29
- node_range = @to ? @node.child_node_range(@to) : @node.loc.expression
30
- @begin_pos = @at == 'end' ? node_range.end_pos : node_range.begin_pos
31
- @end_pos = @begin_pos
32
- end
33
- end
34
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # InsertAfterAction to insert code next to the node.
5
- class Rewriter::InsertAfterAction < Rewriter::Action
6
- private
7
-
8
- # Calculate the begin and end positions.
9
- def calculate_position
10
- @begin_pos = @node.loc.expression.end_pos
11
- @end_pos = @begin_pos
12
- end
13
-
14
- # Indent of the node.
15
- #
16
- # @param node [Parser::AST::Node]
17
- # @return [String] n times whitesphace
18
- def indent(node)
19
- ' ' * node.column
20
- end
21
- end
22
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # PrependAction to prepend code to the top of node body.
5
- class Rewriter::PrependAction < Rewriter::Action
6
- private
7
-
8
- DO_LENGTH = ' do'.length
9
-
10
- # Calculate the begin and end positions.
11
- def calculate_position
12
- @begin_pos =
13
- case @node.type
14
- when :block
15
- if @node.children[1].children.empty?
16
- @node.children[0].loc.expression.end_pos + DO_LENGTH
17
- else
18
- @node.children[1].loc.expression.end_pos
19
- end
20
- when :class
21
- @node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
22
- when :def
23
- @node.children[1].empty? ? @node.loc.name.end_pos : @node.children[1].loc.expression.end_pos
24
- when :defs
25
- @node.children[2].empty? ? @node.loc.name.end_pos : @node.children[2].loc.expression.end_pos
26
- else
27
- @node.children.last.loc.expression.end_pos
28
- end
29
- @end_pos = @begin_pos
30
- end
31
-
32
- # Indent of the node.
33
- #
34
- # @param node [Parser::AST::Node]
35
- # @return [String] n times whitesphace
36
- def indent(node)
37
- if %i[block class def defs].include?(node.type)
38
- ' ' * (node.column + DEFAULT_INDENT)
39
- else
40
- ' ' * node.column
41
- end
42
- end
43
- end
44
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # RemoveAction to remove current node.
5
- class Rewriter::RemoveAction < Rewriter::Action
6
- # Initialize a RemoveAction.
7
- #
8
- # @param instance [Synvert::Core::Rewriter::RemoveAction]
9
- # @param options [Hash] options.
10
- # @option and_comma [Boolean] delete extra comma.
11
- def initialize(instance, and_comma: false)
12
- super(instance, nil)
13
- @and_comma = and_comma
14
- end
15
-
16
- # The rewritten code, always empty string.
17
- def rewritten_code
18
- ''
19
- end
20
-
21
- private
22
-
23
- # Calculate the begin the end positions.
24
- def calculate_position
25
- if take_whole_line?
26
- @begin_pos = start_index
27
- @end_pos = end_index
28
- squeeze_lines
29
- else
30
- @begin_pos = @node.loc.expression.begin_pos
31
- @end_pos = @node.loc.expression.end_pos
32
- squeeze_spaces
33
- remove_comma if @and_command
34
- end
35
- end
36
-
37
- # Check if the source code of current node takes the whole line.
38
- #
39
- # @return [Boolean]
40
- def take_whole_line?
41
- @node.to_source == file_source[start_index...end_index].strip
42
- end
43
-
44
- # Get the start position of the line
45
- def start_index
46
- index = file_source[0..@node.loc.expression.begin_pos].rindex("\n")
47
- index ? index + "\n".length : @node.loc.expression.begin_pos
48
- end
49
-
50
- # Get the end position of the line
51
- def end_index
52
- index = file_source[@node.loc.expression.end_pos..-1].index("\n")
53
- index ? @node.loc.expression.end_pos + index + "\n".length : @node.loc.expression.end_pos
54
- end
55
- end
56
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # ReplaceAction to replace child node with code.
5
- class Rewriter::ReplaceAction < Rewriter::Action
6
- # Initailize a ReplaceAction.
7
- #
8
- # @param instance [Synvert::Core::Rewriter::Instance]
9
- # @param selectors [Array<Symbol|String>] used to select child nodes
10
- # @param with [String] the new code
11
- def initialize(instance, *selectors, with:)
12
- super(instance, with)
13
- @selectors = selectors
14
- end
15
-
16
- # The rewritten source code.
17
- #
18
- # @return [String] rewritten code.
19
- def rewritten_code
20
- rewritten_source
21
- end
22
-
23
- private
24
-
25
- # Calculate the begin the end positions.
26
- def calculate_position
27
- @begin_pos = @selectors.map { |selector| @node.child_node_range(selector).begin_pos }
28
- .min
29
- @end_pos = @selectors.map { |selector| @node.child_node_range(selector).end_pos }
30
- .max
31
- end
32
- end
33
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # ReplaceWithAction to replace code.
5
- class Rewriter::ReplaceWithAction < Rewriter::Action
6
- # The rewritten source code with proper indent.
7
- #
8
- # @return [String] rewritten code.
9
- def rewritten_code
10
- if rewritten_source.include?("\n")
11
- new_code = []
12
- rewritten_source.split("\n").each_with_index do |line, index|
13
- new_code << (index == 0 ? line : indent + line)
14
- end
15
- new_code.join("\n")
16
- else
17
- rewritten_source
18
- end
19
- end
20
-
21
- private
22
-
23
- # Calculate the begin the end positions.
24
- def calculate_position
25
- @begin_pos = @node.loc.expression.begin_pos
26
- @end_pos = @node.loc.expression.end_pos
27
- end
28
-
29
- # Indent of the node
30
- #
31
- # @return [String] n times whitesphace
32
- def indent
33
- ' ' * @node.column
34
- end
35
- end
36
- end