synvert-core 1.3.0 → 1.5.0

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