synvert-core 0.54.0 → 0.55.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c7ee99e848625ed3bad729d3e08186370d5fb58d4412fb1d09d89bf15559a22
4
- data.tar.gz: 30f16d4aa02a2a6f6677a5657b24296127869e17c5237a4b63f2d6d1ed658203
3
+ metadata.gz: 0afc5cb58f822a56343dfba1c4f2c762e52db5d4eefb3d88eb1b72788a97a8b3
4
+ data.tar.gz: 56c67dd89f69ba0dd90a91818740c0f70b18d3fe4887e50fed37c7eea736b18f
5
5
  SHA512:
6
- metadata.gz: 936f304f6b829be86eb1da55aa110a7b3ed7c249cb9da4a1bc5246a1d687fb8dd7548fe68a2632c4df874c921f829e05ec5cab838373b587db5a3e59b900a0ab
7
- data.tar.gz: 15f39e1386ee166762dd3afca86b8d246e3cc8ea0cbdcb5efd15eb9141f911ffcaabaf15a6062f9a6fe31dc720e7242fe4a81633bc12ffa06c18efe431d4d410
6
+ metadata.gz: 6e9270a5518350d8bdab54a9548047cb678e72eeb5702be9857852522a796d70573ce958fca00f75df776b707bd021ea3f4c7b7720f69755f6b862c4fc70d32f
7
+ data.tar.gz: 57a6ac61ee0724c2c6a10524192b4911d65c3cb977104720a1a2da6e5fcd2d967030aaddf8df1605037343620ef1ec9878943fc23935a2a656f7a0cc0672a173
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.55.0 (2021-09-11)
4
+
5
+ * Add `Configuration.show_run_process`
6
+ * Fix remove action `begin_pos` and `end_pos`
7
+ * Fix `nil` match
8
+ * Rewrite `remove` action
9
+
3
10
  ## 0.54.0 (2021-08-28)
4
11
 
5
12
  * Change `goto_scope` param from array to string
@@ -4,7 +4,7 @@ module Synvert::Core
4
4
  # Synvert global configuration.
5
5
  class Configuration
6
6
  class << self
7
- attr_writer :path, :skip_files
7
+ attr_writer :path, :skip_files, :show_run_process
8
8
 
9
9
  def path
10
10
  @path || '.'
@@ -13,6 +13,10 @@ module Synvert::Core
13
13
  def skip_files
14
14
  @skip_files || []
15
15
  end
16
+
17
+ def show_run_process
18
+ @show_run_process || false
19
+ end
16
20
  end
17
21
  end
18
22
  end
@@ -576,7 +576,11 @@ module Parser::AST
576
576
 
577
577
  actual.zip(expected).all? { |a, e| match_value?(a, e) }
578
578
  when NilClass
579
- actual.nil?
579
+ if actual.is_a?(Parser::AST::Node)
580
+ :nil == actual.type
581
+ else
582
+ actual.nil?
583
+ end
580
584
  when Numeric
581
585
  if actual.is_a?(Parser::AST::Node)
582
586
  actual.children[0] == expected
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
- # DeleteAction to delete code.
4
+ # DeleteAction to delete child nodes.
5
5
  class Rewriter::DeleteAction < Rewriter::Action
6
6
  def initialize(instance, *selectors)
7
7
  super(instance, nil)
@@ -1,29 +1,57 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
- # RemoveAction to remove code.
4
+ # RemoveAction to remove current node.
5
5
  class Rewriter::RemoveAction < Rewriter::Action
6
- def initialize(instance, code = nil)
7
- super
6
+ def initialize(instance)
7
+ super(instance, nil)
8
8
  end
9
9
 
10
10
  # Begin position of code to replace.
11
11
  #
12
12
  # @return [Integer] begin position.
13
13
  def begin_pos
14
- @node.loc.expression.begin_pos
14
+ if take_whole_line?
15
+ start_index
16
+ else
17
+ @node.loc.expression.begin_pos
18
+ end
15
19
  end
16
20
 
17
21
  # End position of code to replace.
18
22
  #
19
23
  # @return [Integer] end position.
20
24
  def end_pos
21
- @node.loc.expression.end_pos
25
+ if take_whole_line?
26
+ end_index
27
+ else
28
+ @node.loc.expression.end_pos
29
+ end
22
30
  end
23
31
 
24
32
  # The rewritten code, always empty string.
25
33
  def rewritten_code
26
34
  ''
27
35
  end
36
+
37
+ private
38
+
39
+ def take_whole_line?
40
+ @node.to_source == file_source[start_index...end_index].strip
41
+ end
42
+
43
+ def start_index
44
+ index = file_source[0..@node.loc.expression.begin_pos].rindex("\n")
45
+ index ? index + "\n".length : @node.loc.expression.begin_pos
46
+ end
47
+
48
+ def end_index
49
+ index = file_source[@node.loc.expression.end_pos..-1].index("\n")
50
+ index ? @node.loc.expression.end_pos + index + "\n".length : @node.loc.expression.end_pos
51
+ end
52
+
53
+ def file_source
54
+ @file_source ||= @instance.file_source
55
+ end
28
56
  end
29
57
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
- # IfExistCondition checks if node has only one child node and the child node matches rules.
4
+ # IfOnlyExistCondition checks if node has only one child node and the child node matches rules.
5
5
  class Rewriter::IfOnlyExistCondition < Rewriter::Condition
6
6
  # check if only have one child node and the child node matches rules.
7
7
  def match?
@@ -64,6 +64,11 @@ module Synvert::Core
64
64
  # @return current filename
65
65
  attr_accessor :current_node, :current_file
66
66
 
67
+ # Current file source
68
+ def file_source
69
+ self.class.file_source(current_file)
70
+ end
71
+
67
72
  # Initialize an instance.
68
73
  #
69
74
  # @param rewriter [Synvert::Core::Rewriter]
@@ -87,6 +92,7 @@ module Synvert::Core
87
92
  next if Configuration.skip_files.include? file_path
88
93
 
89
94
  begin
95
+ puts file_path if Configuration.show_run_process
90
96
  conflict_actions = []
91
97
  source = +self.class.file_source(file_path)
92
98
  ast = self.class.file_ast(file_path)
@@ -107,7 +113,6 @@ module Synvert::Core
107
113
  conflict_actions = get_conflict_actions
108
114
  @actions.reverse_each do |action|
109
115
  source[action.begin_pos...action.end_pos] = action.rewritten_code
110
- source = remove_code_or_whole_line(source, action.line)
111
116
  end
112
117
  @actions = []
113
118
 
@@ -323,25 +328,6 @@ module Synvert::Core
323
328
  conflict_actions
324
329
  end
325
330
 
326
- # It checks if code is removed and that line is empty.
327
- #
328
- # @param source [String] source code of file
329
- # @param line [String] the line number
330
- def remove_code_or_whole_line(source, line)
331
- newline_at_end_of_line = source[-1] == "\n"
332
- source_arr = source.split("\n")
333
- if source_arr[line - 1] && source_arr[line - 1].strip.empty?
334
- source_arr.delete_at(line - 1)
335
- if source_arr[line - 2] && source_arr[line - 2].strip.empty? && source_arr[line - 1] &&
336
- source_arr[line - 1].strip.empty?
337
- source_arr.delete_at(line - 1)
338
- end
339
- source_arr.join("\n") + (newline_at_end_of_line ? "\n" : '')
340
- else
341
- source
342
- end
343
- end
344
-
345
331
  # It updates a file with new source code.
346
332
  #
347
333
  # @param file_path [String] the file path
@@ -81,7 +81,7 @@ module Synvert::Core
81
81
  def fetch(group, name)
82
82
  group = group.to_s
83
83
  name = name.to_s
84
- if exist? group, name
84
+ if rewriters[group] && rewriters[group][name]
85
85
  rewriters[group][name]
86
86
  else
87
87
  raise RewriterNotFound, "Rewriter #{group} #{name} not found"
@@ -96,34 +96,13 @@ module Synvert::Core
96
96
  # @return [Synvert::Core::Rewriter] the registered rewriter.
97
97
  # @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
98
98
  def call(group, name, sandbox = false)
99
- group = group.to_s
100
- name = name.to_s
101
- if exist? group, name
102
- rewriter = rewriters[group][name]
103
- if sandbox
104
- rewriter.process_with_sandbox
105
- else
106
- rewriter.process
107
- end
108
- rewriter
109
- else
110
- raise RewriterNotFound, "Rewriter #{group}/#{name} not found"
111
- end
112
- end
113
-
114
- # Check if one rewriter exist.
115
- #
116
- # @param group [String] the rewriter group.
117
- # @param name [String] the rewriter name.
118
- # @return [Boolean] true if the rewriter exist.
119
- def exist?(group, name)
120
- group = group.to_s
121
- name = name.to_s
122
- if rewriters[group] && rewriters[group][name]
123
- true
99
+ rewriter = fetch(group, name)
100
+ if sandbox
101
+ rewriter.process_with_sandbox
124
102
  else
125
- false
103
+ rewriter.process
126
104
  end
105
+ rewriter
127
106
  end
128
107
 
129
108
  # Get all available rewriters
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.54.0'
5
+ VERSION = '0.55.0'
6
6
  end
7
7
  end
@@ -7,7 +7,7 @@ module Synvert::Core
7
7
  subject {
8
8
  source = "user = User.new params[:user]\nuser.save\nrender\n"
9
9
  send_node = Parser::CurrentRuby.parse(source).children[1]
10
- instance = double(current_node: send_node)
10
+ instance = double(current_node: send_node, file_source: source)
11
11
  Rewriter::RemoveAction.new(instance)
12
12
  }
13
13
 
@@ -16,7 +16,7 @@ module Synvert::Core
16
16
  end
17
17
 
18
18
  it 'gets end_pos' do
19
- expect(subject.end_pos).to eq "user = User.new params[:user]\nuser.save".length
19
+ expect(subject.end_pos).to eq "user = User.new params[:user]\nuser.save\n".length
20
20
  end
21
21
 
22
22
  it 'gets rewritten_code' do
@@ -54,9 +54,9 @@ module Synvert::Core
54
54
  it 'parses goto_node' do
55
55
  scope = double
56
56
  block = proc {}
57
- expect(Rewriter::GotoScope).to receive(:new).with(instance, :caller, :receiver, &block).and_return(scope)
57
+ expect(Rewriter::GotoScope).to receive(:new).with(instance, 'caller.receiver', &block).and_return(scope)
58
58
  expect(scope).to receive(:process)
59
- instance.goto_node(:caller, :receiver, &block)
59
+ instance.goto_node('caller.receiver', &block)
60
60
  end
61
61
 
62
62
  it 'parses if_exist_node' do
@@ -249,17 +249,6 @@ module Synvert::Core
249
249
  expect { Rewriter.call 'group', 'rewriter' }.to raise_error(RewriterNotFound)
250
250
  end
251
251
 
252
- context 'exist?' do
253
- it 'returns true if rewriter exists' do
254
- Rewriter.new 'group', 'rewriter'
255
- expect(Rewriter.exist?('group', 'rewriter')).to be_truthy
256
- end
257
-
258
- it 'returns false if rewriter does not exist' do
259
- expect(Rewriter.exist?('group', 'rewriter')).to be_falsey
260
- end
261
- end
262
-
263
252
  context 'available' do
264
253
  it 'lists empty rewriters' do
265
254
  expect(Rewriter.availables).to eq({})
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.54.0
4
+ version: 0.55.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-08-28 00:00:00.000000000 Z
11
+ date: 2021-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport