synvert-core 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43a4d413d049428517fba9bfe79d57aadec0fa3b98d72a377170888005fe8e50
4
- data.tar.gz: c0efe87cdfcbac1e837126f6525c45bfaba5213fee0688ca774666a60ba62851
3
+ metadata.gz: 8c112197bf98b5adfe2b5826ea3301f06f66511c111906bfd0c72a8c54a10d33
4
+ data.tar.gz: adb56b1ae52dd1b87d84d2861b7433586c96ddc6718fafcd79852775f84b38c4
5
5
  SHA512:
6
- metadata.gz: 8b81c56d7e16caf4d1409e896dd405a6b6c242df8512da45ca83b43040e74edb34a11b1197e90d6d58e05c5225ff695992d52bbaae0862c45e68ad151791e29b
7
- data.tar.gz: 56f14b2292823f227193faf06c727c819b0adcb684f6d3db2429b2cb8cc01cc7a0f9a2147eb98e7c2ea8c23297dcf415a9c27a59ba5ad02b2dbd187b1d08a393
6
+ metadata.gz: e2f79639b669fc7b054ba8e57df70925f7f9bfdf6e40144069d41848bc0d0b3df0d60e73fc078c8e78eb29718bc8f3a181ee7964c63a3dcadd9a8981ce31544b
7
+ data.tar.gz: 758e0896064a0c15dbe4e3d3d128bc3f42517c2f2802067cccab20f3b366165d7ef5f3203975e261903573f2989f82f3918b09db925a2d896b9bee5897299a58
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.22.0 (2021-03-13)
4
+
5
+ * Track `affected_files` for rewriter
6
+ * Fix `find_matching_nodes` for `current_node`
7
+
8
+ ## 0.21.0 (2021-02-25)
9
+
10
+ * Set `env['BUNDLE_GEMFILE']` before parsing `Gemfile.lock`
11
+ * Add `Rewriter::RubyVersion` test
12
+ * Add `reject_keys_from_hash` helper method
13
+
3
14
  ## 0.20.0 (2021-02-15)
4
15
 
5
16
  * Call snippet in sandbox mode
data/lib/synvert/core.rb CHANGED
@@ -7,6 +7,7 @@ require 'parser/current'
7
7
  require 'ast'
8
8
  require 'active_support/core_ext/object'
9
9
  require 'erubis'
10
+ require 'set'
10
11
  require 'synvert/core/node_ext'
11
12
 
12
13
  module Synvert
@@ -31,21 +31,23 @@ module Synvert::Core
31
31
  private
32
32
 
33
33
  def decode_ruby_stmt(source)
34
- source.gsub(/#{ERUBY_STMT_SPLITTER}(.+?)#{ERUBY_STMT_SPLITTER}/m) { "<%#{$1}%>" }
34
+ source.gsub(/#{ERUBY_STMT_SPLITTER}(.+?)#{ERUBY_STMT_SPLITTER}/mo) { "<%#{Regexp.last_match(1)}%>" }
35
35
  end
36
36
 
37
37
  def decode_ruby_output(source)
38
- source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { "<%=#{$1}%>" }.gsub(
39
- /@output_buffer.append= (.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m
40
- ) { |m| "<%=#{m.sub('@output_buffer.append= ', '').sub(ERUBY_EXPR_SPLITTER, '')}%>" }
38
+ source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/mo) {
39
+ "<%=#{Regexp.last_match(1)}%>"
40
+ }.gsub(/@output_buffer.append= (.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/mo) { |m|
41
+ "<%=#{m.sub('@output_buffer.append= ', '').sub(ERUBY_EXPR_SPLITTER, '')}%>"
42
+ }
41
43
  end
42
44
 
43
45
  def decode_html_output(source)
44
- source.gsub(/@output_buffer.safe_append='(.+?)'.freeze;/m) { reverse_escape_text($1) }.gsub(
45
- /@output_buffer.safe_append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m
46
- ) { reverse_escape_text($1) }.gsub(
47
- /@output_buffer.safe_append=(.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m
48
- ) { reverse_escape_text($1) }
46
+ source.gsub(/@output_buffer.safe_append='(.+?)'.freeze;/m) { reverse_escape_text(Regexp.last_match(1)) }.gsub(
47
+ /@output_buffer.safe_append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/mo
48
+ ) { reverse_escape_text(Regexp.last_match(1)) }.gsub(
49
+ /@output_buffer.safe_append=(.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/mo
50
+ ) { reverse_escape_text(Regexp.last_match(1)) }
49
51
  end
50
52
 
51
53
  def remove_erubis_buf(source)
@@ -97,7 +99,7 @@ module Synvert::Core
97
99
 
98
100
  def add_expr_literal(src, code)
99
101
  flush_newline_if_pending(src)
100
- if code =~ BLOCK_EXPR
102
+ if BLOCK_EXPR.match?(code)
101
103
  src << '@output_buffer.append= ' << code << ERUBY_EXPR_SPLITTER
102
104
  else
103
105
  src << '@output_buffer.append=(' << code << ');' << ERUBY_EXPR_SPLITTER
@@ -106,7 +108,7 @@ module Synvert::Core
106
108
 
107
109
  def add_expr_escaped(src, code)
108
110
  flush_newline_if_pending(src)
109
- if code =~ BLOCK_EXPR
111
+ if BLOCK_EXPR.match?(code)
110
112
  src << '@output_buffer.safe_append= ' << code << ERUBY_EXPR_SPLITTER
111
113
  else
112
114
  src << '@output_buffer.safe_append=(' << code << ');' << ERUBY_EXPR_SPLITTER
@@ -119,9 +121,9 @@ module Synvert::Core
119
121
  index =
120
122
  case code
121
123
  when /\A(\s*)\r?\n/
122
- $1.length
124
+ Regexp.last_match(1).length
123
125
  when /\A(\s+)/
124
- $1.end_with?(' ') ? $1.length - 1 : $1.length
126
+ Regexp.last_match(1).end_with?(' ') ? Regexp.last_match(1).length - 1 : Regexp.last_match(1).length
125
127
  else
126
128
  0
127
129
  end
@@ -321,11 +321,11 @@ module Parser::AST
321
321
  #
322
322
  # @yield [child] Gives a child node.
323
323
  # @yieldparam child [Parser::AST::Node] child node
324
- def recursive_children
324
+ def recursive_children(&block)
325
325
  children.each do |child|
326
- if Parser::AST::Node === child
326
+ if child.is_a?(Parser::AST::Node)
327
327
  yield child
328
- child.recursive_children { |c| yield c }
328
+ child.recursive_children(&block)
329
329
  end
330
330
  end
331
331
  end
@@ -335,24 +335,22 @@ module Parser::AST
335
335
  # @param rules [Hash] rules to match.
336
336
  # @return true if matches.
337
337
  def match?(rules)
338
- flat_hash(rules)
339
- .keys
340
- .all? do |multi_keys|
341
- case multi_keys.last
342
- when :any
343
- actual_values = actual_value(self, multi_keys[0...-1])
344
- expected = expected_value(rules, multi_keys)
345
- actual_values.any? { |actual| match_value?(actual, expected) }
346
- when :not
347
- actual = actual_value(self, multi_keys[0...-1])
348
- expected = expected_value(rules, multi_keys)
349
- !match_value?(actual, expected)
350
- else
351
- actual = actual_value(self, multi_keys)
352
- expected = expected_value(rules, multi_keys)
353
- match_value?(actual, expected)
354
- end
338
+ flat_hash(rules).keys.all? do |multi_keys|
339
+ case multi_keys.last
340
+ when :any
341
+ actual_values = actual_value(self, multi_keys[0...-1])
342
+ expected = expected_value(rules, multi_keys)
343
+ actual_values.any? { |actual| match_value?(actual, expected) }
344
+ when :not
345
+ actual = actual_value(self, multi_keys[0...-1])
346
+ expected = expected_value(rules, multi_keys)
347
+ !match_value?(actual, expected)
348
+ else
349
+ actual = actual_value(self, multi_keys)
350
+ expected = expected_value(rules, multi_keys)
351
+ match_value?(actual, expected)
355
352
  end
353
+ end
356
354
  end
357
355
 
358
356
  # Get rewritten source code.
@@ -364,7 +362,7 @@ module Parser::AST
364
362
  # @raise [Synvert::Core::MethodNotSupported] if string in block {{ }} does not support.
365
363
  def rewritten_source(code)
366
364
  code.gsub(/{{(.*?)}}/m) do
367
- old_code = $1
365
+ old_code = Regexp.last_match(1)
368
366
  if respond_to? old_code.split(/\.|\[/).first
369
367
  evaluated = instance_eval old_code
370
368
  case evaluated
@@ -410,20 +408,20 @@ module Parser::AST
410
408
  def match_value?(actual, expected)
411
409
  case expected
412
410
  when Symbol
413
- if Parser::AST::Node === actual
411
+ if actual.is_a?(Parser::AST::Node)
414
412
  actual.to_source == ":#{expected}"
415
413
  else
416
414
  actual.to_sym == expected
417
415
  end
418
416
  when String
419
- if Parser::AST::Node === actual
417
+ if actual.is_a?(Parser::AST::Node)
420
418
  actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
421
419
  actual.to_source[1...-1] == expected
422
420
  else
423
421
  actual.to_s == expected
424
422
  end
425
423
  when Regexp
426
- if Parser::AST::Node === actual
424
+ if actual.is_a?(Parser::AST::Node)
427
425
  actual.to_source =~ Regexp.new(expected.to_s, Regexp::MULTILINE)
428
426
  else
429
427
  actual.to_s =~ Regexp.new(expected.to_s, Regexp::MULTILINE)
@@ -435,7 +433,7 @@ module Parser::AST
435
433
  when NilClass
436
434
  actual.nil?
437
435
  when Numeric
438
- if Parser::AST::Node === actual
436
+ if actual.is_a?(Parser::AST::Node)
439
437
  actual.children[0] == expected
440
438
  else
441
439
  actual == expected
@@ -56,7 +56,8 @@ module Synvert::Core
56
56
  # @param name [String] the unique rewriter name.
57
57
  # @param rewriter [Synvert::Core::Rewriter] the rewriter to register.
58
58
  def register(group, name, rewriter)
59
- group, name = group.to_s, name.to_s
59
+ group = group.to_s
60
+ name = name.to_s
60
61
  rewriters[group] ||= {}
61
62
  rewriters[group][name] = rewriter
62
63
  end
@@ -68,7 +69,8 @@ module Synvert::Core
68
69
  # @return [Synvert::Core::Rewriter] the matching rewriter.
69
70
  # @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
70
71
  def fetch(group, name)
71
- group, name = group.to_s, name.to_s
72
+ group = group.to_s
73
+ name = name.to_s
72
74
  if exist? group, name
73
75
  rewriters[group][name]
74
76
  else
@@ -84,7 +86,8 @@ module Synvert::Core
84
86
  # @return [Synvert::Core::Rewriter] the registered rewriter.
85
87
  # @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
86
88
  def call(group, name, sandbox = false)
87
- group, name = group.to_s, name.to_s
89
+ group = group.to_s
90
+ name = name.to_s
88
91
  if exist? group, name
89
92
  rewriter = rewriters[group][name]
90
93
  if sandbox
@@ -104,7 +107,8 @@ module Synvert::Core
104
107
  # @param name [String] the rewriter name.
105
108
  # @return [Boolean] true if the rewriter exist.
106
109
  def exist?(group, name)
107
- group, name = group.to_s, name.to_s
110
+ group = group.to_s
111
+ name = name.to_s
108
112
  if rewriters[group] && rewriters[group][name]
109
113
  true
110
114
  else
@@ -141,7 +145,9 @@ module Synvert::Core
141
145
  # @return [Array] helper methods.
142
146
  # @!attribute [r] warnings
143
147
  # @return [Array<Synvert::Core::Rewriter::Warning>] warning messages.
144
- attr_reader :group, :name, :sub_snippets, :helpers, :warnings
148
+ # @!attribute [r] affected_files
149
+ # @return [Set] affected fileds
150
+ attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files
145
151
 
146
152
  # Initialize a rewriter.
147
153
  # When a rewriter is initialized, it is also registered.
@@ -157,13 +163,14 @@ module Synvert::Core
157
163
  @helpers = []
158
164
  @sub_snippets = []
159
165
  @warnings = []
166
+ @affected_files = Set.new
160
167
  self.class.register(@group, @name, self)
161
168
  end
162
169
 
163
170
  # Process the rewriter.
164
171
  # It will call the block.
165
172
  def process
166
- instance_eval &@block
173
+ instance_eval(&@block)
167
174
  end
168
175
 
169
176
  # Process rewriter with sandbox mode.
@@ -184,6 +191,13 @@ module Synvert::Core
184
191
  @warnings << warning
185
192
  end
186
193
 
194
+ # Add an affected file.
195
+ #
196
+ # @param file_path [String]
197
+ def add_affected_file(file_path)
198
+ @affected_files.add(file_path)
199
+ end
200
+
187
201
  #######
188
202
  # DSL #
189
203
  #######
@@ -23,11 +23,9 @@ module Synvert::Core
23
23
  def rewritten_code
24
24
  if rewritten_source.split("\n").length > 1
25
25
  new_code = []
26
- rewritten_source
27
- .split("\n")
28
- .each_with_index { |line, index|
29
- new_code << (index == 0 || !@options[:autoindent] ? line : indent(@node) + line)
30
- }
26
+ rewritten_source.split("\n").each_with_index { |line, index|
27
+ new_code << (index == 0 || !@options[:autoindent] ? line : indent(@node) + line)
28
+ }
31
29
  new_code.join("\n")
32
30
  else
33
31
  rewritten_source
@@ -17,7 +17,7 @@ module Synvert::Core
17
17
 
18
18
  # If condition matches, run the block code.
19
19
  def process
20
- @instance.instance_eval &@block if match?
20
+ @instance.instance_eval(&@block) if match?
21
21
  end
22
22
  end
23
23
  end
@@ -7,7 +7,7 @@ module Synvert::Core
7
7
  def match?
8
8
  match = false
9
9
  @instance.current_node.recursive_children do |child_node|
10
- match ||= (child_node&.match?(@rules))
10
+ match ||= child_node&.match?(@rules)
11
11
  end
12
12
  match
13
13
  end
@@ -7,7 +7,7 @@ module Synvert::Core
7
7
  def match?
8
8
  match = false
9
9
  @instance.current_node.recursive_children do |child_node|
10
- match ||= (child_node&.match?(@rules))
10
+ match ||= child_node&.match?(@rules)
11
11
  end
12
12
  !match
13
13
  end
@@ -12,7 +12,7 @@ module Synvert::Core
12
12
  # comparator key can be eq, lt, gt, lte, gte or ne.
13
13
  def initialize(name, comparator)
14
14
  @name = name
15
- if Hash === comparator
15
+ if comparator.is_a?(Hash)
16
16
  @operator = comparator.keys.first
17
17
  @version = Gem::Version.new comparator.values.first
18
18
  else
@@ -31,6 +31,7 @@ module Synvert::Core
31
31
  # if Gemfile.lock does not exist, just ignore this check
32
32
  return true unless File.exist?(gemfile_lock_path)
33
33
 
34
+ ENV['BUNDLE_GEMFILE'] = Configuration.path # make sure bundler reads Gemfile.lock in the correct path
34
35
  parser = Bundler::LockfileParser.new(File.read(gemfile_lock_path))
35
36
  if spec = parser.specs.find { |spec| spec.name == @name }
36
37
  Gem::Version.new(spec.version).send(OPERATORS[@operator], @version)
@@ -65,7 +65,23 @@ module Synvert::Core
65
65
  #
66
66
  # strip_brackets("(1..100)") #=> "1..100"
67
67
  def strip_brackets(code)
68
- code.sub(/^\((.*)\)$/) { $1 }.sub(/^\[(.*)\]$/) { $1 }.sub(/^{(.*)}$/) { $1 }
68
+ code.sub(/^\((.*)\)$/) { Regexp.last_match(1) }.sub(/^\[(.*)\]$/) { Regexp.last_match(1) }.sub(/^{(.*)}$/) {
69
+ Regexp.last_match(1)
70
+ }
71
+ end
72
+
73
+ # Reject some keys from hash node.
74
+ #
75
+ # @param hash_node [Parser::AST::Node]
76
+ # @param keys [Array] keys should be rejected from the hash.
77
+ # @return [String] source of of the hash node after rejecting some keys.
78
+ #
79
+ # @example
80
+ #
81
+ # hash_node = Parser::CurrentRuby.parse("{ key1: 'value1', key2: 'value2' }")
82
+ # reject_keys_from_hash(hash_node, :key1) => "key2: 'value2'"
83
+ def reject_keys_from_hash(hash_node, *keys)
84
+ hash_node.children.reject { |pair_node| keys.include?(pair_node.key.to_value) }.map(&:to_source).join(', ')
69
85
  end
70
86
  end
71
87
  end
@@ -18,7 +18,7 @@ module Synvert::Core
18
18
  @file_source[file_path] ||=
19
19
  begin
20
20
  source = File.read(file_path)
21
- source = Engine::ERB.encode(source) if file_path =~ /\.erb$/
21
+ source = Engine::ERB.encode(source) if /\.erb$/.match?(file_path)
22
22
  source
23
23
  end
24
24
  end
@@ -45,7 +45,7 @@ module Synvert::Core
45
45
  # @param file_path [String] file path
46
46
  # @param source [String] file source
47
47
  def write_file(file_path, source)
48
- source = Engine::ERB.decode(source) if file_path =~ /\.erb/
48
+ source = Engine::ERB.decode(source) if /\.erb/.match?(file_path)
49
49
  File.write file_path, source.gsub(/ +\n/, "\n")
50
50
  @file_source[file_path] = nil
51
51
  @file_ast[file_path] = nil
@@ -87,43 +87,41 @@ module Synvert::Core
87
87
  # and rewrite source code back to original file.
88
88
  def process
89
89
  file_pattern = File.join(Configuration.path, @file_pattern)
90
- Dir
91
- .glob(file_pattern)
92
- .each do |file_path|
93
- next if Configuration.skip_files.include? file_path
94
-
95
- begin
96
- conflict_actions = []
97
- source = +self.class.file_source(file_path)
98
- ast = self.class.file_ast(file_path)
99
-
100
- @current_file = file_path
101
-
102
- process_with_node ast do
103
- begin
104
- instance_eval &@block
105
- rescue NoMethodError
106
- puts @current_node.debug_info
107
- raise
108
- end
90
+ Dir.glob(file_pattern).each do |file_path|
91
+ next if Configuration.skip_files.include? file_path
92
+
93
+ begin
94
+ conflict_actions = []
95
+ source = +self.class.file_source(file_path)
96
+ ast = self.class.file_ast(file_path)
97
+
98
+ @current_file = file_path
99
+
100
+ process_with_node ast do
101
+ begin
102
+ instance_eval(&@block)
103
+ rescue NoMethodError
104
+ puts @current_node.debug_info
105
+ raise
109
106
  end
107
+ end
110
108
 
111
- if @actions.length > 0
112
- @actions.sort_by! { |action| action.send(@options[:sort_by]) }
113
- conflict_actions = get_conflict_actions
114
- @actions.reverse_each do |action|
115
- source[action.begin_pos...action.end_pos] = action.rewritten_code
116
- source = remove_code_or_whole_line(source, action.line)
117
- end
118
- @actions = []
119
-
120
- self.class.write_file(file_path, source)
109
+ if @actions.length > 0
110
+ @actions.sort_by! { |action| action.send(@options[:sort_by]) }
111
+ conflict_actions = get_conflict_actions
112
+ @actions.reverse_each do |action|
113
+ source[action.begin_pos...action.end_pos] = action.rewritten_code
114
+ source = remove_code_or_whole_line(source, action.line)
121
115
  end
122
- rescue Parser::SyntaxError
123
- puts "[Warn] file #{file_path} was not parsed correctly."
124
- # do nothing, iterate next file
125
- end while !conflict_actions.empty?
126
- end
116
+ @actions = []
117
+
118
+ update_file(file_path, source)
119
+ end
120
+ rescue Parser::SyntaxError
121
+ puts "[Warn] file #{file_path} was not parsed correctly."
122
+ # do nothing, iterate next file
123
+ end while !conflict_actions.empty?
124
+ end
127
125
  end
128
126
 
129
127
  # Gets current node, it allows to get current node in block code.
@@ -312,5 +310,14 @@ module Synvert::Core
312
310
  source
313
311
  end
314
312
  end
313
+
314
+ # It updates a file with new source code.
315
+ #
316
+ # @param file_path [String] the file path
317
+ # @param source [String] the new source code
318
+ def update_file(file_path, source)
319
+ self.class.write_file(file_path, source)
320
+ @rewriter.add_affected_file(file_path)
321
+ end
315
322
  end
316
323
  end
@@ -14,9 +14,7 @@ module Synvert::Core
14
14
  #
15
15
  # @return [Boolean] true if matches, otherwise false.
16
16
  def match?
17
- # Gem::Version initialize will strip RUBY_VERSION directly in ruby 1.9,
18
- # which is solved from ruby 2.0.0, which calls dup internally.
19
- Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new(@version)
17
+ Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(@version)
20
18
  end
21
19
  end
22
20
  end
@@ -21,7 +21,7 @@ module Synvert::Core
21
21
 
22
22
  child_node = current_node.send @child_node_name
23
23
  @instance.process_with_other_node child_node do
24
- @instance.instance_eval &@block
24
+ @instance.instance_eval(&@block)
25
25
  end
26
26
  end
27
27
  end
@@ -26,7 +26,7 @@ module Synvert::Core
26
26
  @instance.process_with_node current_node do
27
27
  matching_nodes.each do |matching_node|
28
28
  @instance.process_with_node matching_node do
29
- @instance.instance_eval &@block
29
+ @instance.instance_eval(&@block)
30
30
  end
31
31
  end
32
32
  end
@@ -41,6 +41,14 @@ module Synvert::Core
41
41
  current_node.recursive_children do |child_node|
42
42
  matching_nodes << child_node if child_node.match? @rules
43
43
  end
44
+ elsif current_node.is_a?(Parser::AST::Node)
45
+ if current_node.type == :begin
46
+ current_node.children.each do |child_node|
47
+ matching_nodes << child_node if child_node.match? @rules
48
+ end
49
+ elsif current_node.match? @rules
50
+ matching_nodes << current_node
51
+ end
44
52
  else
45
53
  current_node.each do |child_node|
46
54
  matching_nodes << child_node if child_node.match? @rules
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.20.0'
5
+ VERSION = '0.22.0'
6
6
  end
7
7
  end
@@ -295,7 +295,7 @@ describe Parser::AST::Node do
295
295
 
296
296
  it 'get for range' do
297
297
  node = parse('(1..10)')
298
- expect(node.to_value).to eq (1..10)
298
+ expect(node.to_value).to eq(1..10)
299
299
  end
300
300
 
301
301
  it 'gets for array' do
@@ -80,5 +80,17 @@ module Synvert::Core
80
80
  expect(dummy_instance.strip_brackets('(123]')).to eq '(123]'
81
81
  end
82
82
  end
83
+
84
+ describe '#reject_keys_from_hash' do
85
+ it 'rejects single key' do
86
+ hash_node = Parser::CurrentRuby.parse("{ key1: 'value1', key2: 'value2' }")
87
+ expect(dummy_instance.reject_keys_from_hash(hash_node, :key1)).to eq "key2: 'value2'"
88
+ end
89
+
90
+ it 'rejects multi keys' do
91
+ hash_node = Parser::CurrentRuby.parse("{ key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4' }")
92
+ expect(dummy_instance.reject_keys_from_hash(hash_node, :key1, :key3)).to eq "key2: 'value2', key4: 'value4'"
93
+ end
94
+ end
83
95
  end
84
96
  end
@@ -243,6 +243,7 @@ end
243
243
  expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(output)
244
244
  instance.process
245
245
  instance.process
246
+ expect(rewriter.affected_files).to be_include('spec/models/post_spec.rb')
246
247
  end
247
248
  end
248
249
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Synvert::Core
6
+ describe Rewriter::RubyVersion do
7
+ it 'returns true if ruby version is greater than 1.9' do
8
+ ruby_version = Rewriter::RubyVersion.new('1.9')
9
+ expect(ruby_version).to be_match
10
+ end
11
+
12
+ it 'returns false if ruby version is less than 19.0' do
13
+ ruby_version = Rewriter::RubyVersion.new('19.0')
14
+ expect(ruby_version).not_to be_match
15
+ end
16
+ end
17
+ end
data/synvert-core.gemspec CHANGED
@@ -1,5 +1,6 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'synvert/core/version'
5
6
 
@@ -8,8 +9,8 @@ Gem::Specification.new do |spec|
8
9
  spec.version = Synvert::Core::VERSION
9
10
  spec.authors = ["Richard Huang"]
10
11
  spec.email = ["flyerhzm@gmail.com"]
11
- spec.summary = %q{convert ruby code to better syntax.}
12
- spec.description = %q{convert ruby code to better syntax automatically.}
12
+ spec.summary = 'convert ruby code to better syntax.'
13
+ spec.description = 'convert ruby code to better syntax automatically.'
13
14
  spec.homepage = "https://github.com/xinminlabs/synvert-core"
14
15
  spec.license = "MIT"
15
16
 
@@ -18,13 +19,13 @@ Gem::Specification.new do |spec|
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
20
  spec.require_paths = ["lib"]
20
21
 
21
- spec.add_runtime_dependency "parser", "~> 3.0.0"
22
22
  spec.add_runtime_dependency "activesupport"
23
23
  spec.add_runtime_dependency "erubis"
24
+ spec.add_runtime_dependency "parser", "~> 3.0.0"
24
25
 
25
26
  spec.add_development_dependency "bundler"
26
- spec.add_development_dependency "rake"
27
- spec.add_development_dependency "rspec"
28
27
  spec.add_development_dependency "guard"
29
28
  spec.add_development_dependency "guard-rspec"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec"
30
31
  end
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.0
4
+ version: 0.22.0
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-02-15 00:00:00.000000000 Z
11
+ date: 2021-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: parser
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
19
+ version: '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: 3.0.0
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activesupport
28
+ name: erubis
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: erubis
42
+ name: parser
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 3.0.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 3.0.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake
70
+ name: guard
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rspec
84
+ name: guard-rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: guard
98
+ name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: guard-rspec
112
+ name: rspec
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -183,6 +183,7 @@ files:
183
183
  - spec/synvert/core/rewriter/gem_spec_spec.rb
184
184
  - spec/synvert/core/rewriter/helper_spec.rb
185
185
  - spec/synvert/core/rewriter/instance_spec.rb
186
+ - spec/synvert/core/rewriter/ruby_version_spec.rb
186
187
  - spec/synvert/core/rewriter/scope/goto_scope_spec.rb
187
188
  - spec/synvert/core/rewriter/scope/within_scope.rb
188
189
  - spec/synvert/core/rewriter/scope_spec.rb
@@ -208,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
209
  - !ruby/object:Gem::Version
209
210
  version: '0'
210
211
  requirements: []
211
- rubygems_version: 3.1.4
212
+ rubygems_version: 3.0.3
212
213
  signing_key:
213
214
  specification_version: 4
214
215
  summary: convert ruby code to better syntax.
@@ -231,6 +232,7 @@ test_files:
231
232
  - spec/synvert/core/rewriter/gem_spec_spec.rb
232
233
  - spec/synvert/core/rewriter/helper_spec.rb
233
234
  - spec/synvert/core/rewriter/instance_spec.rb
235
+ - spec/synvert/core/rewriter/ruby_version_spec.rb
234
236
  - spec/synvert/core/rewriter/scope/goto_scope_spec.rb
235
237
  - spec/synvert/core/rewriter/scope/within_scope.rb
236
238
  - spec/synvert/core/rewriter/scope_spec.rb