synvert-core 0.16.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -1
- data/CHANGELOG.md +16 -0
- data/lib/synvert/core.rb +1 -4
- data/lib/synvert/core/configuration.rb +9 -17
- data/lib/synvert/core/engine/erb.rb +29 -22
- data/lib/synvert/core/exceptions.rb +3 -3
- data/lib/synvert/core/node_ext.rb +102 -101
- data/lib/synvert/core/rewriter.rb +33 -19
- data/lib/synvert/core/rewriter/action.rb +3 -5
- data/lib/synvert/core/rewriter/action/append_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/insert_action.rb +7 -3
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/remove_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +4 -3
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +6 -4
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -2
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/gem_spec.rb +10 -10
- data/lib/synvert/core/rewriter/helper.rb +3 -5
- data/lib/synvert/core/rewriter/instance.rb +22 -19
- data/lib/synvert/core/rewriter/scope/within_scope.rb +18 -11
- data/lib/synvert/core/version.rb +1 -1
- data/spec/spec_helper.rb +1 -6
- data/spec/synvert/core/engine/erb_spec.rb +30 -30
- data/spec/synvert/core/node_ext_spec.rb +53 -52
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +8 -8
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +3 -3
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +15 -11
- data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +17 -9
- data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +21 -12
- data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +17 -9
- data/spec/synvert/core/rewriter/gem_spec_spec.rb +11 -10
- data/spec/synvert/core/rewriter/helper_spec.rb +34 -31
- data/spec/synvert/core/rewriter/instance_spec.rb +104 -70
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +8 -6
- data/spec/synvert/core/rewriter/scope/within_scope.rb +16 -9
- data/spec/synvert/core/rewriter_spec.rb +105 -68
- metadata +2 -4
- data/spec/synvert/core/configuration_spec.rb +0 -12
@@ -42,7 +42,14 @@ module Synvert::Core
|
|
42
42
|
autoload :RubyVersion, 'synvert/core/rewriter/ruby_version'
|
43
43
|
autoload :GemSpec, 'synvert/core/rewriter/gem_spec'
|
44
44
|
|
45
|
-
class <<self
|
45
|
+
class << self
|
46
|
+
# Execute the temporary rewriter without group and name.
|
47
|
+
#
|
48
|
+
# @param block [Block] a block defines the behaviors of the rewriter.
|
49
|
+
def execute(&block)
|
50
|
+
Rewriter.new('', '', &block).process
|
51
|
+
end
|
52
|
+
|
46
53
|
# Register a rewriter with its group and name.
|
47
54
|
#
|
48
55
|
# @param group [String] the rewriter group.
|
@@ -65,7 +72,7 @@ module Synvert::Core
|
|
65
72
|
if exist? group, name
|
66
73
|
rewriters[group][name]
|
67
74
|
else
|
68
|
-
raise RewriterNotFound
|
75
|
+
raise RewriterNotFound, "Rewriter #{group} #{name} not found"
|
69
76
|
end
|
70
77
|
end
|
71
78
|
|
@@ -73,16 +80,21 @@ module Synvert::Core
|
|
73
80
|
#
|
74
81
|
# @param group [String] the rewriter group.
|
75
82
|
# @param name [String] the rewriter name.
|
83
|
+
# @param sandbox [Boolean] if run in sandbox mode, default is false.
|
76
84
|
# @return [Synvert::Core::Rewriter] the registered rewriter.
|
77
85
|
# @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
|
78
|
-
def call(group, name)
|
86
|
+
def call(group, name, sandbox = false)
|
79
87
|
group, name = group.to_s, name.to_s
|
80
88
|
if exist? group, name
|
81
89
|
rewriter = rewriters[group][name]
|
82
|
-
|
90
|
+
if sandbox
|
91
|
+
rewriter.process_with_sandbox
|
92
|
+
else
|
93
|
+
rewriter.process
|
94
|
+
end
|
83
95
|
rewriter
|
84
96
|
else
|
85
|
-
raise RewriterNotFound
|
97
|
+
raise RewriterNotFound, "Rewriter #{group}/#{name} not found"
|
86
98
|
end
|
87
99
|
end
|
88
100
|
|
@@ -112,7 +124,7 @@ module Synvert::Core
|
|
112
124
|
rewriters.clear
|
113
125
|
end
|
114
126
|
|
115
|
-
|
127
|
+
private
|
116
128
|
|
117
129
|
def rewriters
|
118
130
|
@rewriters ||= {}
|
@@ -151,15 +163,18 @@ module Synvert::Core
|
|
151
163
|
# Process the rewriter.
|
152
164
|
# It will call the block.
|
153
165
|
def process
|
154
|
-
|
166
|
+
instance_eval &@block
|
155
167
|
end
|
156
168
|
|
157
169
|
# Process rewriter with sandbox mode.
|
158
170
|
# It will call the block but doesn't change any file.
|
159
171
|
def process_with_sandbox
|
160
172
|
@sandbox = true
|
161
|
-
|
162
|
-
|
173
|
+
begin
|
174
|
+
process
|
175
|
+
ensure
|
176
|
+
@sandbox = false
|
177
|
+
end
|
163
178
|
end
|
164
179
|
|
165
180
|
# Add a warning.
|
@@ -178,7 +193,7 @@ module Synvert::Core
|
|
178
193
|
#
|
179
194
|
# @param description [String] rewriter description.
|
180
195
|
# @return rewriter description.
|
181
|
-
def description(description=nil)
|
196
|
+
def description(description = nil)
|
182
197
|
if description
|
183
198
|
@description = description
|
184
199
|
else
|
@@ -208,17 +223,16 @@ module Synvert::Core
|
|
208
223
|
# @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
|
209
224
|
# @param options [Hash] instance options.
|
210
225
|
# @param block [Block] the block to rewrite code in the matching files.
|
211
|
-
def within_files(file_pattern, options={}, &block)
|
226
|
+
def within_files(file_pattern, options = {}, &block)
|
212
227
|
return if @sandbox
|
213
228
|
|
214
|
-
if (!@ruby_version || @ruby_version.match?) &&
|
215
|
-
(!@gem_spec || @gem_spec.match?)
|
229
|
+
if (!@ruby_version || @ruby_version.match?) && (!@gem_spec || @gem_spec.match?)
|
216
230
|
Rewriter::Instance.new(self, file_pattern, options, &block).process
|
217
231
|
end
|
218
232
|
end
|
219
233
|
|
220
234
|
# Parse within_file dsl, it finds a specifiled file.
|
221
|
-
|
235
|
+
alias within_file within_files
|
222
236
|
|
223
237
|
# Parses add_file dsl, it adds a new file.
|
224
238
|
#
|
@@ -227,7 +241,7 @@ module Synvert::Core
|
|
227
241
|
def add_file(filename, content)
|
228
242
|
return if @sandbox
|
229
243
|
|
230
|
-
filepath = File.join(Configuration.
|
244
|
+
filepath = File.join(Configuration.path, filename)
|
231
245
|
if File.exist?(filepath)
|
232
246
|
puts "File #{filepath} already exists."
|
233
247
|
return
|
@@ -245,7 +259,7 @@ module Synvert::Core
|
|
245
259
|
def remove_file(filename)
|
246
260
|
return if @sandbox
|
247
261
|
|
248
|
-
file_path = File.join(Configuration.
|
262
|
+
file_path = File.join(Configuration.path, filename)
|
249
263
|
File.delete(file_path) if File.exist?(file_path)
|
250
264
|
end
|
251
265
|
|
@@ -254,7 +268,7 @@ module Synvert::Core
|
|
254
268
|
# @param group [String] group of another rewriter.
|
255
269
|
# @param name [String] name of another rewriter.
|
256
270
|
def add_snippet(group, name)
|
257
|
-
@sub_snippets << self.class.call(group.to_s, name.to_s)
|
271
|
+
@sub_snippets << self.class.call(group.to_s, name.to_s, @sandbox)
|
258
272
|
end
|
259
273
|
|
260
274
|
# Parse helper_method dsl, it defines helper method for [Synvert::Core::Rewriter::Instance].
|
@@ -262,7 +276,7 @@ module Synvert::Core
|
|
262
276
|
# @param name [String] helper method name.
|
263
277
|
# @param block [Block] helper method block.
|
264
278
|
def helper_method(name, &block)
|
265
|
-
@helpers << {name: name, block: block}
|
279
|
+
@helpers << { name: name, block: block }
|
266
280
|
end
|
267
281
|
|
268
282
|
# Parse todo dsl, it sets todo of the rewriter.
|
@@ -270,7 +284,7 @@ module Synvert::Core
|
|
270
284
|
#
|
271
285
|
# @param todo_list [String] rewriter todo.
|
272
286
|
# @return [String] rewriter todo.
|
273
|
-
def todo(todo=nil)
|
287
|
+
def todo(todo = nil)
|
274
288
|
if todo
|
275
289
|
@todo = todo
|
276
290
|
else
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Synvert::Core
|
4
4
|
# Action defines rewriter action, add, replace or remove code.
|
5
5
|
class Rewriter::Action
|
6
|
-
DEFAULT_OPTIONS = { autoindent: true }
|
6
|
+
DEFAULT_OPTIONS = { autoindent: true }.freeze
|
7
7
|
DEFAULT_INDENT = 2
|
8
8
|
|
9
9
|
# Initialize an action.
|
@@ -11,7 +11,7 @@ module Synvert::Core
|
|
11
11
|
# @param instance [Synvert::Core::Rewriter::Instance]
|
12
12
|
# @param code [String] new code to add, replace or remove.
|
13
13
|
# @param options [Hash] action options, it includes :autoindent.
|
14
|
-
def initialize(instance, code, options={})
|
14
|
+
def initialize(instance, code, options = {})
|
15
15
|
@instance = instance
|
16
16
|
@code = code
|
17
17
|
@options = DEFAULT_OPTIONS.merge(options)
|
@@ -30,9 +30,7 @@ module Synvert::Core
|
|
30
30
|
# @return [String] rewritten code.
|
31
31
|
def rewritten_code
|
32
32
|
if rewritten_source.split("\n").length > 1
|
33
|
-
"\n\n" + rewritten_source.split("\n").map { |line|
|
34
|
-
indent(@node) + line
|
35
|
-
}.join("\n")
|
33
|
+
"\n\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }.join("\n")
|
36
34
|
else
|
37
35
|
"\n" + indent(@node) + rewritten_source
|
38
36
|
end
|
@@ -3,7 +3,7 @@
|
|
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 =
|
6
|
+
DO_LENGTH = ' do'.length
|
7
7
|
|
8
8
|
# Begin position to insert code.
|
9
9
|
#
|
@@ -11,7 +11,11 @@ module Synvert::Core
|
|
11
11
|
def begin_pos
|
12
12
|
case @node.type
|
13
13
|
when :block
|
14
|
-
@node.children[1].children.empty?
|
14
|
+
if @node.children[1].children.empty?
|
15
|
+
@node.children[0].loc.expression.end_pos + DO_LENGTH
|
16
|
+
else
|
17
|
+
@node.children[1].loc.expression.end_pos
|
18
|
+
end
|
15
19
|
when :class
|
16
20
|
@node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
|
17
21
|
else
|
@@ -26,7 +30,7 @@ module Synvert::Core
|
|
26
30
|
begin_pos
|
27
31
|
end
|
28
32
|
|
29
|
-
|
33
|
+
private
|
30
34
|
|
31
35
|
# Indent of the node.
|
32
36
|
#
|
@@ -4,7 +4,7 @@ module Synvert::Core
|
|
4
4
|
# ReplaceErbStmtWithExprAction to replace erb stmt code to expr,
|
5
5
|
# e.g. <% form_for ... %> => <%= form_for ... %>.
|
6
6
|
class Rewriter::ReplaceErbStmtWithExprAction < Rewriter::Action
|
7
|
-
def initialize(instance, code=nil)
|
7
|
+
def initialize(instance, code = nil)
|
8
8
|
super
|
9
9
|
end
|
10
10
|
|
@@ -23,7 +23,7 @@ module Synvert::Core
|
|
23
23
|
# @return [Integer] end position.
|
24
24
|
def end_pos
|
25
25
|
node_begin_pos = @node.loc.expression.begin_pos
|
26
|
-
node_begin_pos += @node.loc.expression.source.index
|
26
|
+
node_begin_pos += @node.loc.expression.source.index 'do'
|
27
27
|
while @node.loc.expression.source_buffer.source[node_begin_pos += 1] != '@'
|
28
28
|
end
|
29
29
|
node_begin_pos
|
@@ -33,7 +33,8 @@ module Synvert::Core
|
|
33
33
|
#
|
34
34
|
# @return [String] rewritten code.
|
35
35
|
def rewritten_code
|
36
|
-
@node.loc.expression.source_buffer.source[begin_pos...end_pos]
|
36
|
+
@node.loc.expression.source_buffer.source[begin_pos...end_pos]
|
37
|
+
.sub(Engine::ERUBY_STMT_SPLITTER, '@output_buffer.append= ')
|
37
38
|
.sub(Engine::ERUBY_STMT_SPLITTER, Engine::ERUBY_EXPR_SPLITTER)
|
38
39
|
end
|
39
40
|
end
|
@@ -23,16 +23,18 @@ 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
|
-
|
28
|
-
|
26
|
+
rewritten_source
|
27
|
+
.split("\n")
|
28
|
+
.each_with_index { |line, index|
|
29
|
+
new_code << (index == 0 || !@options[:autoindent] ? line : indent(@node) + line)
|
30
|
+
}
|
29
31
|
new_code.join("\n")
|
30
32
|
else
|
31
33
|
rewritten_source
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
|
-
|
37
|
+
private
|
36
38
|
|
37
39
|
# Indent of the node
|
38
40
|
#
|
@@ -5,8 +5,7 @@ module Synvert::Core
|
|
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?
|
8
|
-
@instance.current_node.body.size == 1 &&
|
9
|
-
@instance.current_node.body.first.match?(@rules)
|
8
|
+
@instance.current_node.body.size == 1 && @instance.current_node.body.first.match?(@rules)
|
10
9
|
end
|
11
10
|
end
|
12
11
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Synvert::Core
|
4
4
|
# GemSpec checks and compares gem version.
|
5
5
|
class Rewriter::GemSpec
|
6
|
-
OPERATORS = {eq: '==', lt: '<', gt: '>', lte: '<=', gte: '>=', ne: '!='}
|
6
|
+
OPERATORS = { eq: '==', lt: '<', gt: '>', lte: '<=', gte: '>=', ne: '!=' }.freeze
|
7
7
|
|
8
8
|
# Initialize a gem_spec.
|
9
9
|
#
|
@@ -26,16 +26,16 @@ module Synvert::Core
|
|
26
26
|
# @return [Boolean] true if matches, otherwise false.
|
27
27
|
# @raise [Synvert::Core::GemfileLockNotFound] raise if Gemfile.lock does not exist.
|
28
28
|
def match?
|
29
|
-
gemfile_lock_path = File.join(Configuration.
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
29
|
+
gemfile_lock_path = File.join(Configuration.path, 'Gemfile.lock')
|
30
|
+
|
31
|
+
# if Gemfile.lock does not exist, just ignore this check
|
32
|
+
return true unless File.exist?(gemfile_lock_path)
|
33
|
+
|
34
|
+
parser = Bundler::LockfileParser.new(File.read(gemfile_lock_path))
|
35
|
+
if spec = parser.specs.find { |spec| spec.name == @name }
|
36
|
+
Gem::Version.new(spec.version).send(OPERATORS[@operator], @version)
|
37
37
|
else
|
38
|
-
|
38
|
+
false
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -34,9 +34,9 @@ module Synvert::Core
|
|
34
34
|
# if current_node has argument, it returns "({{arguments}})"
|
35
35
|
def add_arguments_with_parenthesis_if_necessary
|
36
36
|
if node.arguments.size > 0
|
37
|
-
|
37
|
+
'({{arguments}})'
|
38
38
|
else
|
39
|
-
|
39
|
+
''
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -65,9 +65,7 @@ module Synvert::Core
|
|
65
65
|
#
|
66
66
|
# strip_brackets("(1..100)") #=> "1..100"
|
67
67
|
def strip_brackets(code)
|
68
|
-
code.sub(/^\((.*)\)$/) { $1 }
|
69
|
-
.sub(/^\[(.*)\]$/) { $1 }
|
70
|
-
.sub(/^{(.*)}$/) { $1 }
|
68
|
+
code.sub(/^\((.*)\)$/) { $1 }.sub(/^\[(.*)\]$/) { $1 }.sub(/^{(.*)}$/) { $1 }
|
71
69
|
end
|
72
70
|
end
|
73
71
|
end
|
@@ -8,7 +8,7 @@ module Synvert::Core
|
|
8
8
|
class Rewriter::Instance
|
9
9
|
include Rewriter::Helper
|
10
10
|
|
11
|
-
class <<self
|
11
|
+
class << self
|
12
12
|
# Cached file source.
|
13
13
|
#
|
14
14
|
# @param file_path [String] file path
|
@@ -64,7 +64,7 @@ module Synvert::Core
|
|
64
64
|
# @return current filename
|
65
65
|
attr_accessor :current_node, :current_file
|
66
66
|
|
67
|
-
DEFAULT_OPTIONS = { sort_by: 'begin_pos' }
|
67
|
+
DEFAULT_OPTIONS = { sort_by: 'begin_pos' }.freeze
|
68
68
|
|
69
69
|
# Initialize an instance.
|
70
70
|
#
|
@@ -73,22 +73,25 @@ module Synvert::Core
|
|
73
73
|
# @param options [Hash] instance options, it includes :sort_by.
|
74
74
|
# @param block [Block] block code to find nodes, match conditions and rewrite code.
|
75
75
|
# @return [Synvert::Core::Rewriter::Instance]
|
76
|
-
def initialize(rewriter, file_pattern, options={}, &block)
|
76
|
+
def initialize(rewriter, file_pattern, options = {}, &block)
|
77
77
|
@rewriter = rewriter
|
78
78
|
@actions = []
|
79
79
|
@file_pattern = file_pattern
|
80
80
|
@options = DEFAULT_OPTIONS.merge(options)
|
81
81
|
@block = block
|
82
|
-
rewriter.helpers.each { |helper|
|
82
|
+
rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) }
|
83
83
|
end
|
84
84
|
|
85
85
|
# Process the instance.
|
86
86
|
# It finds all files, for each file, it executes the block code, gets all rewrite actions,
|
87
87
|
# and rewrite source code back to original file.
|
88
88
|
def process
|
89
|
-
file_pattern = File.join(Configuration.
|
90
|
-
Dir
|
91
|
-
|
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
|
+
|
92
95
|
begin
|
93
96
|
conflict_actions = []
|
94
97
|
source = +self.class.file_source(file_path)
|
@@ -96,7 +99,7 @@ module Synvert::Core
|
|
96
99
|
|
97
100
|
@current_file = file_path
|
98
101
|
|
99
|
-
|
102
|
+
process_with_node ast do
|
100
103
|
begin
|
101
104
|
instance_eval &@block
|
102
105
|
rescue NoMethodError
|
@@ -108,7 +111,7 @@ module Synvert::Core
|
|
108
111
|
if @actions.length > 0
|
109
112
|
@actions.sort_by! { |action| action.send(@options[:sort_by]) }
|
110
113
|
conflict_actions = get_conflict_actions
|
111
|
-
@actions.
|
114
|
+
@actions.reverse_each do |action|
|
112
115
|
source[action.begin_pos...action.end_pos] = action.rewritten_code
|
113
116
|
source = remove_code_or_whole_line(source, action.line)
|
114
117
|
end
|
@@ -121,7 +124,6 @@ module Synvert::Core
|
|
121
124
|
# do nothing, iterate next file
|
122
125
|
end while !conflict_actions.empty?
|
123
126
|
end
|
124
|
-
end
|
125
127
|
end
|
126
128
|
|
127
129
|
# Gets current node, it allows to get current node in block code.
|
@@ -146,7 +148,7 @@ module Synvert::Core
|
|
146
148
|
# @param node [Parser::AST::Node] node set to current_node
|
147
149
|
# @yield process
|
148
150
|
def process_with_other_node(node)
|
149
|
-
original_node =
|
151
|
+
original_node = current_node
|
150
152
|
self.current_node = node
|
151
153
|
yield
|
152
154
|
self.current_node = original_node
|
@@ -165,7 +167,7 @@ module Synvert::Core
|
|
165
167
|
Rewriter::WithinScope.new(self, rules, { recursive: true }, &block).process
|
166
168
|
end
|
167
169
|
|
168
|
-
|
170
|
+
alias with_node within_node
|
169
171
|
|
170
172
|
# Parse within_direct_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find direct matching ast nodes,
|
171
173
|
# then continue operating on each matching ast node.
|
@@ -176,7 +178,7 @@ module Synvert::Core
|
|
176
178
|
Rewriter::WithinScope.new(self, rules, { recursive: false }, &block).process
|
177
179
|
end
|
178
180
|
|
179
|
-
|
181
|
+
alias with_direct_node within_direct_node
|
180
182
|
|
181
183
|
# Parse goto_node dsl, it creates a [Synvert::Core::Rewriter::GotoScope] to go to a child node,
|
182
184
|
# then continue operating on the child node.
|
@@ -220,7 +222,7 @@ module Synvert::Core
|
|
220
222
|
#
|
221
223
|
# @param code [String] code need to be appended.
|
222
224
|
# @param options [Hash] action options.
|
223
|
-
def append(code, options={})
|
225
|
+
def append(code, options = {})
|
224
226
|
@actions << Rewriter::AppendAction.new(self, code, options)
|
225
227
|
end
|
226
228
|
|
@@ -229,7 +231,7 @@ module Synvert::Core
|
|
229
231
|
#
|
230
232
|
# @param code [String] code need to be inserted.
|
231
233
|
# @param options [Hash] action options.
|
232
|
-
def insert(code, options={})
|
234
|
+
def insert(code, options = {})
|
233
235
|
@actions << Rewriter::InsertAction.new(self, code, options)
|
234
236
|
end
|
235
237
|
|
@@ -238,7 +240,7 @@ module Synvert::Core
|
|
238
240
|
#
|
239
241
|
# @param code [String] code need to be inserted.
|
240
242
|
# @param options [Hash] action options.
|
241
|
-
def insert_after(node, options={})
|
243
|
+
def insert_after(node, options = {})
|
242
244
|
@actions << Rewriter::InsertAfterAction.new(self, node, options)
|
243
245
|
end
|
244
246
|
|
@@ -247,7 +249,7 @@ module Synvert::Core
|
|
247
249
|
#
|
248
250
|
# @param code [String] code need to be replaced with.
|
249
251
|
# @param options [Hash] action options.
|
250
|
-
def replace_with(code, options={})
|
252
|
+
def replace_with(code, options = {})
|
251
253
|
@actions << Rewriter::ReplaceWithAction.new(self, code, options)
|
252
254
|
end
|
253
255
|
|
@@ -269,7 +271,7 @@ module Synvert::Core
|
|
269
271
|
@rewriter.add_warning Rewriter::Warning.new(self, message)
|
270
272
|
end
|
271
273
|
|
272
|
-
|
274
|
+
private
|
273
275
|
|
274
276
|
# It changes source code from bottom to top, and it can change source code twice at the same time,
|
275
277
|
# So if there is an overlap between two actions, it removes the conflict actions and operate them in the next loop.
|
@@ -301,7 +303,8 @@ module Synvert::Core
|
|
301
303
|
source_arr = source.split("\n")
|
302
304
|
if source_arr[line - 1] && source_arr[line - 1].strip.empty?
|
303
305
|
source_arr.delete_at(line - 1)
|
304
|
-
if source_arr[line - 2] && source_arr[line - 2].strip.empty? && source_arr[line - 1] &&
|
306
|
+
if source_arr[line - 2] && source_arr[line - 2].strip.empty? && source_arr[line - 1] &&
|
307
|
+
source_arr[line - 1].strip.empty?
|
305
308
|
source_arr.delete_at(line - 1)
|
306
309
|
end
|
307
310
|
source_arr.join("\n") + (newline_at_end_of_line ? "\n" : '')
|