synvert-core 0.4.3 → 0.5.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 +4 -4
- data/CHANGELOG.md +4 -12
- data/lib/synvert/core/rewriter/helper.rb +21 -0
- data/lib/synvert/core/rewriter/instance.rb +70 -40
- data/lib/synvert/core/rewriter.rb +27 -18
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +2 -2
- data/spec/synvert/core/rewriter/helper_spec.rb +30 -0
- data/spec/synvert/core/rewriter/instance_spec.rb +58 -70
- data/spec/synvert/core/rewriter_spec.rb +29 -29
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 908d88152aaea79eafba824db6d91351f6b02394
|
4
|
+
data.tar.gz: b68026829b49fd6a6b78295f8c8abae77a0812b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5bbe05c11edcc9b1f4688f962850851b07a9e5a2a01871f8455a3213823ff15a565c686e73eae92f6045eba29f810993a2e4a2ab1cecc5340b5838e707b728f
|
7
|
+
data.tar.gz: 5808b8122edd185de72864ac178e64bae3888a8be284774bd3e5bc274dfebc43d6280978d98d4d91e8046503f5dd94c4bfe06bd8149e6cf766f529f792ddcb1f
|
data/CHANGELOG.md
CHANGED
@@ -1,20 +1,12 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.5.0 (2014-08-21)
|
4
4
|
|
5
|
+
* Add group to rewriter
|
5
6
|
* Add parent_class for :class node
|
6
|
-
* Move process_with_node to instance
|
7
|
-
* Add Instance#process_with_other_node
|
8
|
-
* Fix indent for append action
|
9
|
-
|
10
|
-
## 0.4.2
|
11
|
-
|
12
|
-
* Do not rewrite code in {{ }} in synvert can't rewrite it
|
13
|
-
* Fix indent for replace_with action
|
14
|
-
|
15
|
-
## 0.4.1
|
16
|
-
|
17
7
|
* Add Rewriter::Helper module to provide common helper methods.
|
8
|
+
* Fix indent for append and replace_with action
|
9
|
+
* Cache file source and ast
|
18
10
|
|
19
11
|
## 0.4.0 (2014-07-26)
|
20
12
|
|
@@ -35,5 +35,26 @@ module Synvert::Core
|
|
35
35
|
.sub(/^\[(.*)\]$/) { $1 }
|
36
36
|
.sub(/^{(.*)}$/) { $1 }
|
37
37
|
end
|
38
|
+
|
39
|
+
# Set current_node to node and process.
|
40
|
+
#
|
41
|
+
# @param node [Parser::AST::Node] node set to current_node
|
42
|
+
# @yield process
|
43
|
+
def process_with_node(node)
|
44
|
+
self.current_node = node
|
45
|
+
yield
|
46
|
+
self.current_node = node
|
47
|
+
end
|
48
|
+
|
49
|
+
# Set current_node properly, process and set current_node back to original current_node.
|
50
|
+
#
|
51
|
+
# @param node [Parser::AST::Node] node set to current_node
|
52
|
+
# @yield process
|
53
|
+
def process_with_other_node(node)
|
54
|
+
original_node = self.current_node
|
55
|
+
self.current_node = node
|
56
|
+
yield
|
57
|
+
self.current_node = original_node
|
58
|
+
end
|
38
59
|
end
|
39
60
|
end
|
@@ -9,7 +9,57 @@ module Synvert::Core
|
|
9
9
|
include Rewriter::Helper
|
10
10
|
|
11
11
|
class <<self
|
12
|
+
# @!attribute [rw] current
|
13
|
+
# @return current instance
|
12
14
|
attr_accessor :current
|
15
|
+
|
16
|
+
# Cached file source.
|
17
|
+
#
|
18
|
+
# @param file_path [String] file path
|
19
|
+
# @return [String] file source
|
20
|
+
def file_source(file_path)
|
21
|
+
@file_source ||= {}
|
22
|
+
@file_source[file_path] ||=
|
23
|
+
begin
|
24
|
+
source = File.read(file_path)
|
25
|
+
source = Engine::ERB.encode(source) if file_path =~ /\.erb$/
|
26
|
+
source
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Cached file ast.
|
31
|
+
#
|
32
|
+
# @param file_path [String] file path
|
33
|
+
# @return [String] ast node for file
|
34
|
+
def file_ast(file_path)
|
35
|
+
@file_ast ||= {}
|
36
|
+
@file_ast[file_path] ||=
|
37
|
+
begin
|
38
|
+
buffer = Parser::Source::Buffer.new file_path
|
39
|
+
buffer.source = file_source(file_path)
|
40
|
+
|
41
|
+
parser = Parser::CurrentRuby.new
|
42
|
+
parser.reset
|
43
|
+
parser.parse buffer
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Write source to file and remove cached file source and ast.
|
48
|
+
#
|
49
|
+
# @param file_path [String] file path
|
50
|
+
# @param source [String] file source
|
51
|
+
def write_file(file_path, source)
|
52
|
+
source = Engine::ERB.decode(source) if file_path =~ /\.erb/
|
53
|
+
File.write file_path, source
|
54
|
+
@file_source[file_path] = nil
|
55
|
+
@file_ast[file_path] = nil
|
56
|
+
end
|
57
|
+
|
58
|
+
# Reset cached file source and ast.
|
59
|
+
def reset
|
60
|
+
@file_source = {}
|
61
|
+
@file_ast = {}
|
62
|
+
end
|
13
63
|
end
|
14
64
|
|
15
65
|
# @!attribute [rw] current_node
|
@@ -40,36 +90,33 @@ module Synvert::Core
|
|
40
90
|
def process
|
41
91
|
self.class.current = self
|
42
92
|
|
43
|
-
parser = Parser::CurrentRuby.new
|
44
93
|
file_pattern = File.join(Configuration.instance.get(:path), @file_pattern)
|
45
94
|
Dir.glob(file_pattern).each do |file_path|
|
46
95
|
unless Configuration.instance.get(:skip_files).include? file_path
|
47
96
|
begin
|
48
|
-
|
49
|
-
source =
|
50
|
-
|
51
|
-
buffer.source = source
|
52
|
-
|
53
|
-
parser.reset
|
54
|
-
ast = parser.parse buffer
|
97
|
+
conflict_actions = []
|
98
|
+
source = self.class.file_source(file_path)
|
99
|
+
ast = self.class.file_ast(file_path)
|
55
100
|
|
56
101
|
@current_file = file_path
|
57
102
|
@current_source = source
|
103
|
+
|
58
104
|
self.process_with_node ast do
|
59
105
|
instance_eval &@block
|
60
106
|
end
|
61
107
|
|
62
|
-
@actions.
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
108
|
+
if @actions.length > 0
|
109
|
+
@actions.sort!
|
110
|
+
conflict_actions = get_conflict_actions
|
111
|
+
@actions.reverse.each do |action|
|
112
|
+
source[action.begin_pos...action.end_pos] = action.rewritten_code
|
113
|
+
source = remove_code_or_whole_line(source, action.line)
|
114
|
+
end
|
115
|
+
@actions = []
|
69
116
|
|
70
|
-
|
71
|
-
|
72
|
-
end while
|
117
|
+
self.class.write_file(file_path, source)
|
118
|
+
end
|
119
|
+
end while !conflict_actions.empty?
|
73
120
|
end
|
74
121
|
end
|
75
122
|
end
|
@@ -174,44 +221,27 @@ module Synvert::Core
|
|
174
221
|
@rewriter.add_warning Rewriter::Warning.new(self, message)
|
175
222
|
end
|
176
223
|
|
177
|
-
# Set current_node to node and process.
|
178
|
-
# @param node [Parser::AST::Node] node set to current_node
|
179
|
-
def process_with_node(node)
|
180
|
-
self.current_node = node
|
181
|
-
yield
|
182
|
-
self.current_node = node
|
183
|
-
end
|
184
|
-
|
185
|
-
# Set current_node properly, process and set current_node back to original current_node.
|
186
|
-
# @param node [Parser::AST::Node] node set to current_node
|
187
|
-
def process_with_other_node(node)
|
188
|
-
original_node = self.current_node
|
189
|
-
self.current_node = node
|
190
|
-
yield
|
191
|
-
self.current_node = original_node
|
192
|
-
end
|
193
|
-
|
194
224
|
private
|
195
225
|
|
196
|
-
# It changes source code from bottom to top, and it can change source code twice at the same time
|
226
|
+
# It changes source code from bottom to top, and it can change source code twice at the same time,
|
197
227
|
# So if there is an overlap between two actions, it removes the conflict actions and operate them in the next loop.
|
198
|
-
def
|
228
|
+
def get_conflict_actions
|
199
229
|
i = @actions.length - 1
|
200
230
|
j = i - 1
|
201
|
-
|
231
|
+
conflict_actions = []
|
202
232
|
return if i < 0
|
203
233
|
|
204
234
|
begin_pos = @actions[i].begin_pos
|
205
235
|
while j > -1
|
206
236
|
if begin_pos <= @actions[j].end_pos
|
207
|
-
|
237
|
+
conflict_actions << @actions.delete_at(j)
|
208
238
|
else
|
209
239
|
i = j
|
210
240
|
begin_pos = @actions[i].begin_pos
|
211
241
|
end
|
212
242
|
j -= 1
|
213
243
|
end
|
214
|
-
|
244
|
+
conflict_actions
|
215
245
|
end
|
216
246
|
|
217
247
|
# It checks if code is removed and that line is empty.
|
@@ -40,42 +40,46 @@ module Synvert::Core
|
|
40
40
|
autoload :GemSpec, 'synvert/core/rewriter/gem_spec'
|
41
41
|
|
42
42
|
class <<self
|
43
|
-
# Register a rewriter with its name.
|
43
|
+
# Register a rewriter with its group and name.
|
44
44
|
#
|
45
|
+
# @param group [String] the rewriter group.
|
45
46
|
# @param name [String] the unique rewriter name.
|
46
47
|
# @param rewriter [Synvert::Core::Rewriter] the rewriter to register.
|
47
|
-
def register(name, rewriter)
|
48
|
+
def register(group, name, rewriter)
|
48
49
|
@rewriters ||= {}
|
49
|
-
@rewriters[
|
50
|
+
@rewriters[group] ||= {}
|
51
|
+
@rewriters[group][name] = rewriter
|
50
52
|
end
|
51
53
|
|
52
|
-
# Fetch a rewriter by name.
|
54
|
+
# Fetch a rewriter by group and name.
|
53
55
|
#
|
56
|
+
# @param group [String] rewrtier group.
|
54
57
|
# @param name [String] rewrtier name.
|
55
58
|
# @return [Synvert::Core::Rewriter] the matching rewriter.
|
56
|
-
def fetch(name)
|
57
|
-
@rewriters[name]
|
59
|
+
def fetch(group, name)
|
60
|
+
@rewriters[group][name]
|
58
61
|
end
|
59
62
|
|
60
|
-
# Get a registered rewriter by
|
63
|
+
# Get a registered rewriter by group and name, then process that rewriter.
|
61
64
|
#
|
65
|
+
# @param group [String] the rewriter group.
|
62
66
|
# @param name [String] the rewriter name.
|
63
67
|
# @return [Synvert::Core::Rewriter] the registered rewriter.
|
64
68
|
# @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
|
65
|
-
def call(name)
|
66
|
-
if (rewriter = @rewriters[name])
|
69
|
+
def call(group, name)
|
70
|
+
if (rewriter = @rewriters[group][name])
|
67
71
|
rewriter.process
|
68
72
|
rewriter
|
69
73
|
else
|
70
|
-
raise RewriterNotFound.new "Rewriter #{name} not found"
|
74
|
+
raise RewriterNotFound.new "Rewriter #{group} #{name} not found"
|
71
75
|
end
|
72
76
|
end
|
73
77
|
|
74
78
|
# Get all available rewriters
|
75
79
|
#
|
76
|
-
# @return [
|
80
|
+
# @return [Hash<String, Hash<String, Rewriter>>]
|
77
81
|
def availables
|
78
|
-
@rewriters
|
82
|
+
@rewriters
|
79
83
|
end
|
80
84
|
|
81
85
|
# Clear all registered rewriters.
|
@@ -84,6 +88,8 @@ module Synvert::Core
|
|
84
88
|
end
|
85
89
|
end
|
86
90
|
|
91
|
+
# @!attribute [r] group
|
92
|
+
# @return [String] the group of rewriter
|
87
93
|
# @!attribute [r] name
|
88
94
|
# @return [String] the unique name of rewriter
|
89
95
|
# @!attribute [r] sub_snippets
|
@@ -92,21 +98,23 @@ module Synvert::Core
|
|
92
98
|
# @return [Array] helper methods.
|
93
99
|
# @!attribute [r] warnings
|
94
100
|
# @return [Array<Synvert::Core::Rewriter::Warning>] warning messages.
|
95
|
-
attr_reader :name, :sub_snippets, :helpers, :warnings
|
101
|
+
attr_reader :group, :name, :sub_snippets, :helpers, :warnings
|
96
102
|
|
97
103
|
# Initialize a rewriter.
|
98
104
|
# When a rewriter is initialized, it is also registered.
|
99
105
|
#
|
106
|
+
# @param group [String] group of the rewriter.
|
100
107
|
# @param name [String] name of the rewriter.
|
101
108
|
# @param block [Block] a block defines the behaviors of the rewriter, block code won't be called when initialization.
|
102
109
|
# @return [Synvert::Core::Rewriter]
|
103
|
-
def initialize(name, &block)
|
104
|
-
@
|
110
|
+
def initialize(group, name, &block)
|
111
|
+
@group = group
|
112
|
+
@name = name
|
105
113
|
@block = block
|
106
114
|
@helpers = []
|
107
115
|
@sub_snippets = []
|
108
116
|
@warnings = []
|
109
|
-
self.class.register(@name, self)
|
117
|
+
self.class.register(@group, @name, self)
|
110
118
|
end
|
111
119
|
|
112
120
|
# Process the rewriter.
|
@@ -196,9 +204,10 @@ module Synvert::Core
|
|
196
204
|
|
197
205
|
# Parse add_snippet dsl, it calls anther rewriter.
|
198
206
|
#
|
207
|
+
# @param group [String] group of another rewriter.
|
199
208
|
# @param name [String] name of another rewriter.
|
200
|
-
def add_snippet(name)
|
201
|
-
@sub_snippets << self.class.call(name.to_s)
|
209
|
+
def add_snippet(group, name)
|
210
|
+
@sub_snippets << self.class.call(group.to_s, name.to_s)
|
202
211
|
end
|
203
212
|
|
204
213
|
# Parse helper_method dsl, it defines helper method for [Synvert::Core::Rewriter::Instance].
|
data/lib/synvert/core/version.rb
CHANGED
@@ -220,7 +220,7 @@ describe Parser::AST::Node do
|
|
220
220
|
|
221
221
|
describe '#match?' do
|
222
222
|
let(:instance) {
|
223
|
-
rewriter = Synvert::Rewriter.new('
|
223
|
+
rewriter = Synvert::Rewriter.new('foo', 'bar')
|
224
224
|
Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
|
225
225
|
}
|
226
226
|
before { Synvert::Rewriter::Instance.current = instance }
|
@@ -277,7 +277,7 @@ describe Parser::AST::Node do
|
|
277
277
|
|
278
278
|
describe '#rewritten_source' do
|
279
279
|
let(:instance) {
|
280
|
-
rewriter = Synvert::Rewriter.new('
|
280
|
+
rewriter = Synvert::Rewriter.new('foo', 'bar')
|
281
281
|
Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
|
282
282
|
}
|
283
283
|
before { Synvert::Rewriter::Instance.current = instance }
|
@@ -3,6 +3,10 @@ require 'spec_helper'
|
|
3
3
|
module Synvert::Core
|
4
4
|
describe Rewriter::Helper do
|
5
5
|
let(:dummy_instance) { Class.new { include Rewriter::Helper }.new }
|
6
|
+
let(:instance) do
|
7
|
+
rewriter = Rewriter.new('foo', 'bar')
|
8
|
+
Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do; end
|
9
|
+
end
|
6
10
|
|
7
11
|
describe "add_receiver_if_necessary" do
|
8
12
|
context "with receiver" do
|
@@ -41,5 +45,31 @@ module Synvert::Core
|
|
41
45
|
expect(dummy_instance.strip_brackets("(123]")).to eq "(123]"
|
42
46
|
end
|
43
47
|
end
|
48
|
+
|
49
|
+
describe '#process_with_node' do
|
50
|
+
it 'resets current_node' do
|
51
|
+
node1 = double()
|
52
|
+
node2 = double()
|
53
|
+
instance.process_with_node(node1) do
|
54
|
+
instance.current_node = node2
|
55
|
+
expect(instance.current_node).to eq node2
|
56
|
+
end
|
57
|
+
expect(instance.current_node).to eq node1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#process_with_other_node' do
|
62
|
+
it 'resets current_node' do
|
63
|
+
node1 = double()
|
64
|
+
node2 = double()
|
65
|
+
node3 = double()
|
66
|
+
instance.current_node = node1
|
67
|
+
instance.process_with_other_node(node2) do
|
68
|
+
instance.current_node = node3
|
69
|
+
expect(instance.current_node).to eq node3
|
70
|
+
end
|
71
|
+
expect(instance.current_node).to eq node1
|
72
|
+
end
|
73
|
+
end
|
44
74
|
end
|
45
75
|
end
|
@@ -2,8 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
4
|
describe Rewriter::Instance do
|
5
|
+
before { Rewriter::Instance.reset }
|
6
|
+
|
5
7
|
let(:instance) {
|
6
|
-
rewriter = Rewriter.new('
|
8
|
+
rewriter = Rewriter.new('foo', 'bar')
|
7
9
|
Rewriter::Instance.new(rewriter, 'file pattern')
|
8
10
|
}
|
9
11
|
|
@@ -78,9 +80,9 @@ module Synvert::Core
|
|
78
80
|
end
|
79
81
|
|
80
82
|
describe '#process' do
|
81
|
-
let(:rewriter) { Rewriter.new('
|
83
|
+
let(:rewriter) { Rewriter.new('foo', 'bar') }
|
82
84
|
|
83
|
-
it '
|
85
|
+
it 'writes new code to file' do
|
84
86
|
instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
|
85
87
|
with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
|
86
88
|
replace_with 'create {{arguments}}'
|
@@ -106,8 +108,8 @@ end
|
|
106
108
|
instance.process
|
107
109
|
end
|
108
110
|
|
109
|
-
it '
|
110
|
-
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
111
|
+
it 'does not write if file content is not changed' do
|
112
|
+
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
|
111
113
|
with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
|
112
114
|
unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
113
115
|
insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
|
@@ -116,6 +118,7 @@ end
|
|
116
118
|
end
|
117
119
|
input = """
|
118
120
|
RSpec.configure do |config|
|
121
|
+
config.include FactoryGirl::Syntax::Methods
|
119
122
|
end
|
120
123
|
"""
|
121
124
|
output = """
|
@@ -125,11 +128,11 @@ end
|
|
125
128
|
"""
|
126
129
|
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
|
127
130
|
expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
|
128
|
-
expect(File).
|
131
|
+
expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
|
129
132
|
instance.process
|
130
133
|
end
|
131
134
|
|
132
|
-
it 'does not
|
135
|
+
it 'does not read file if already read' do
|
133
136
|
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
|
134
137
|
with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
|
135
138
|
unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
@@ -147,80 +150,65 @@ end
|
|
147
150
|
config.include FactoryGirl::Syntax::Methods
|
148
151
|
end
|
149
152
|
"""
|
150
|
-
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
|
151
|
-
expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
|
152
|
-
expect(File).
|
153
|
+
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
|
154
|
+
expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input).once
|
155
|
+
expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
|
156
|
+
instance.process
|
153
157
|
instance.process
|
154
158
|
end
|
155
159
|
|
156
|
-
it '
|
157
|
-
instance = Rewriter::Instance.new rewriter, '
|
158
|
-
with_node type: 'send', receiver:
|
159
|
-
|
160
|
+
it 'updates file_source and file_ast when writing a file' do
|
161
|
+
instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
|
162
|
+
with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
|
163
|
+
replace_with 'create {{arguments}}'
|
160
164
|
end
|
161
165
|
end
|
162
|
-
input =
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
166
|
+
input = """
|
167
|
+
it 'uses factory_girl' do
|
168
|
+
user = FactoryGirl.create :user
|
169
|
+
post = FactoryGirl.create :post, user: user
|
170
|
+
assert post.valid?
|
171
|
+
end
|
172
|
+
"""
|
173
|
+
output = """
|
174
|
+
it 'uses factory_girl' do
|
175
|
+
user = create :user
|
176
|
+
post = create :post, user: user
|
177
|
+
assert post.valid?
|
178
|
+
end
|
179
|
+
"""
|
180
|
+
expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
|
181
|
+
expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
|
182
|
+
expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
|
183
|
+
expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(output)
|
184
|
+
instance.process
|
167
185
|
instance.process
|
168
|
-
end
|
169
|
-
|
170
|
-
describe "#check_conflict_actions" do
|
171
|
-
it "has no conflict" do
|
172
|
-
action1 = double(begin_pos: 10, end_pos: 20)
|
173
|
-
action2 = double(begin_pos: 30, end_pos: 40)
|
174
|
-
action3 = double(begin_pos: 50, end_pos: 60)
|
175
|
-
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
176
|
-
instance.instance_variable_set :@actions, [action1, action2, action3]
|
177
|
-
instance.send(:check_conflict_actions)
|
178
|
-
expect(instance.instance_variable_get :@conflict_actions).to eq []
|
179
|
-
expect(instance.instance_variable_get :@actions).to eq [action1, action2, action3]
|
180
|
-
end
|
181
|
-
|
182
|
-
it "has no conflict" do
|
183
|
-
action1 = double(begin_pos: 30, end_pos: 40)
|
184
|
-
action2 = double(begin_pos: 50, end_pos: 60)
|
185
|
-
action3 = double(begin_pos: 10, end_pos: 20)
|
186
|
-
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
187
|
-
instance.instance_variable_set :@actions, [action1, action2, action3]
|
188
|
-
instance.send(:check_conflict_actions)
|
189
|
-
expect(instance.instance_variable_get :@conflict_actions).to eq [action2, action1]
|
190
|
-
expect(instance.instance_variable_get :@actions).to eq [action3]
|
191
|
-
end
|
192
186
|
end
|
193
187
|
end
|
194
188
|
|
195
|
-
describe
|
196
|
-
let(:rewriter) { Rewriter.new('
|
197
|
-
|
198
|
-
it
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
instance.
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
expect(instance.
|
189
|
+
describe "#get_conflict_actions" do
|
190
|
+
let(:rewriter) { Rewriter.new('foo', 'bar') }
|
191
|
+
|
192
|
+
it "has no conflict" do
|
193
|
+
action1 = double(begin_pos: 10, end_pos: 20)
|
194
|
+
action2 = double(begin_pos: 30, end_pos: 40)
|
195
|
+
action3 = double(begin_pos: 50, end_pos: 60)
|
196
|
+
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
197
|
+
instance.instance_variable_set :@actions, [action1, action2, action3]
|
198
|
+
conflict_actions = instance.send(:get_conflict_actions)
|
199
|
+
expect(conflict_actions).to eq []
|
200
|
+
expect(instance.instance_variable_get :@actions).to eq [action1, action2, action3]
|
207
201
|
end
|
208
|
-
end
|
209
202
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
instance = Rewriter::Instance.new rewriter, 'spec
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
instance.
|
219
|
-
instance.process_with_other_node(node2) do
|
220
|
-
instance.current_node = node3
|
221
|
-
expect(instance.current_node).to eq node3
|
222
|
-
end
|
223
|
-
expect(instance.current_node).to eq node1
|
203
|
+
it "has no conflict" do
|
204
|
+
action1 = double(begin_pos: 30, end_pos: 40)
|
205
|
+
action2 = double(begin_pos: 50, end_pos: 60)
|
206
|
+
action3 = double(begin_pos: 10, end_pos: 20)
|
207
|
+
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
208
|
+
instance.instance_variable_set :@actions, [action1, action2, action3]
|
209
|
+
conflict_actions = instance.send(:get_conflict_actions)
|
210
|
+
expect(conflict_actions).to eq [action2, action1]
|
211
|
+
expect(instance.instance_variable_get :@actions).to eq [action3]
|
224
212
|
end
|
225
213
|
end
|
226
214
|
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module Synvert::Core
|
4
4
|
describe Rewriter do
|
5
5
|
it 'parses description' do
|
6
|
-
rewriter = Rewriter.new 'name' do
|
6
|
+
rewriter = Rewriter.new 'group', 'name' do
|
7
7
|
description 'rewriter description'
|
8
8
|
end
|
9
9
|
rewriter.process
|
@@ -12,7 +12,7 @@ module Synvert::Core
|
|
12
12
|
|
13
13
|
it 'parses if_gem' do
|
14
14
|
expect(Rewriter::GemSpec).to receive(:new).with('synvert', {gte: '1.0.0'})
|
15
|
-
rewriter = Rewriter.new 'name' do
|
15
|
+
rewriter = Rewriter.new 'group', 'name' do
|
16
16
|
if_gem 'synvert', {gte: '1.0.0'}
|
17
17
|
end
|
18
18
|
rewriter.process
|
@@ -22,7 +22,7 @@ module Synvert::Core
|
|
22
22
|
it 'does nothing if if_gem not match' do
|
23
23
|
expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(false)
|
24
24
|
expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
|
25
|
-
rewriter = Rewriter.new 'name' do
|
25
|
+
rewriter = Rewriter.new 'group', 'name' do
|
26
26
|
if_gem 'synvert', '1.0.0'
|
27
27
|
within_file 'config/routes.rb' do; end
|
28
28
|
end
|
@@ -31,7 +31,7 @@ module Synvert::Core
|
|
31
31
|
|
32
32
|
it 'delegates process to instances if if_gem not exist' do
|
33
33
|
expect_any_instance_of(Rewriter::Instance).to receive(:process)
|
34
|
-
rewriter = Rewriter.new 'name' do
|
34
|
+
rewriter = Rewriter.new 'group', 'name' do
|
35
35
|
within_file 'config/routes.rb' do; end
|
36
36
|
end
|
37
37
|
rewriter.process
|
@@ -40,7 +40,7 @@ module Synvert::Core
|
|
40
40
|
it 'delegates process to instances if if_gem matches' do
|
41
41
|
expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(true)
|
42
42
|
expect_any_instance_of(Rewriter::Instance).to receive(:process)
|
43
|
-
rewriter = Rewriter.new 'name' do
|
43
|
+
rewriter = Rewriter.new 'group', 'name' do
|
44
44
|
if_gem 'synvert', '1.0.0'
|
45
45
|
within_file 'config/routes.rb' do; end
|
46
46
|
end
|
@@ -50,7 +50,7 @@ module Synvert::Core
|
|
50
50
|
it 'does nothing in sandbox mode' do
|
51
51
|
expect_any_instance_of(Rewriter::GemSpec).not_to receive(:match?)
|
52
52
|
expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
|
53
|
-
rewriter = Rewriter.new 'name' do
|
53
|
+
rewriter = Rewriter.new 'group', 'name' do
|
54
54
|
if_gem 'synvert', '1.0.0'
|
55
55
|
within_file 'config/routes.rb' do; end
|
56
56
|
end
|
@@ -60,7 +60,7 @@ module Synvert::Core
|
|
60
60
|
|
61
61
|
describe 'parses add_file' do
|
62
62
|
it 'creates a new file' do
|
63
|
-
rewriter = Rewriter.new 'rewriter2' do
|
63
|
+
rewriter = Rewriter.new 'group', 'rewriter2' do
|
64
64
|
add_file 'foo.bar', 'FooBar'
|
65
65
|
end
|
66
66
|
rewriter.process
|
@@ -69,7 +69,7 @@ module Synvert::Core
|
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'does nothing in sandbox mode' do
|
72
|
-
rewriter = Rewriter.new 'rewriter2' do
|
72
|
+
rewriter = Rewriter.new 'group', 'rewriter2' do
|
73
73
|
add_file 'foo.bar', 'FooBar'
|
74
74
|
end
|
75
75
|
rewriter.process_with_sandbox
|
@@ -80,7 +80,7 @@ module Synvert::Core
|
|
80
80
|
describe 'parses remove_file' do
|
81
81
|
it 'removes a file' do
|
82
82
|
FileUtils.touch './foo.bar'
|
83
|
-
rewriter = Rewriter.new 'rewriter2' do
|
83
|
+
rewriter = Rewriter.new 'group', 'rewriter2' do
|
84
84
|
remove_file 'foo.bar'
|
85
85
|
end
|
86
86
|
rewriter.process
|
@@ -88,7 +88,7 @@ module Synvert::Core
|
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'does nothing if file not exist' do
|
91
|
-
rewriter = Rewriter.new 'rewriter2' do
|
91
|
+
rewriter = Rewriter.new 'group', 'rewriter2' do
|
92
92
|
remove_file 'foo.bar'
|
93
93
|
end
|
94
94
|
rewriter.process
|
@@ -97,7 +97,7 @@ module Synvert::Core
|
|
97
97
|
|
98
98
|
it 'does nothing in sandbox mode' do
|
99
99
|
FileUtils.touch './foo.bar'
|
100
|
-
rewriter = Rewriter.new 'rewriter2' do
|
100
|
+
rewriter = Rewriter.new 'group', 'rewriter2' do
|
101
101
|
add_file 'foo.bar', 'FooBar'
|
102
102
|
end
|
103
103
|
rewriter.process_with_sandbox
|
@@ -108,18 +108,18 @@ module Synvert::Core
|
|
108
108
|
|
109
109
|
describe 'parses add_snippet' do
|
110
110
|
it 'processes the rewritter' do
|
111
|
-
rewriter1 = Rewriter.new 'rewriter1'
|
112
|
-
rewriter2 = Rewriter.new 'rewriter2' do
|
113
|
-
add_snippet :rewriter1
|
111
|
+
rewriter1 = Rewriter.new 'group', 'rewriter1'
|
112
|
+
rewriter2 = Rewriter.new 'group', 'rewriter2' do
|
113
|
+
add_snippet :group, :rewriter1
|
114
114
|
end
|
115
115
|
expect(rewriter1).to receive(:process)
|
116
116
|
rewriter2.process
|
117
117
|
end
|
118
118
|
|
119
119
|
it 'adds sub_snippets' do
|
120
|
-
rewriter1 = Rewriter.new 'rewriter1'
|
121
|
-
rewriter2 = Rewriter.new 'rewriter2' do
|
122
|
-
add_snippet :rewriter1
|
120
|
+
rewriter1 = Rewriter.new 'group', 'rewriter1'
|
121
|
+
rewriter2 = Rewriter.new 'group', 'rewriter2' do
|
122
|
+
add_snippet :group, :rewriter1
|
123
123
|
end
|
124
124
|
expect(rewriter1).to receive(:process)
|
125
125
|
rewriter2.process
|
@@ -127,15 +127,15 @@ module Synvert::Core
|
|
127
127
|
end
|
128
128
|
|
129
129
|
it 'raises RewriterNotFound' do
|
130
|
-
rewriter = Rewriter.new 'name' do
|
131
|
-
add_snippet :not_exist
|
130
|
+
rewriter = Rewriter.new 'group', 'name' do
|
131
|
+
add_snippet :group, :not_exist
|
132
132
|
end
|
133
133
|
expect { rewriter.process }.to raise_error(RewriterNotFound)
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
137
|
it 'parses helper_method' do
|
138
|
-
rewriter = Rewriter.new 'name' do
|
138
|
+
rewriter = Rewriter.new 'group', 'name' do
|
139
139
|
helper_method 'dynamic_helper' do |arg1, arg2|
|
140
140
|
'dynamic result'
|
141
141
|
end
|
@@ -146,7 +146,7 @@ module Synvert::Core
|
|
146
146
|
end
|
147
147
|
|
148
148
|
it 'parses todo' do
|
149
|
-
rewriter = Rewriter.new 'name' do
|
149
|
+
rewriter = Rewriter.new 'group', 'name' do
|
150
150
|
todo "this rewriter doesn't do blah blah blah"
|
151
151
|
end
|
152
152
|
rewriter.process
|
@@ -159,25 +159,25 @@ module Synvert::Core
|
|
159
159
|
end
|
160
160
|
|
161
161
|
it 'registers and fetches' do
|
162
|
-
rewriter = Rewriter.new 'rewriter'
|
163
|
-
expect(Rewriter.fetch('rewriter')).to eq rewriter
|
162
|
+
rewriter = Rewriter.new 'group', 'rewriter'
|
163
|
+
expect(Rewriter.fetch('group', 'rewriter')).to eq rewriter
|
164
164
|
end
|
165
165
|
|
166
166
|
it 'registers and calls rewriter' do
|
167
|
-
rewriter = Rewriter.new 'rewriter'
|
167
|
+
rewriter = Rewriter.new 'group', 'rewriter'
|
168
168
|
expect(rewriter).to receive(:process)
|
169
|
-
Rewriter.call 'rewriter'
|
169
|
+
Rewriter.call 'group', 'rewriter'
|
170
170
|
end
|
171
171
|
|
172
172
|
context "available" do
|
173
173
|
it 'lists empty rewriters' do
|
174
|
-
expect(Rewriter.availables).to eq
|
174
|
+
expect(Rewriter.availables).to eq({})
|
175
175
|
end
|
176
176
|
|
177
177
|
it 'registers and lists all available rewriters' do
|
178
|
-
rewriter1 = Rewriter.new 'rewriter1'
|
179
|
-
rewriter2 = Rewriter.new 'rewriter2'
|
180
|
-
expect(Rewriter.availables).to eq
|
178
|
+
rewriter1 = Rewriter.new 'group', 'rewriter1'
|
179
|
+
rewriter2 = Rewriter.new 'group', 'rewriter2'
|
180
|
+
expect(Rewriter.availables).to eq({'group' => {'rewriter1' => rewriter1, 'rewriter2' => rewriter2}})
|
181
181
|
end
|
182
182
|
end
|
183
183
|
end
|
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.
|
4
|
+
version: 0.5.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: 2014-08-
|
11
|
+
date: 2014-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|