synvert-core 0.55.0 → 0.57.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/lib/synvert/core/node_ext.rb +25 -4
  4. data/lib/synvert/core/rewriter/action/append_action.rb +3 -16
  5. data/lib/synvert/core/rewriter/action/delete_action.rb +5 -12
  6. data/lib/synvert/core/rewriter/action/insert_action.rb +3 -16
  7. data/lib/synvert/core/rewriter/action/insert_after_action.rb +3 -12
  8. data/lib/synvert/core/rewriter/action/prepend_action.rb +13 -21
  9. data/lib/synvert/core/rewriter/action/remove_action.rb +8 -18
  10. data/lib/synvert/core/rewriter/action/replace_action.rb +3 -12
  11. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +3 -11
  12. data/lib/synvert/core/rewriter/action/replace_with_action.rb +3 -12
  13. data/lib/synvert/core/rewriter/action/wrap_action.rb +3 -12
  14. data/lib/synvert/core/rewriter/action.rb +47 -0
  15. data/lib/synvert/core/rewriter/instance.rb +11 -11
  16. data/lib/synvert/core/rewriter/ruby_version.rb +10 -2
  17. data/lib/synvert/core/rewriter.rb +1 -1
  18. data/lib/synvert/core/version.rb +1 -1
  19. data/spec/synvert/core/node_ext_spec.rb +45 -0
  20. data/spec/synvert/core/rewriter/action/append_action_spec.rb +2 -2
  21. data/spec/synvert/core/rewriter/action/delete_action_spec.rb +2 -2
  22. data/spec/synvert/core/rewriter/action/insert_action_spec.rb +2 -2
  23. data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +1 -1
  24. data/spec/synvert/core/rewriter/action/prepend_action_spec.rb +4 -4
  25. data/spec/synvert/core/rewriter/action/remove_action_spec.rb +1 -1
  26. data/spec/synvert/core/rewriter/action/replace_action_spec.rb +1 -1
  27. data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +2 -2
  28. data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +1 -1
  29. data/spec/synvert/core/rewriter/instance_spec.rb +32 -12
  30. data/spec/synvert/core/rewriter/ruby_version_spec.rb +5 -0
  31. data/spec/synvert/core/rewriter_spec.rb +4 -2
  32. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0afc5cb58f822a56343dfba1c4f2c762e52db5d4eefb3d88eb1b72788a97a8b3
4
- data.tar.gz: 56c67dd89f69ba0dd90a91818740c0f70b18d3fe4887e50fed37c7eea736b18f
3
+ metadata.gz: d308267b7dc3f8561869a44ab25b6ffb329f4dfbc04ae66e960de167faa1ee4a
4
+ data.tar.gz: c6863adae12f49e7d8c0eb79c336f11b5dfaead8b7679a32c32a2131a0cd2ccf
5
5
  SHA512:
6
- metadata.gz: 6e9270a5518350d8bdab54a9548047cb678e72eeb5702be9857852522a796d70573ce958fca00f75df776b707bd021ea3f4c7b7720f69755f6b862c4fc70d32f
7
- data.tar.gz: 57a6ac61ee0724c2c6a10524192b4911d65c3cb977104720a1a2da6e5fcd2d967030aaddf8df1605037343620ef1ec9878943fc23935a2a656f7a0cc0672a173
6
+ metadata.gz: ce3db59d4d4d047c4abafbe3130252615bf601fc980c601d93552777105c646eb76174e04ae5e7af16004570f4d091c7549c40c5b5b0e803ab8eb916607ae34a
7
+ data.tar.gz: 2095af835a69d267a8d78112752aa553c002cb0699f3ca082b2ef0600e95b772eba9aae76cbd863ae3e8d7a7101c92942e03ae89e34d0c3abd6a636a5b65ac29
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.57.1 (2021-10-18)
4
+
5
+ * Support `left_value` and `right_value` for `and` and `or` node
6
+
7
+ ## 0.57.0 (2021-10-02)
8
+
9
+ * Compare ruby version in `.ruby-version` or `.rvmrc`
10
+ * Support `left_value` and `right_value` for `casgn` node
11
+ * Refactor - `calculate_position` to set `begin_pos` and `end_pos`
12
+ * Abstract `squeeze_spaces` and `squeeze_lines`
13
+ * Remove unused comma after delete/remove action
14
+ * Handle array child node in `childNodeRange`
15
+
16
+ ## 0.56.0 (2021-09-14)
17
+
18
+ * Support `name` for `:lvar`, `:ivar`, `:cvar`
19
+ * Delete one more space if two spaces left
20
+
3
21
  ## 0.55.0 (2021-09-11)
4
22
 
5
23
  * Add `Configuration.show_run_process`
@@ -9,7 +9,7 @@ module Parser::AST
9
9
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
10
10
  def name
11
11
  case type
12
- when :class, :module, :def, :arg, :blockarg, :restarg
12
+ when :class, :module, :def, :arg, :blockarg, :restarg, :lvar, :ivar, :cvar
13
13
  children[0]
14
14
  when :defs, :const
15
15
  children[1]
@@ -215,7 +215,7 @@ module Parser::AST
215
215
  # @return [Parser::AST::Node] variable nodes.
216
216
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
217
217
  def left_value
218
- if %i[masgn lvasgn ivasgn].include? type
218
+ if %i[masgn lvasgn ivasgn cvasgn and or].include? type
219
219
  children[0]
220
220
  elsif :or_asgn == type
221
221
  children[0].children[0]
@@ -229,7 +229,7 @@ module Parser::AST
229
229
  # @return [Array<Parser::AST::Node>] variable nodes.
230
230
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
231
231
  def right_value
232
- if %i[masgn lvasgn ivasgn or_asgn].include? type
232
+ if %i[masgn lvasgn ivasgn cvasgn or_asgn and or].include? type
233
233
  children[1]
234
234
  else
235
235
  raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}"
@@ -373,7 +373,28 @@ module Parser::AST
373
373
  if respond_to?(direct_child_name)
374
374
  child_node = send(direct_child_name)
375
375
 
376
- return child_node.child_node_range(nested_child_name) if nested_child_name
376
+ if nested_child_name
377
+ if child_node.is_a?(Array)
378
+ child_direct_child_name, *child_nested_child_name = nested_child_name
379
+ child_direct_child_node = child_direct_child_name =~ /\A\d+\z/ ? child_node[child_direct_child_name] : child_node.send(child_direct_child_name)
380
+ if child_nested_child_name.length > 0
381
+ return child_direct_child_node.child_node_range(child_nested_child_name.join('.'))
382
+ elsif child_direct_child_node
383
+ return (
384
+ Parser::Source::Range.new(
385
+ '(string)',
386
+ child_direct_child_node.loc.expression.begin_pos,
387
+ child_direct_child_node.loc.expression.end_pos
388
+ )
389
+ )
390
+ else
391
+ raise Synvert::Core::MethodNotSupported,
392
+ "child_node_range is not handled for #{debug_info}, child_name: #{child_name}"
393
+ end
394
+ end
395
+
396
+ return child_node.child_node_range(nested_child_name)
397
+ end
377
398
 
378
399
  return nil if child_node.nil?
379
400
 
@@ -5,22 +5,9 @@ module Synvert::Core
5
5
  class Rewriter::AppendAction < Rewriter::Action
6
6
  END_LENGTH = "\nend".length
7
7
 
8
- # Begin position to append code.
9
- #
10
- # @return [Integer] begin position.
11
- def begin_pos
12
- if :begin == @node.type
13
- @node.loc.expression.end_pos
14
- else
15
- @node.loc.expression.end_pos - @node.column - END_LENGTH
16
- end
17
- end
18
-
19
- # End position, always same to begin position.
20
- #
21
- # @return [Integer] end position.
22
- def end_pos
23
- begin_pos
8
+ def calculate_position
9
+ @begin_pos = :begin == @node.type ? @node.loc.expression.end_pos : @node.loc.expression.end_pos - @node.column - END_LENGTH
10
+ @end_pos = @begin_pos
24
11
  end
25
12
 
26
13
  private
@@ -8,18 +8,11 @@ module Synvert::Core
8
8
  @selectors = selectors
9
9
  end
10
10
 
11
- # Begin position of code to delete.
12
- #
13
- # @return [Integer] begin position.
14
- def begin_pos
15
- @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
16
- end
17
-
18
- # End position of code to delete.
19
- #
20
- # @return [Integer] end position.
21
- def end_pos
22
- @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
11
+ def calculate_position
12
+ @begin_pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
13
+ @end_pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
14
+ squeeze_spaces
15
+ remove_comma
23
16
  end
24
17
 
25
18
  # The rewritten code, always empty string.
@@ -8,22 +8,9 @@ module Synvert::Core
8
8
  @at = at
9
9
  end
10
10
 
11
- # Begin position to insert code.
12
- #
13
- # @return [Integer] begin position.
14
- def begin_pos
15
- if @at == 'end'
16
- @node.loc.expression.end_pos
17
- else
18
- @node.loc.expression.begin_pos
19
- end
20
- end
21
-
22
- # End position, always same to begin position.
23
- #
24
- # @return [Integer] end position.
25
- def end_pos
26
- begin_pos
11
+ def calculate_position
12
+ @begin_pos = @at == 'end' ? @node.loc.expression.end_pos : @node.loc.expression.begin_pos
13
+ @end_pos = @begin_pos
27
14
  end
28
15
 
29
16
  # The rewritten source code.
@@ -3,18 +3,9 @@
3
3
  module Synvert::Core
4
4
  # InsertAfterAction to insert code next to the node.
5
5
  class Rewriter::InsertAfterAction < Rewriter::Action
6
- # Begin position to insert code.
7
- #
8
- # @return [Integer] begin position.
9
- def begin_pos
10
- @node.loc.expression.end_pos
11
- end
12
-
13
- # End position, always same to begin position.
14
- #
15
- # @return [Integer] end position.
16
- def end_pos
17
- begin_pos
6
+ def calculate_position
7
+ @begin_pos = @node.loc.expression.end_pos
8
+ @end_pos = @begin_pos
18
9
  end
19
10
 
20
11
  private
@@ -5,29 +5,21 @@ module Synvert::Core
5
5
  class Rewriter::PrependAction < Rewriter::Action
6
6
  DO_LENGTH = ' do'.length
7
7
 
8
- # Begin position to prepend code.
9
- #
10
- # @return [Integer] begin position.
11
- def begin_pos
12
- case @node.type
13
- when :block
14
- if @node.children[1].children.empty?
15
- @node.children[0].loc.expression.end_pos + DO_LENGTH
8
+ def calculate_position
9
+ @begin_pos =
10
+ case @node.type
11
+ when :block
12
+ if @node.children[1].children.empty?
13
+ @node.children[0].loc.expression.end_pos + DO_LENGTH
14
+ else
15
+ @node.children[1].loc.expression.end_pos
16
+ end
17
+ when :class
18
+ @node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
16
19
  else
17
- @node.children[1].loc.expression.end_pos
20
+ @node.children.last.loc.expression.end_pos
18
21
  end
19
- when :class
20
- @node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
21
- else
22
- @node.children.last.loc.expression.end_pos
23
- end
24
- end
25
-
26
- # End position, always same to begin position.
27
- #
28
- # @return [Integer] end position.
29
- def end_pos
30
- begin_pos
22
+ @end_pos = @begin_pos
31
23
  end
32
24
 
33
25
  private
@@ -10,22 +10,16 @@ module Synvert::Core
10
10
  # Begin position of code to replace.
11
11
  #
12
12
  # @return [Integer] begin position.
13
- def begin_pos
13
+ def calculate_position
14
14
  if take_whole_line?
15
- start_index
15
+ @begin_pos = start_index
16
+ @end_pos = end_index
17
+ squeeze_lines
16
18
  else
17
- @node.loc.expression.begin_pos
18
- end
19
- end
20
-
21
- # End position of code to replace.
22
- #
23
- # @return [Integer] end position.
24
- def end_pos
25
- if take_whole_line?
26
- end_index
27
- else
28
- @node.loc.expression.end_pos
19
+ @begin_pos = @node.loc.expression.begin_pos
20
+ @end_pos = @node.loc.expression.end_pos
21
+ squeeze_spaces
22
+ remove_comma
29
23
  end
30
24
  end
31
25
 
@@ -49,9 +43,5 @@ module Synvert::Core
49
43
  index = file_source[@node.loc.expression.end_pos..-1].index("\n")
50
44
  index ? @node.loc.expression.end_pos + index + "\n".length : @node.loc.expression.end_pos
51
45
  end
52
-
53
- def file_source
54
- @file_source ||= @instance.file_source
55
- end
56
46
  end
57
47
  end
@@ -8,18 +8,9 @@ module Synvert::Core
8
8
  @selectors = selectors
9
9
  end
10
10
 
11
- # Begin position of code to replace.
12
- #
13
- # @return [Integer] begin position.
14
- def begin_pos
15
- @selectors.map { |selector| @node.child_node_range(selector).begin_pos }.min
16
- end
17
-
18
- # End position of code to replace.
19
- #
20
- # @return [Integer] end position.
21
- def end_pos
22
- @selectors.map { |selector| @node.child_node_range(selector).end_pos }.max
11
+ def calculate_position
12
+ @begin_pos = @selectors.map { |selector| @node.child_node_range(selector).begin_pos }.min
13
+ @end_pos = @selectors.map { |selector| @node.child_node_range(selector).end_pos }.max
23
14
  end
24
15
 
25
16
  # The rewritten source code.
@@ -8,25 +8,17 @@ module Synvert::Core
8
8
  super
9
9
  end
10
10
 
11
- # Begin position of code to replace.
12
- #
13
- # @return [Integer] begin position.
14
- def begin_pos
11
+ def calculate_position
15
12
  node_begin_pos = @node.loc.expression.begin_pos
16
13
  while @node.loc.expression.source_buffer.source[node_begin_pos -= 1] == ' '
17
14
  end
18
- node_begin_pos - Engine::ERUBY_STMT_SPLITTER.length + 1
19
- end
15
+ @begin_pos = node_begin_pos - Engine::ERUBY_STMT_SPLITTER.length + 1
20
16
 
21
- # End position of code to replace.
22
- #
23
- # @return [Integer] end position.
24
- def end_pos
25
17
  node_begin_pos = @node.loc.expression.begin_pos
26
18
  node_begin_pos += @node.loc.expression.source.index 'do'
27
19
  while @node.loc.expression.source_buffer.source[node_begin_pos += 1] != '@'
28
20
  end
29
- node_begin_pos
21
+ @end_pos = node_begin_pos
30
22
  end
31
23
 
32
24
  # The rewritten erb expr code.
@@ -3,18 +3,9 @@
3
3
  module Synvert::Core
4
4
  # ReplaceWithAction to replace code.
5
5
  class Rewriter::ReplaceWithAction < Rewriter::Action
6
- # Begin position of code to replace.
7
- #
8
- # @return [Integer] begin position.
9
- def begin_pos
10
- @node.loc.expression.begin_pos
11
- end
12
-
13
- # End position of code to replace.
14
- #
15
- # @return [Integer] end position.
16
- def end_pos
17
- @node.loc.expression.end_pos
6
+ def calculate_position
7
+ @begin_pos = @node.loc.expression.begin_pos
8
+ @end_pos = @node.loc.expression.end_pos
18
9
  end
19
10
 
20
11
  # The rewritten source code with proper indent.
@@ -11,18 +11,9 @@ module Synvert::Core
11
11
  @indent = indent || @node.column
12
12
  end
13
13
 
14
- # Begin position of code to wrap.
15
- #
16
- # @return [Integer] begin position.
17
- def begin_pos
18
- @node.loc.expression.begin_pos
19
- end
20
-
21
- # End position of code to wrap.
22
- #
23
- # @return [Integer] end position.
24
- def end_pos
25
- @node.loc.expression.end_pos
14
+ def calculate_position
15
+ @begin_pos = @node.loc.expression.begin_pos
16
+ @end_pos = @node.loc.expression.end_pos
26
17
  end
27
18
 
28
19
  # The rewritten source code.
@@ -5,6 +5,12 @@ module Synvert::Core
5
5
  class Rewriter::Action
6
6
  DEFAULT_INDENT = 2
7
7
 
8
+ # @!attribute [r] begin_pos
9
+ # @return [Integer] begin position
10
+ # @!attribute [r] end_pos
11
+ # @return [Integer] end position
12
+ attr_reader :begin_pos, :end_pos
13
+
8
14
  # Initialize an action.
9
15
  #
10
16
  # @param instance [Synvert::Core::Rewriter::Instance]
@@ -15,6 +21,11 @@ module Synvert::Core
15
21
  @node = @instance.current_node
16
22
  end
17
23
 
24
+ def process
25
+ calculate_position
26
+ self
27
+ end
28
+
18
29
  # Line number of current node.
19
30
  #
20
31
  # @return [Integer] line number.
@@ -33,11 +44,47 @@ module Synvert::Core
33
44
  end
34
45
  end
35
46
 
47
+ protected
48
+
36
49
  # The rewritten source code.
37
50
  #
38
51
  # @return [String] rewritten source code.
39
52
  def rewritten_source
40
53
  @rewritten_source ||= @node.rewritten_source(@code)
41
54
  end
55
+
56
+ def squeeze_spaces
57
+ if file_source[@begin_pos - 1] == ' ' && file_source[@end_pos] == ' '
58
+ @begin_pos = @begin_pos - 1
59
+ end
60
+ end
61
+
62
+ def squeeze_lines
63
+ lines = file_source.split("\n")
64
+ begin_line = @node.loc.expression.first_line
65
+ end_line = @node.loc.expression.last_line
66
+ before_line_is_blank = begin_line == 1 || lines[begin_line - 2] == ''
67
+ after_line_is_blank = lines[end_line] == ''
68
+
69
+ if lines.length > 1 && before_line_is_blank && after_line_is_blank
70
+ @end_pos = @end_pos + "\n".length
71
+ end
72
+ end
73
+
74
+ def remove_comma
75
+ if ',' == file_source[@begin_pos - 1]
76
+ @begin_pos = @begin_pos - 1
77
+ elsif ', ' == file_source[@begin_pos - 2, 2]
78
+ @begin_pos = @begin_pos - 2
79
+ elsif ', ' == file_source[@end_pos, 2]
80
+ @end_pos = @end_pos + 2
81
+ elsif ',' == file_source[@end_pos]
82
+ @end_pos = @end_pos + 1
83
+ end
84
+ end
85
+
86
+ def file_source
87
+ @file_source ||= @instance.file_source
88
+ end
42
89
  end
43
90
  end
@@ -46,7 +46,7 @@ module Synvert::Core
46
46
  # @param source [String] file source
47
47
  def write_file(file_path, source)
48
48
  source = Engine::ERB.decode(source) if /\.erb/.match?(file_path)
49
- File.write file_path, source.gsub(/ +\n/, "\n")
49
+ File.write(file_path, source.gsub(/ +\n/, "\n"))
50
50
  @file_source[file_path] = nil
51
51
  @file_ast[file_path] = nil
52
52
  end
@@ -221,7 +221,7 @@ module Synvert::Core
221
221
  #
222
222
  # @param code [String] code need to be appended.
223
223
  def append(code)
224
- @actions << Rewriter::AppendAction.new(self, code)
224
+ @actions << Rewriter::AppendAction.new(self, code).process
225
225
  end
226
226
 
227
227
  # Parse prepend dsl, it creates a [Synvert::Core::Rewriter::PrependAction] to
@@ -229,7 +229,7 @@ module Synvert::Core
229
229
  #
230
230
  # @param code [String] code need to be prepended.
231
231
  def prepend(code)
232
- @actions << Rewriter::PrependAction.new(self, code)
232
+ @actions << Rewriter::PrependAction.new(self, code).process
233
233
  end
234
234
 
235
235
  # Parse insert dsl, it creates a [Synvert::Core::Rewriter::InsertAction] to
@@ -238,7 +238,7 @@ module Synvert::Core
238
238
  # @param code [String] code need to be inserted.
239
239
  # @param at [String] insert position, beginning or end, end is the default.
240
240
  def insert(code, at: 'end')
241
- @actions << Rewriter::InsertAction.new(self, code, at: at)
241
+ @actions << Rewriter::InsertAction.new(self, code, at: at).process
242
242
  end
243
243
 
244
244
  # Parse insert_after dsl, it creates a [Synvert::Core::Rewriter::InsertAfterAction] to
@@ -246,7 +246,7 @@ module Synvert::Core
246
246
  #
247
247
  # @param code [String] code need to be inserted.
248
248
  def insert_after(node)
249
- @actions << Rewriter::InsertAfterAction.new(self, node)
249
+ @actions << Rewriter::InsertAfterAction.new(self, node).process
250
250
  end
251
251
 
252
252
  # Parse replace_with dsl, it creates a [Synvert::Core::Rewriter::ReplaceWithAction] to
@@ -254,7 +254,7 @@ module Synvert::Core
254
254
  #
255
255
  # @param code [String] code need to be replaced with.
256
256
  def replace_with(code)
257
- @actions << Rewriter::ReplaceWithAction.new(self, code)
257
+ @actions << Rewriter::ReplaceWithAction.new(self, code).process
258
258
  end
259
259
 
260
260
  # Parse replace with dsl, it creates a [Synvert::Core::Rewriter::ReplaceAction] to
@@ -263,25 +263,25 @@ module Synvert::Core
263
263
  # @param selectors [Array<Symbol>] selector names of child node.
264
264
  # @param with [String] code need to be replaced with.
265
265
  def replace(*selectors, with:)
266
- @actions << Rewriter::ReplaceAction.new(self, *selectors, with: with)
266
+ @actions << Rewriter::ReplaceAction.new(self, *selectors, with: with).process
267
267
  end
268
268
 
269
269
  # Parse replace_erb_stmt_with_expr dsl, it creates a [Synvert::Core::Rewriter::ReplaceErbStmtWithExprAction] to
270
270
  # replace erb stmt code to expr code.
271
271
  def replace_erb_stmt_with_expr
272
- @actions << Rewriter::ReplaceErbStmtWithExprAction.new(self)
272
+ @actions << Rewriter::ReplaceErbStmtWithExprAction.new(self).process
273
273
  end
274
274
 
275
275
  # Parse remove dsl, it creates a [Synvert::Core::Rewriter::RemoveAction] to remove current node.
276
276
  def remove
277
- @actions << Rewriter::RemoveAction.new(self)
277
+ @actions << Rewriter::RemoveAction.new(self).process
278
278
  end
279
279
 
280
280
  # Parse delete dsl, it creates a [Synvert::Core::Rewriter::DeleteAction] to delete child nodes.
281
281
  #
282
282
  # @param selectors [Array<Symbol>] selector names of child node.
283
283
  def delete(*selectors)
284
- @actions << Rewriter::DeleteAction.new(self, *selectors)
284
+ @actions << Rewriter::DeleteAction.new(self, *selectors).process
285
285
  end
286
286
 
287
287
  # Parse wrap with dsl, it creates a [Synvert::Core::Rewriter::WrapAction] to
@@ -290,7 +290,7 @@ module Synvert::Core
290
290
  # @param with [String] code need to be wrapped with.
291
291
  # @param indent [Integer] number of whitespaces.
292
292
  def wrap(with:, indent: nil)
293
- @actions << Rewriter::WrapAction.new(self, with: with, indent: indent)
293
+ @actions << Rewriter::WrapAction.new(self, with: with, indent: indent).process
294
294
  end
295
295
 
296
296
  # Parse warn dsl, it creates a [Synvert::Core::Rewriter::Warning] to save warning message.
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
- # GemSpec checks and compares gem version.
4
+ # GemSpec checks and compares ruby version.
5
5
  class Rewriter::RubyVersion
6
6
  attr_reader :version
7
7
 
@@ -16,7 +16,15 @@ module Synvert::Core
16
16
  #
17
17
  # @return [Boolean] true if matches, otherwise false.
18
18
  def match?
19
- Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(@version)
19
+ if File.exist?(File.join(Configuration.path, '.ruby-version'))
20
+ versionFile = '.ruby-version'
21
+ elsif File.exist?(File.join(Configuration.path, '.rvmrc'))
22
+ versionFile = '.rvmrc'
23
+ end
24
+ return true if !versionFile
25
+
26
+ version = File.read(File.join(Configuration.path, versionFile))
27
+ Gem::Version.new(version) >= Gem::Version.new(@version)
20
28
  end
21
29
  end
22
30
  end
@@ -213,7 +213,7 @@ module Synvert::Core
213
213
  end
214
214
  end
215
215
 
216
- # Parse if_ruby dal, it checks if ruby version if greater than or equal to the specified ruby version.
216
+ # Parse if_ruby dsl, it checks if ruby version if greater than or equal to the specified ruby version.
217
217
  #
218
218
  # @param version, [String] specified ruby version.
219
219
  def if_ruby(version)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.55.0'
5
+ VERSION = '0.57.1'
6
6
  end
7
7
  end
@@ -42,6 +42,21 @@ describe Parser::AST::Node do
42
42
  expect(node.name).to eq :Synvert
43
43
  end
44
44
 
45
+ it 'gets for lvar node' do
46
+ node = parse("foo = 'bar'\nfoo").children[1]
47
+ expect(node.name).to eq :foo
48
+ end
49
+
50
+ it 'gets for ivar node' do
51
+ node = parse("@foo = 'bar'\n@foo").children[1]
52
+ expect(node.name).to eq :@foo
53
+ end
54
+
55
+ it 'gets for cvar node' do
56
+ node = parse("@@foo = 'bar'\n@@foo").children[1]
57
+ expect(node.name).to eq :@@foo
58
+ end
59
+
45
60
  it 'gets for mlhs node' do
46
61
  node = parse('var.each { |(param1, param2)| }')
47
62
  expect(node.arguments.first.name).to eq node.arguments.first
@@ -267,10 +282,25 @@ describe Parser::AST::Node do
267
282
  expect(node.left_value).to eq :@a
268
283
  end
269
284
 
285
+ it 'gets for cvasgn' do
286
+ node = parse('@@a = 1')
287
+ expect(node.left_value).to eq :@@a
288
+ end
289
+
270
290
  it 'gets for or_asgn' do
271
291
  node = parse('a ||= 1')
272
292
  expect(node.left_value).to eq :a
273
293
  end
294
+
295
+ it 'gets for and' do
296
+ node = parse('foo && bar')
297
+ expect(node.left_value).to eq parse('foo')
298
+ end
299
+
300
+ it 'gets for or' do
301
+ node = parse('foo || bar')
302
+ expect(node.left_value).to eq parse('foo')
303
+ end
274
304
  end
275
305
 
276
306
  describe '#right_value' do
@@ -294,10 +324,25 @@ describe Parser::AST::Node do
294
324
  expect(node.right_value).to eq parse('1')
295
325
  end
296
326
 
327
+ it 'gets for cvasgn' do
328
+ node = parse('@@a = 1')
329
+ expect(node.right_value).to eq parse('1')
330
+ end
331
+
297
332
  it 'gets for or_asgn' do
298
333
  node = parse('a ||= 1')
299
334
  expect(node.right_value).to eq parse('1')
300
335
  end
336
+
337
+ it 'gets for and' do
338
+ node = parse('foo && bar')
339
+ expect(node.right_value).to eq parse('bar')
340
+ end
341
+
342
+ it 'gets for or' do
343
+ node = parse('foo || bar')
344
+ expect(node.right_value).to eq parse('bar')
345
+ end
301
346
  end
302
347
 
303
348
  describe '#to_value' do
@@ -9,7 +9,7 @@ module Synvert::Core
9
9
  source = "class User\n has_many :posts\nend"
10
10
  class_node = Parser::CurrentRuby.parse(source)
11
11
  instance = double(current_node: class_node)
12
- Rewriter::AppendAction.new(instance, "def as_json\n super\nend")
12
+ Rewriter::AppendAction.new(instance, "def as_json\n super\nend").process
13
13
  end
14
14
 
15
15
  it 'gets begin_pos' do
@@ -30,7 +30,7 @@ module Synvert::Core
30
30
  source = "gem 'rails'\ngem 'mysql2'"
31
31
  begin_node = Parser::CurrentRuby.parse(source)
32
32
  instance = double(current_node: begin_node)
33
- Rewriter::AppendAction.new(instance, "gem 'twitter'")
33
+ Rewriter::AppendAction.new(instance, "gem 'twitter'").process
34
34
  end
35
35
 
36
36
  it 'gets begin_pos' do
@@ -7,8 +7,8 @@ module Synvert::Core
7
7
  subject {
8
8
  source = 'arr.map {}.flatten'
9
9
  node = Parser::CurrentRuby.parse(source)
10
- instance = double(current_node: node)
11
- Rewriter::DeleteAction.new(instance, :dot, :message)
10
+ instance = double(current_node: node, file_source: source)
11
+ Rewriter::DeleteAction.new(instance, :dot, :message).process
12
12
  }
13
13
 
14
14
  it 'gets begin_pos' do
@@ -9,7 +9,7 @@ module Synvert::Core
9
9
  source = " User.where(username: 'Richard')"
10
10
  node = Parser::CurrentRuby.parse(source)
11
11
  instance = double(current_node: node)
12
- Rewriter::InsertAction.new(instance, '.first', at: 'end')
12
+ Rewriter::InsertAction.new(instance, '.first', at: 'end').process
13
13
  }
14
14
 
15
15
  it 'gets begin_pos' do
@@ -30,7 +30,7 @@ module Synvert::Core
30
30
  source = " open('http://test.com')"
31
31
  node = Parser::CurrentRuby.parse(source)
32
32
  instance = double(current_node: node)
33
- Rewriter::InsertAction.new(instance, 'URI.', at: 'beginning')
33
+ Rewriter::InsertAction.new(instance, 'URI.', at: 'beginning').process
34
34
  }
35
35
 
36
36
  it 'gets begin_pos' do
@@ -8,7 +8,7 @@ module Synvert::Core
8
8
  source = ' include Foo'
9
9
  node = Parser::CurrentRuby.parse(source)
10
10
  instance = double(current_node: node)
11
- Rewriter::InsertAfterAction.new(instance, 'include Bar')
11
+ Rewriter::InsertAfterAction.new(instance, 'include Bar').process
12
12
  }
13
13
 
14
14
  it 'gets begin_pos' do
@@ -9,7 +9,7 @@ module Synvert::Core
9
9
  source = "Synvert::Application.configure do\nend"
10
10
  block_node = Parser::CurrentRuby.parse(source)
11
11
  instance = double(current_node: block_node)
12
- Rewriter::PrependAction.new(instance, 'config.eager_load = true')
12
+ Rewriter::PrependAction.new(instance, 'config.eager_load = true').process
13
13
  }
14
14
 
15
15
  it 'gets begin_pos' do
@@ -30,7 +30,7 @@ module Synvert::Core
30
30
  source = "RSpec.configure do |config|\nend"
31
31
  block_node = Parser::CurrentRuby.parse(source)
32
32
  instance = double(current_node: block_node)
33
- Rewriter::PrependAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
33
+ Rewriter::PrependAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods').process
34
34
  }
35
35
 
36
36
  it 'gets begin_pos' do
@@ -51,7 +51,7 @@ module Synvert::Core
51
51
  source = "class User\n has_many :posts\nend"
52
52
  class_node = Parser::CurrentRuby.parse(source)
53
53
  instance = double(current_node: class_node)
54
- Rewriter::PrependAction.new(instance, 'include Deletable')
54
+ Rewriter::PrependAction.new(instance, 'include Deletable').process
55
55
  }
56
56
 
57
57
  it 'gets begin_pos' do
@@ -72,7 +72,7 @@ module Synvert::Core
72
72
  source = "class User < ActiveRecord::Base\n has_many :posts\nend"
73
73
  class_node = Parser::CurrentRuby.parse(source)
74
74
  instance = double(current_node: class_node)
75
- Rewriter::PrependAction.new(instance, 'include Deletable')
75
+ Rewriter::PrependAction.new(instance, 'include Deletable').process
76
76
  }
77
77
 
78
78
  it 'gets begin_pos' do
@@ -8,7 +8,7 @@ module Synvert::Core
8
8
  source = "user = User.new params[:user]\nuser.save\nrender\n"
9
9
  send_node = Parser::CurrentRuby.parse(source).children[1]
10
10
  instance = double(current_node: send_node, file_source: source)
11
- Rewriter::RemoveAction.new(instance)
11
+ Rewriter::RemoveAction.new(instance).process
12
12
  }
13
13
 
14
14
  it 'gets begin_pos' do
@@ -9,7 +9,7 @@ module Synvert::Core
9
9
  source = 'FactoryBot.create(:user)'
10
10
  node = Parser::CurrentRuby.parse(source)
11
11
  instance = double(current_node: node)
12
- Rewriter::ReplaceAction.new(instance, :receiver, :dot, :message, with: 'create')
12
+ Rewriter::ReplaceAction.new(instance, :receiver, :dot, :message, with: 'create').process
13
13
  }
14
14
 
15
15
  it 'gets begin_pos' do
@@ -9,7 +9,7 @@ module Synvert::Core
9
9
  source = 'post = FactoryGirl.create_list :post, 2'
10
10
  send_node = Parser::CurrentRuby.parse(source).children[1]
11
11
  instance = double(current_node: send_node)
12
- Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}')
12
+ Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}').process
13
13
  }
14
14
 
15
15
  it 'gets begin_pos' do
@@ -30,7 +30,7 @@ module Synvert::Core
30
30
  source = ' its(:size) { should == 1 }'
31
31
  send_node = Parser::CurrentRuby.parse(source)
32
32
  instance = double(current_node: send_node)
33
- Rewriter::ReplaceWithAction.new(instance, <<~EOS)
33
+ Rewriter::ReplaceWithAction.new(instance, <<~EOS).process
34
34
  describe '#size' do
35
35
  subject { super().size }
36
36
  it { {{body}} }
@@ -8,7 +8,7 @@ module Synvert::Core
8
8
  source = "class Bar\nend"
9
9
  node = Parser::CurrentRuby.parse(source)
10
10
  instance = double(current_node: node)
11
- Rewriter::WrapAction.new(instance, with: 'module Foo')
11
+ Rewriter::WrapAction.new(instance, with: 'module Foo').process
12
12
  }
13
13
 
14
14
  it 'gets begin_pos' do
@@ -90,58 +90,78 @@ module Synvert::Core
90
90
  end
91
91
 
92
92
  it 'parses append' do
93
- expect(Rewriter::AppendAction).to receive(:new).with(instance, 'include FactoryGirl::Syntax::Methods')
93
+ action = double
94
+ expect(Rewriter::AppendAction).to receive(:new).with(instance, 'include FactoryGirl::Syntax::Methods').and_return(action)
95
+ expect(action).to receive(:process)
94
96
  instance.append 'include FactoryGirl::Syntax::Methods'
95
97
  end
96
98
 
97
99
  it 'parses prepend' do
100
+ action = double
98
101
  expect(Rewriter::PrependAction).to receive(:new).with(
99
102
  instance,
100
103
  '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
101
- )
104
+ ).and_return(action)
105
+ expect(action).to receive(:process)
102
106
  instance.prepend '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
103
107
  end
104
108
 
105
- it 'parses insert' do
106
- expect(Rewriter::InsertAction).to receive(:new).with(instance, '.first', at: 'end')
109
+ it 'parses insert at end' do
110
+ action = double
111
+ expect(Rewriter::InsertAction).to receive(:new).with(instance, '.first', at: 'end').and_return(action)
112
+ expect(action).to receive(:process)
107
113
  instance.insert '.first'
108
114
  end
109
115
 
110
- it 'parses insert' do
111
- expect(Rewriter::InsertAction).to receive(:new).with(instance, 'URI.', at: 'beginning')
116
+ it 'parses insert at beginning' do
117
+ action = double
118
+ expect(Rewriter::InsertAction).to receive(:new).with(instance, 'URI.', at: 'beginning').and_return(action)
119
+ expect(action).to receive(:process)
112
120
  instance.insert 'URI.', at: 'beginning'
113
121
  end
114
122
 
115
123
  it 'parses insert_after' do
124
+ action = double
116
125
  expect(Rewriter::InsertAfterAction).to receive(:new).with(
117
126
  instance,
118
127
  '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
119
- )
128
+ ).and_return(action)
129
+ expect(action).to receive(:process)
120
130
  instance.insert_after '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
121
131
  end
122
132
 
123
133
  it 'parses replace_with' do
124
- expect(Rewriter::ReplaceWithAction).to receive(:new).with(instance, 'create {{arguments}}')
134
+ action = double
135
+ expect(Rewriter::ReplaceWithAction).to receive(:new).with(instance, 'create {{arguments}}').and_return(action)
136
+ expect(action).to receive(:process)
125
137
  instance.replace_with 'create {{arguments}}'
126
138
  end
127
139
 
128
140
  it 'parses replace with' do
129
- expect(Rewriter::ReplaceAction).to receive(:new).with(instance, :message, with: 'test')
141
+ action = double
142
+ expect(Rewriter::ReplaceAction).to receive(:new).with(instance, :message, with: 'test').and_return(action)
143
+ expect(action).to receive(:process)
130
144
  instance.replace :message, with: 'test'
131
145
  end
132
146
 
133
147
  it 'parses remove' do
134
- expect(Rewriter::RemoveAction).to receive(:new).with(instance)
148
+ action = double
149
+ expect(Rewriter::RemoveAction).to receive(:new).with(instance).and_return(action)
150
+ expect(action).to receive(:process)
135
151
  instance.remove
136
152
  end
137
153
 
138
154
  it 'parses remove' do
139
- expect(Rewriter::DeleteAction).to receive(:new).with(instance, :dot, :message)
155
+ action = double
156
+ expect(Rewriter::DeleteAction).to receive(:new).with(instance, :dot, :message).and_return(action)
157
+ expect(action).to receive(:process)
140
158
  instance.delete :dot, :message
141
159
  end
142
160
 
143
161
  it 'parses wrap with' do
144
- expect(Rewriter::WrapAction).to receive(:new).with(instance, with: 'module Foo', indent: nil)
162
+ action = double
163
+ expect(Rewriter::WrapAction).to receive(:new).with(instance, with: 'module Foo', indent: nil).and_return(action)
164
+ expect(action).to receive(:process)
145
165
  instance.wrap with: 'module Foo'
146
166
  end
147
167
 
@@ -4,6 +4,11 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter::RubyVersion do
7
+ before do
8
+ expect(File).to receive(:exist?).with('./.ruby-version').and_return(true)
9
+ expect(File).to receive(:read).with('./.ruby-version').and_return('3.0.0')
10
+ end
11
+
7
12
  it 'returns true if ruby version is greater than 1.9' do
8
13
  ruby_version = Rewriter::RubyVersion.new('1.9')
9
14
  expect(ruby_version).to be_match
@@ -39,7 +39,8 @@ module Synvert::Core
39
39
 
40
40
  describe 'parses within_file' do
41
41
  it 'does nothing if if_ruby does not match' do
42
- stub_const('RUBY_VERSION', '2.0.0')
42
+ expect(File).to receive(:exist?).with('./.ruby-version').and_return(true)
43
+ expect(File).to receive(:read).with('./.ruby-version').and_return('2.0.0')
43
44
  expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
44
45
  rewriter =
45
46
  Rewriter.new 'group', 'name' do
@@ -51,7 +52,8 @@ module Synvert::Core
51
52
  end
52
53
 
53
54
  it 'delegates process to instances if if_ruby matches' do
54
- stub_const('RUBY_VERSION', '2.0.0')
55
+ expect(File).to receive(:exist?).with('./.ruby-version').and_return(true)
56
+ expect(File).to receive(:read).with('./.ruby-version').and_return('2.0.0')
55
57
  expect_any_instance_of(Rewriter::Instance).to receive(:process)
56
58
  rewriter =
57
59
  Rewriter.new 'group', 'name' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.55.0
4
+ version: 0.57.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-11 00:00:00.000000000 Z
11
+ date: 2021-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport