synvert-core 0.15.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +10 -0
  3. data/Gemfile +2 -0
  4. data/Guardfile +2 -0
  5. data/Rakefile +2 -0
  6. data/lib/synvert/core.rb +1 -2
  7. data/lib/synvert/core/configuration.rb +2 -1
  8. data/lib/synvert/core/engine.rb +1 -1
  9. data/lib/synvert/core/engine/erb.rb +2 -1
  10. data/lib/synvert/core/exceptions.rb +5 -3
  11. data/lib/synvert/core/node_ext.rb +7 -2
  12. data/lib/synvert/core/rewriter.rb +1 -1
  13. data/lib/synvert/core/rewriter/action.rb +2 -2
  14. data/lib/synvert/core/rewriter/action/append_action.rb +7 -5
  15. data/lib/synvert/core/rewriter/action/insert_action.rb +13 -18
  16. data/lib/synvert/core/rewriter/action/insert_after_action.rb +1 -1
  17. data/lib/synvert/core/rewriter/action/remove_action.rb +1 -1
  18. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +1 -1
  19. data/lib/synvert/core/rewriter/action/replace_with_action.rb +1 -1
  20. data/lib/synvert/core/rewriter/condition.rb +1 -1
  21. data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
  22. data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -1
  23. data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
  24. data/lib/synvert/core/rewriter/gem_spec.rb +8 -9
  25. data/lib/synvert/core/rewriter/helper.rb +1 -1
  26. data/lib/synvert/core/rewriter/instance.rb +18 -7
  27. data/lib/synvert/core/rewriter/ruby_version.rb +1 -1
  28. data/lib/synvert/core/rewriter/scope.rb +1 -1
  29. data/lib/synvert/core/rewriter/scope/goto_scope.rb +2 -1
  30. data/lib/synvert/core/rewriter/scope/within_scope.rb +23 -7
  31. data/lib/synvert/core/rewriter/warning.rb +1 -1
  32. data/lib/synvert/core/version.rb +2 -2
  33. data/spec/spec_helper.rb +2 -0
  34. data/spec/support/parser_helper.rb +2 -0
  35. data/spec/synvert/core/configuration_spec.rb +3 -1
  36. data/spec/synvert/core/engine/erb_spec.rb +2 -0
  37. data/spec/synvert/core/node_ext_spec.rb +4 -2
  38. data/spec/synvert/core/rewriter/action/append_action_spec.rb +2 -0
  39. data/spec/synvert/core/rewriter/action/insert_action_spec.rb +2 -0
  40. data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +2 -0
  41. data/spec/synvert/core/rewriter/action/remove_action_spec.rb +2 -0
  42. data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +2 -0
  43. data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +6 -4
  44. data/spec/synvert/core/rewriter/action_spec.rb +2 -0
  45. data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +4 -2
  46. data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +6 -4
  47. data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +4 -2
  48. data/spec/synvert/core/rewriter/condition_spec.rb +2 -0
  49. data/spec/synvert/core/rewriter/gem_spec_spec.rb +10 -9
  50. data/spec/synvert/core/rewriter/helper_spec.rb +2 -0
  51. data/spec/synvert/core/rewriter/instance_spec.rb +38 -20
  52. data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +2 -0
  53. data/spec/synvert/core/rewriter/scope/within_scope.rb +4 -2
  54. data/spec/synvert/core/rewriter/scope_spec.rb +2 -0
  55. data/spec/synvert/core/rewriter/warning_spec.rb +2 -0
  56. data/spec/synvert/core/rewriter_spec.rb +7 -5
  57. data/synvert-core.gemspec +2 -2
  58. metadata +12 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0a0989b728790ffffeda701daec03337d96a350c
4
- data.tar.gz: 0134e7d75925587df9f2f300410bb70199c792d5
2
+ SHA256:
3
+ metadata.gz: b73d8898436945c3266c9d0e4eec002491c3b031e1d571f94b3a59443142a9dd
4
+ data.tar.gz: c8afb1ce36ef5d3e826ebbc7ca10103175661e14f8ea1af1ae5a1a83b8714dbf
5
5
  SHA512:
6
- metadata.gz: 91178d81160d6e6fb51133de73e81b1353453719dd5c35b926ea7cef137f9c1b1a10cb88eee1a565d6aea6c1b4563c155d9282434bc192cbacb7176d710ec7c4
7
- data.tar.gz: a179c5ad2606cac974a40150868ed5f6f598df342afeab1411a564c12678e850bc7696d497089299175b88615ae14356a06f182b7620dbece14458dce8344a1c
6
+ metadata.gz: b7198c44b3696f0c0f640fe378ff3b9923799517998d6be38177096a552b33885bf5df6113d1aa1a0b68432bff79d0322587648d221bd6fbaebcf75ab8b2d32d
7
+ data.tar.gz: 39b8bf09735971e108b112b96b3377df988b82e891c76d5050fef978aec81bf340ff096463ac9416848fd9b8e412b95537377d0647d11a6028d5ab909cac4fa4
@@ -1,5 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.17.0 (2021-01-29)
4
+
5
+ * Ignore `gem_spec` check if `Gemfile.lock` does not eixst
6
+
7
+ ## 0.16.0 (2021-01-17)
8
+
9
+ * Use parser 3.0.0
10
+ * Fix magic number
11
+ * Add `within_direct_node` scope
12
+
3
13
  ## 0.15.0 (2018-05-23)
4
14
 
5
15
  * Use parser 2.5.1.1
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in synvert.gemspec
data/Guardfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  guard :rspec, cmd: 'bundle exec rspec' do
2
4
  watch(%r{^spec/.+_spec\.rb$})
3
5
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require 'rspec/core/rake_task'
3
5
 
@@ -1,6 +1,5 @@
1
- require "synvert/core/version"
1
+ # frozen_string_literal: true
2
2
 
3
- # coding: utf-8
4
3
  require "synvert/core/version"
5
4
  require 'bundler'
6
5
  require 'parser'
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'singleton'
3
4
 
4
5
  module Synvert::Core
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Engine defines how to encode / decode other files (like erb).
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'erubis'
3
4
 
4
5
  module Synvert::Core
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Synvert::Core
2
4
  # Rewriter not found exception.
3
- class RewriterNotFound < Exception
5
+ class RewriterNotFound < RuntimeError
4
6
  end
5
7
 
6
8
  # Gemfile.lock not found exception.
7
- class GemfileLockNotFound < Exception
9
+ class GemfileLockNotFound < RuntimeError
8
10
  end
9
11
 
10
12
  # Method not supported exception.
11
- class MethodNotSupported < Exception
13
+ class MethodNotSupported < RuntimeError
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Parser::AST
2
4
  # ArgumentsNode allows to handle all args as one node or handle all args as an array.
3
5
  class ArgumentsNode
@@ -133,9 +135,11 @@ module Parser::AST
133
135
  self.children
134
136
  when :def, :block
135
137
  return [] if self.children[2].nil?
138
+
136
139
  :begin == self.children[2].type ? self.children[2].body : self.children[2..-1]
137
140
  when :defs
138
141
  return [] if self.children[3].nil?
142
+
139
143
  :begin == self.children[3].type ? self.children[3].body : self.children[3..-1]
140
144
  else
141
145
  raise Synvert::Core::MethodNotSupported.new "body is not handled for #{self.debug_info}"
@@ -234,7 +238,7 @@ module Parser::AST
234
238
  # @return [Parser::AST::Node] variable nodes.
235
239
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
236
240
  def left_value
237
- if [:masgn, :lvasgn, :ivasgn].include? self.type
241
+ if %i[masgn lvasgn ivasgn].include? self.type
238
242
  self.children[0]
239
243
  else
240
244
  raise Synvert::Core::MethodNotSupported.new "left_value is not handled for #{self.debug_info}"
@@ -246,7 +250,7 @@ module Parser::AST
246
250
  # @return [Array<Parser::AST::Node>] variable nodes.
247
251
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
248
252
  def right_value
249
- if [:masgn, :lvasgn, :ivasgn].include? self.type
253
+ if %i[masgn lvasgn ivasgn].include? self.type
250
254
  self.children[1]
251
255
  else
252
256
  raise Synvert::Core::MethodNotSupported.new "right_value is not handled for #{self.debug_info}"
@@ -425,6 +429,7 @@ module Parser::AST
425
429
  end
426
430
  when Array
427
431
  return false unless expected.length == actual.length
432
+
428
433
  actual.zip(expected).all? { |a, e| match_value?(a, e) }
429
434
  when NilClass
430
435
  actual.nil?
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Rewriter is the top level namespace in a snippet.
@@ -1,9 +1,10 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Action defines rewriter action, add, replace or remove code.
5
5
  class Rewriter::Action
6
6
  DEFAULT_OPTIONS = { autoindent: true }
7
+ DEFAULT_INDENT = 2
7
8
 
8
9
  # Initialize an action.
9
10
  #
@@ -45,4 +46,3 @@ module Synvert::Core
45
46
  end
46
47
  end
47
48
  end
48
-
@@ -1,8 +1,10 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
- # AppendWithAction to append code to the bottom of node body.
4
+ # AppendAction to append code to the bottom of node body.
5
5
  class Rewriter::AppendAction < Rewriter::Action
6
+ END_LENGTH = "\nend".length
7
+
6
8
  # Begin position to append code.
7
9
  #
8
10
  # @return [Integer] begin position.
@@ -10,7 +12,7 @@ module Synvert::Core
10
12
  if :begin == @node.type
11
13
  @node.loc.expression.end_pos
12
14
  else
13
- @node.loc.expression.end_pos - @node.indent - 4
15
+ @node.loc.expression.end_pos - @node.indent - END_LENGTH
14
16
  end
15
17
  end
16
18
 
@@ -28,8 +30,8 @@ module Synvert::Core
28
30
  # @param node [Parser::AST::Node]
29
31
  # @return [String] n times whitesphace
30
32
  def indent(node)
31
- if [:block, :class].include? node.type
32
- ' ' * (node.indent + 2)
33
+ if %i[block class].include? node.type
34
+ ' ' * (node.indent + DEFAULT_INDENT)
33
35
  else
34
36
  ' ' * node.indent
35
37
  end
@@ -1,13 +1,22 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # InsertAction to insert code to the top of node body.
5
5
  class Rewriter::InsertAction < Rewriter::Action
6
+ DO_LENGTH = " do".length
7
+
6
8
  # Begin position to insert code.
7
9
  #
8
10
  # @return [Integer] begin position.
9
11
  def begin_pos
10
- insert_position(@node)
12
+ case @node.type
13
+ when :block
14
+ @node.children[1].children.empty? ? @node.children[0].loc.expression.end_pos + DO_LENGTH : @node.children[1].loc.expression.end_pos
15
+ when :class
16
+ @node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
17
+ else
18
+ @node.children.last.loc.expression.end_pos
19
+ end
11
20
  end
12
21
 
13
22
  # End position, always same to begin position.
@@ -19,27 +28,13 @@ module Synvert::Core
19
28
 
20
29
  private
21
30
 
22
- # Insert position.
23
- #
24
- # @return [Integer] insert position.
25
- def insert_position(node)
26
- case node.type
27
- when :block
28
- node.children[1].children.empty? ? node.children[0].loc.expression.end_pos + 3 : node.children[1].loc.expression.end_pos
29
- when :class
30
- node.children[1] ? node.children[1].loc.expression.end_pos : node.children[0].loc.expression.end_pos
31
- else
32
- node.children.last.loc.expression.end_pos
33
- end
34
- end
35
-
36
31
  # Indent of the node.
37
32
  #
38
33
  # @param node [Parser::AST::Node]
39
34
  # @return [String] n times whitesphace
40
35
  def indent(node)
41
- if [:block, :class].include? node.type
42
- ' ' * (node.indent + 2)
36
+ if %i[block class].include? node.type
37
+ ' ' * (node.indent + DEFAULT_INDENT)
43
38
  else
44
39
  ' ' * node.indent
45
40
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # InsertAfterAction to insert code next to the node.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # RemoveAction to remove code.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # ReplaceErbStmtWithExprAction to replace erb stmt code to expr,
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # ReplaceWithAction to replace code.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Condition checks if rules matches.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # IfExistCondition checks if matching node exists in the node children.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # IfExistCondition checks if node has only one child node and the child node matches rules.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # UnlessExistCondition checks if matching node doesn't exist in the node children.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # GemSpec checks and compares gem version.
@@ -27,15 +27,14 @@ module Synvert::Core
27
27
  # @raise [Synvert::Core::GemfileLockNotFound] raise if Gemfile.lock does not exist.
28
28
  def match?
29
29
  gemfile_lock_path = File.join(Configuration.instance.get(:path), 'Gemfile.lock')
30
- if File.exists? gemfile_lock_path
31
- parser = Bundler::LockfileParser.new(File.read(gemfile_lock_path))
32
- if spec = parser.specs.find { |spec| spec.name == @name }
33
- Gem::Version.new(spec.version).send(OPERATORS[@operator], @version)
34
- else
35
- false
36
- end
30
+ # if Gemfile.lock does not exist, just ignore this check
31
+ return true unless File.exist?(gemfile_lock_path)
32
+
33
+ parser = Bundler::LockfileParser.new(File.read(gemfile_lock_path))
34
+ if spec = parser.specs.find { |spec| spec.name == @name }
35
+ Gem::Version.new(spec.version).send(OPERATORS[@operator], @version)
37
36
  else
38
- raise GemfileLockNotFound.new 'Gemfile.lock does not exist'
37
+ false
39
38
  end
40
39
  end
41
40
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Rewriter::Helper provides some helper methods to make it easier to write a snippet.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Instance is an execution unit, it finds specified ast nodes,
@@ -91,7 +91,7 @@ module Synvert::Core
91
91
  unless Configuration.instance.get(:skip_files).include? file_path
92
92
  begin
93
93
  conflict_actions = []
94
- source = self.class.file_source(file_path)
94
+ source = +self.class.file_source(file_path)
95
95
  ast = self.class.file_ast(file_path)
96
96
 
97
97
  @current_file = file_path
@@ -108,7 +108,7 @@ module Synvert::Core
108
108
  if @actions.length > 0
109
109
  @actions.sort_by! { |action| action.send(@options[:sort_by]) }
110
110
  conflict_actions = get_conflict_actions
111
- @actions.reverse.each do |action|
111
+ @actions.reverse_each do |action|
112
112
  source[action.begin_pos...action.end_pos] = action.rewritten_code
113
113
  source = remove_code_or_whole_line(source, action.line)
114
114
  end
@@ -156,15 +156,28 @@ module Synvert::Core
156
156
  # DSL #
157
157
  #######
158
158
 
159
- # Parse within_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find matching ast nodes,
159
+ # Parse within_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find recursive matching ast nodes,
160
160
  # then continue operating on each matching ast node.
161
161
  #
162
162
  # @param rules [Hash] rules to find mathing ast nodes.
163
163
  # @param block [Block] block code to continue operating on the matching nodes.
164
164
  def within_node(rules, &block)
165
- Rewriter::WithinScope.new(self, rules, &block).process
165
+ Rewriter::WithinScope.new(self, rules, { recursive: true }, &block).process
166
166
  end
167
167
 
168
+ alias_method :with_node, :within_node
169
+
170
+ # Parse within_direct_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find direct matching ast nodes,
171
+ # then continue operating on each matching ast node.
172
+ #
173
+ # @param rules [Hash] rules to find mathing ast nodes.
174
+ # @param block [Block] block code to continue operating on the matching nodes.
175
+ def within_direct_node(rules, &block)
176
+ Rewriter::WithinScope.new(self, rules, { recursive: false }, &block).process
177
+ end
178
+
179
+ alias_method :with_direct_node, :within_direct_node
180
+
168
181
  # Parse goto_node dsl, it creates a [Synvert::Core::Rewriter::GotoScope] to go to a child node,
169
182
  # then continue operating on the child node.
170
183
  #
@@ -174,8 +187,6 @@ module Synvert::Core
174
187
  Rewriter::GotoScope.new(self, child_node_name, &block).process
175
188
  end
176
189
 
177
- alias_method :with_node, :within_node
178
-
179
190
  # Parse if_exist_node dsl, it creates a [Synvert::Core::Rewriter::IfExistCondition] to check
180
191
  # if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
181
192
  #
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # GemSpec checks and compares gem version.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Scope finds out nodes which match rules.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Go to and change its scope to a child node.
@@ -18,6 +18,7 @@ module Synvert::Core
18
18
  def process
19
19
  current_node = @instance.current_node
20
20
  return unless current_node
21
+
21
22
  child_node = current_node.send @child_node_name
22
23
  @instance.process_with_other_node child_node do
23
24
  @instance.instance_eval &@block
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # WithinScope finds out nodes which match rules, then change its scope to matching node.
@@ -7,10 +7,12 @@ module Synvert::Core
7
7
  #
8
8
  # @param instance [Synvert::Core::Rewriter::Instance]
9
9
  # @param rules [Hash]
10
+ # @param options [Hash]
10
11
  # @param block [Block]
11
- def initialize(instance, rules, &block)
12
+ def initialize(instance, rules, options = { recursive: true }, &block)
12
13
  @instance = instance
13
14
  @rules = rules
15
+ @options = options
14
16
  @block = block
15
17
  end
16
18
 
@@ -19,12 +21,9 @@ module Synvert::Core
19
21
  def process
20
22
  current_node = @instance.current_node
21
23
  return unless current_node
24
+
25
+ matching_nodes = find_matching_nodes(current_node)
22
26
  @instance.process_with_node current_node do
23
- matching_nodes = []
24
- matching_nodes << current_node if current_node.match? @rules
25
- current_node.recursive_children do |child_node|
26
- matching_nodes << child_node if child_node.match? @rules
27
- end
28
27
  matching_nodes.each do |matching_node|
29
28
  @instance.process_with_node matching_node do
30
29
  @instance.instance_eval &@block
@@ -32,5 +31,22 @@ module Synvert::Core
32
31
  end
33
32
  end
34
33
  end
34
+
35
+ private
36
+
37
+ def find_matching_nodes(current_node)
38
+ matching_nodes = []
39
+ if @options[:recursive]
40
+ matching_nodes << current_node if current_node.match? @rules
41
+ current_node.recursive_children do |child_node|
42
+ matching_nodes << child_node if child_node.match? @rules
43
+ end
44
+ else
45
+ current_node.each do |child_node|
46
+ matching_nodes << child_node if child_node.match? @rules
47
+ end
48
+ end
49
+ matching_nodes
50
+ end
35
51
  end
36
52
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Warning is used to save warning message.
@@ -1,7 +1,7 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = "0.15.0"
5
+ VERSION = "0.17.0"
6
6
  end
7
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
4
 
3
5
  require 'synvert/core'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ParserHelper
2
4
  def parse(code)
3
5
  Parser::CurrentRuby.parse code
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
4
6
  describe Configuration do
5
7
  it 'sets / gets' do
6
8
  Configuration.instance.set :key, 'value'
7
- expect(Configuration.instance.get :key).to eq 'value'
9
+ expect(Configuration.instance.get(:key)).to eq 'value'
8
10
  end
9
11
  end
10
12
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Parser::AST::Node do
@@ -102,12 +104,12 @@ describe Parser::AST::Node do
102
104
  describe '#arguments' do
103
105
  it 'gets for def node' do
104
106
  node = parse("def test(foo, bar); foo + bar; end")
105
- expect(node.arguments.map { |argument| argument.to_source }).to eq ['foo', 'bar']
107
+ expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
106
108
  end
107
109
 
108
110
  it 'gets for defs node' do
109
111
  node = parse("def self.test(foo, bar); foo + bar; end")
110
- expect(node.arguments.map { |argument| argument.to_source }).to eq ['foo', 'bar']
112
+ expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
111
113
  end
112
114
 
113
115
  it 'gets for block node' do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -28,10 +30,10 @@ module Synvert::Core
28
30
  source = " its(:size) { should == 1 }"
29
31
  send_node = Parser::CurrentRuby.parse(source)
30
32
  instance = double(current_node: send_node)
31
- Rewriter::ReplaceWithAction.new(instance, """describe '#size' do
33
+ Rewriter::ReplaceWithAction.new(instance, "describe '#size' do
32
34
  subject { super().size }
33
35
  it { {{body}} }
34
- end""", autoindent: false)
36
+ end", autoindent: false)
35
37
  }
36
38
 
37
39
  it 'gets begin_pos' do
@@ -43,10 +45,10 @@ end""", autoindent: false)
43
45
  end
44
46
 
45
47
  it 'gets rewritten_code' do
46
- expect(subject.rewritten_code).to eq """describe '#size' do
48
+ expect(subject.rewritten_code).to eq "describe '#size' do
47
49
  subject { super().size }
48
50
  it { should == 1 }
49
- end"""
51
+ end"
50
52
  end
51
53
  end
52
54
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
4
6
  describe Rewriter::IfExistCondition do
5
7
  let(:source) {
6
- """
8
+ "
7
9
  RSpec.configure do |config|
8
10
  config.include EmailSpec::Helpers
9
11
  config.include EmailSpec::Methods
10
12
  end
11
- """
13
+ "
12
14
  }
13
15
  let(:node) { Parser::CurrentRuby.parse(source) }
14
16
  let(:instance) { double(:current_node => node) }
@@ -1,25 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
4
6
  describe Rewriter::IfOnlyExistCondition do
5
7
  let(:source) {
6
- """
8
+ "
7
9
  RSpec.configure do |config|
8
10
  config.include EmailSpec::Helpers
9
11
  config.include EmailSpec::Methods
10
12
  end
11
- """
13
+ "
12
14
  }
13
15
  let(:node) { Parser::CurrentRuby.parse(source) }
14
16
  let(:instance) { double(:current_node => node) }
15
17
 
16
18
  describe '#process' do
17
19
  it 'gets matching nodes' do
18
- source = """
20
+ source = "
19
21
  RSpec.configure do |config|
20
22
  config.include EmailSpec::Helpers
21
23
  end
22
- """
24
+ "
23
25
  node = Parser::CurrentRuby.parse(source)
24
26
  instance = double(:current_node => node)
25
27
  run = false
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
4
6
  describe Rewriter::UnlessExistCondition do
5
7
  let(:source) {
6
- """
8
+ "
7
9
  RSpec.configure do |config|
8
10
  config.include EmailSpec::Helpers
9
11
  config.include EmailSpec::Methods
10
12
  end
11
- """
13
+ "
12
14
  }
13
15
  let(:node) { Parser::CurrentRuby.parse(source) }
14
16
  let(:instance) { double(:current_node => node) }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
4
6
  describe Rewriter::GemSpec do
5
- let(:gemfile_lock_content) { """
7
+ let(:gemfile_lock_content) { "
6
8
  GEM
7
9
  remote: https://rubygems.org/
8
10
  specs:
@@ -12,41 +14,40 @@ GEM
12
14
  slop (~> 3.4, >= 3.4.5)
13
15
  rake (10.1.1)
14
16
  slop (3.4.7)
15
- """
16
- }
17
+ "}
17
18
 
18
19
  it 'returns true if version in Gemfile.lock is greater than definition' do
19
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
20
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
20
21
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
21
22
  gem_spec = Rewriter::GemSpec.new('ast', {gte: '1.0.0'})
22
23
  expect(gem_spec).to be_match
23
24
  end
24
25
 
25
26
  it 'returns true if version in Gemfile.lock is equal to definition' do
26
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
27
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
27
28
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
28
29
  gem_spec = Rewriter::GemSpec.new('ast', '1.1.0')
29
30
  expect(gem_spec).to be_match
30
31
  end
31
32
 
32
33
  it 'returns false if version in Gemfile.lock is less than definition' do
33
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
34
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
34
35
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
35
36
  gem_spec = Rewriter::GemSpec.new('ast', {gt: '1.2.0'})
36
37
  expect(gem_spec).not_to be_match
37
38
  end
38
39
 
39
40
  it 'returns false if gem does not exist in Gemfile.lock' do
40
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
41
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
41
42
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
42
43
  gem_spec = Rewriter::GemSpec.new('synvert', '1.0.0')
43
44
  expect(gem_spec).not_to be_match
44
45
  end
45
46
 
46
47
  it 'raise Synvert::Core::GemfileLockNotFound if Gemfile.lock does not exist' do
47
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(false)
48
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(false)
48
49
  gem_spec = Rewriter::GemSpec.new('ast', '1.1.0')
49
- expect { gem_spec.match? }.to raise_error(Synvert::Core::GemfileLockNotFound)
50
+ expect(gem_spec).to be_match
50
51
  end
51
52
  end
52
53
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -12,7 +14,7 @@ module Synvert::Core
12
14
  it 'parses within_node' do
13
15
  scope = double()
14
16
  block = Proc.new {}
15
- expect(Rewriter::WithinScope).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(scope)
17
+ expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: true }, &block).and_return(scope)
16
18
  expect(scope).to receive(:process)
17
19
  instance.within_node(type: 'send', message: 'create', &block)
18
20
  end
@@ -20,11 +22,27 @@ module Synvert::Core
20
22
  it 'parses with_node' do
21
23
  scope = double()
22
24
  block = Proc.new {}
23
- expect(Rewriter::WithinScope).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(scope)
25
+ expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: true }, &block).and_return(scope)
24
26
  expect(scope).to receive(:process)
25
27
  instance.with_node(type: 'send', message: 'create', &block)
26
28
  end
27
29
 
30
+ it 'parses within_direct_node' do
31
+ scope = double()
32
+ block = Proc.new {}
33
+ expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: false }, &block).and_return(scope)
34
+ expect(scope).to receive(:process)
35
+ instance.within_direct_node(type: 'send', message: 'create', &block)
36
+ end
37
+
38
+ it 'parses with_direct_node' do
39
+ scope = double()
40
+ block = Proc.new {}
41
+ expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: false }, &block).and_return(scope)
42
+ expect(scope).to receive(:process)
43
+ instance.with_direct_node(type: 'send', message: 'create', &block)
44
+ end
45
+
28
46
  it 'parses goto_node' do
29
47
  scope = double()
30
48
  block = Proc.new {}
@@ -96,20 +114,20 @@ module Synvert::Core
96
114
  replace_with 'create {{arguments}}'
97
115
  end
98
116
  end
99
- input = """
117
+ input = "
100
118
  it 'uses factory_girl' do
101
119
  user = FactoryGirl.create :user
102
120
  post = FactoryGirl.create :post, user: user
103
121
  assert post.valid?
104
122
  end
105
- """
106
- output = """
123
+ "
124
+ output = "
107
125
  it 'uses factory_girl' do
108
126
  user = create :user
109
127
  post = create :post, user: user
110
128
  assert post.valid?
111
129
  end
112
- """
130
+ "
113
131
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
114
132
  expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
115
133
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
@@ -124,16 +142,16 @@ end
124
142
  end
125
143
  end
126
144
  end
127
- input = """
145
+ input = "
128
146
  RSpec.configure do |config|
129
147
  config.include FactoryGirl::Syntax::Methods
130
148
  end
131
- """
132
- output = """
149
+ "
150
+ output = "
133
151
  RSpec.configure do |config|
134
152
  config.include FactoryGirl::Syntax::Methods
135
153
  end
136
- """
154
+ "
137
155
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
138
156
  expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
139
157
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
@@ -148,16 +166,16 @@ end
148
166
  end
149
167
  end
150
168
  end
151
- input = """
169
+ input = "
152
170
  RSpec.configure do |config|
153
171
  config.include FactoryGirl::Syntax::Methods
154
172
  end
155
- """
156
- output = """
173
+ "
174
+ output = "
157
175
  RSpec.configure do |config|
158
176
  config.include FactoryGirl::Syntax::Methods
159
177
  end
160
- """
178
+ "
161
179
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
162
180
  expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input).once
163
181
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
@@ -171,20 +189,20 @@ end
171
189
  replace_with 'create {{arguments}}'
172
190
  end
173
191
  end
174
- input = """
192
+ input = "
175
193
  it 'uses factory_girl' do
176
194
  user = FactoryGirl.create :user
177
195
  post = FactoryGirl.create :post, user: user
178
196
  assert post.valid?
179
197
  end
180
- """
181
- output = """
198
+ "
199
+ output = "
182
200
  it 'uses factory_girl' do
183
201
  user = create :user
184
202
  post = create :post, user: user
185
203
  assert post.valid?
186
204
  end
187
- """
205
+ "
188
206
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
189
207
  expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
190
208
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
@@ -205,7 +223,7 @@ end
205
223
  instance.instance_variable_set :@actions, [action1, action2, action3]
206
224
  conflict_actions = instance.send(:get_conflict_actions)
207
225
  expect(conflict_actions).to eq []
208
- expect(instance.instance_variable_get :@actions).to eq [action1, action2, action3]
226
+ expect(instance.instance_variable_get(:@actions)).to eq [action1, action2, action3]
209
227
  end
210
228
 
211
229
  it "has no conflict" do
@@ -216,7 +234,7 @@ end
216
234
  instance.instance_variable_set :@actions, [action1, action2, action3]
217
235
  conflict_actions = instance.send(:get_conflict_actions)
218
236
  expect(conflict_actions).to eq [action2, action1]
219
- expect(instance.instance_variable_get :@actions).to eq [action3]
237
+ expect(instance.instance_variable_get(:@actions)).to eq [action3]
220
238
  end
221
239
  end
222
240
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -6,13 +8,13 @@ module Synvert::Core
6
8
  rewriter = Rewriter.new('foo', 'bar')
7
9
  Rewriter::Instance.new(rewriter, 'file pattern')
8
10
  }
9
- let(:source) {"""
11
+ let(:source) {"
10
12
  describe Post do
11
13
  it 'gets post' do
12
14
  FactoryGirl.create :post
13
15
  end
14
16
  end
15
- """
17
+ "
16
18
  }
17
19
  let(:node) { Parser::CurrentRuby.parse(source) }
18
20
  before do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
@@ -92,7 +94,7 @@ module Synvert::Core
92
94
  add_file 'foo.bar', 'FooBar'
93
95
  end
94
96
  rewriter.process
95
- expect(File.read './foo.bar').to eq 'FooBar'
97
+ expect(File.read('./foo.bar')).to eq 'FooBar'
96
98
  FileUtils.rm './foo.bar'
97
99
  end
98
100
 
@@ -112,7 +114,7 @@ module Synvert::Core
112
114
  remove_file 'foo.bar'
113
115
  end
114
116
  rewriter.process
115
- expect(File.exist? './foo.bar').to be_falsey
117
+ expect(File.exist?('./foo.bar')).to be_falsey
116
118
  end
117
119
 
118
120
  it 'does nothing if file not exist' do
@@ -120,7 +122,7 @@ module Synvert::Core
120
122
  remove_file 'foo.bar'
121
123
  end
122
124
  rewriter.process
123
- expect(File.exist? './foo.bar').to be_falsey
125
+ expect(File.exist?('./foo.bar')).to be_falsey
124
126
  end
125
127
 
126
128
  it 'does nothing in sandbox mode' do
@@ -204,11 +206,11 @@ module Synvert::Core
204
206
  context "exist?" do
205
207
  it 'returns true if rewriter exists' do
206
208
  Rewriter.new 'group', 'rewriter'
207
- expect(Rewriter.exist? 'group', 'rewriter').to be_truthy
209
+ expect(Rewriter.exist?('group', 'rewriter')).to be_truthy
208
210
  end
209
211
 
210
212
  it 'returns false if rewriter does not exist' do
211
- expect(Rewriter.exist? 'group', 'rewriter').to be_falsey
213
+ expect(Rewriter.exist?('group', 'rewriter')).to be_falsey
212
214
  end
213
215
  end
214
216
 
@@ -18,11 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "parser", "~> 2.5.0"
21
+ spec.add_runtime_dependency "parser", "~> 3.0.0"
22
22
  spec.add_runtime_dependency "activesupport"
23
23
  spec.add_runtime_dependency "erubis"
24
24
 
25
- spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "bundler"
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "rspec"
28
28
  spec.add_development_dependency "guard"
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.15.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-23 00:00:00.000000000 Z
11
+ date: 2021-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.5.0
19
+ version: 3.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.5.0
26
+ version: 3.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '1.6'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '1.6'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -194,7 +194,7 @@ homepage: https://github.com/xinminlabs/synvert-core
194
194
  licenses:
195
195
  - MIT
196
196
  metadata: {}
197
- post_install_message:
197
+ post_install_message:
198
198
  rdoc_options: []
199
199
  require_paths:
200
200
  - lib
@@ -209,9 +209,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
209
209
  - !ruby/object:Gem::Version
210
210
  version: '0'
211
211
  requirements: []
212
- rubyforge_project:
213
- rubygems_version: 2.6.14
214
- signing_key:
212
+ rubygems_version: 3.1.4
213
+ signing_key:
215
214
  specification_version: 4
216
215
  summary: convert ruby code to better syntax.
217
216
  test_files: