synvert-core 0.54.0 → 0.54.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/synvert/core/rewriter/action/delete_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/remove_action.rb +31 -5
- data/lib/synvert/core/rewriter/instance.rb +5 -20
- data/lib/synvert/core/rewriter.rb +6 -27
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +2 -2
- data/spec/synvert/core/rewriter/instance_spec.rb +2 -2
- data/spec/synvert/core/rewriter_spec.rb +0 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dce993d0b27272e83abe3bd3a1ff2a9917a2c9953bb30e57671216aed001292c
|
4
|
+
data.tar.gz: 0b65093ca670d59065707e122a9964f163fbca2e4201fd75005295b7841e4db1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bb49555a37b7778e37216f267b0e2ab938030ac5428f7bbef60d8462fb83597693bad9a5cd12c43c52a80330de48662151abbcc8edebb7572538f1520539bac
|
7
|
+
data.tar.gz: eaf5690913c21c3b043f5119f2b8b4afe08c16c5a9db8d9d209a42f0a64a72d4f19376da6dee6ed43b04b79d8077f4463a47acb5d18742a6dcb1499b3191db7a
|
data/CHANGELOG.md
CHANGED
@@ -1,29 +1,55 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
# RemoveAction to remove
|
4
|
+
# RemoveAction to remove current node.
|
5
5
|
class Rewriter::RemoveAction < Rewriter::Action
|
6
|
-
def initialize(instance
|
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
|
-
|
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
|
-
|
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
|
+
file_source[0..@node.loc.expression.begin_pos].rindex("\n") + "\n".length
|
45
|
+
end
|
46
|
+
|
47
|
+
def end_index
|
48
|
+
file_source[@node.loc.expression.end_pos..-1].index("\n") + @node.loc.expression.end_pos + "\n".length
|
49
|
+
end
|
50
|
+
|
51
|
+
def file_source
|
52
|
+
@file_source ||= @instance.file_source
|
53
|
+
end
|
28
54
|
end
|
29
55
|
end
|
@@ -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]
|
@@ -107,7 +112,6 @@ module Synvert::Core
|
|
107
112
|
conflict_actions = get_conflict_actions
|
108
113
|
@actions.reverse_each do |action|
|
109
114
|
source[action.begin_pos...action.end_pos] = action.rewritten_code
|
110
|
-
source = remove_code_or_whole_line(source, action.line)
|
111
115
|
end
|
112
116
|
@actions = []
|
113
117
|
|
@@ -323,25 +327,6 @@ module Synvert::Core
|
|
323
327
|
conflict_actions
|
324
328
|
end
|
325
329
|
|
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
330
|
# It updates a file with new source code.
|
346
331
|
#
|
347
332
|
# @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
|
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
|
-
|
100
|
-
|
101
|
-
|
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
|
-
|
103
|
+
rewriter.process
|
126
104
|
end
|
105
|
+
rewriter
|
127
106
|
end
|
128
107
|
|
129
108
|
# Get all available rewriters
|
data/lib/synvert/core/version.rb
CHANGED
@@ -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,
|
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(
|
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.
|
4
|
+
version: 0.54.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-08
|
11
|
+
date: 2021-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|