synvert-core 0.21.2 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -6
- data/lib/synvert/core.rb +1 -0
- data/lib/synvert/core/node_ext.rb +15 -17
- data/lib/synvert/core/rewriter.rb +11 -1
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +3 -5
- data/lib/synvert/core/rewriter/instance.rb +41 -34
- data/lib/synvert/core/rewriter/scope/within_scope.rb +2 -2
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/rewriter/instance_spec.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c112197bf98b5adfe2b5826ea3301f06f66511c111906bfd0c72a8c54a10d33
|
4
|
+
data.tar.gz: adb56b1ae52dd1b87d84d2861b7433586c96ddc6718fafcd79852775f84b38c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2f79639b669fc7b054ba8e57df70925f7f9bfdf6e40144069d41848bc0d0b3df0d60e73fc078c8e78eb29718bc8f3a181ee7964c63a3dcadd9a8981ce31544b
|
7
|
+
data.tar.gz: 758e0896064a0c15dbe4e3d3d128bc3f42517c2f2802067cccab20f3b366165d7ef5f3203975e261903573f2989f82f3918b09db925a2d896b9bee5897299a58
|
data/CHANGELOG.md
CHANGED
@@ -1,12 +1,9 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.22.0 (2021-03-13)
|
4
4
|
|
5
|
-
*
|
6
|
-
|
7
|
-
## 0.21.1 (2021-02-26)
|
8
|
-
|
9
|
-
* Fix `find_matching_nodes` if `current_node` is a `Parser::AST::Node`
|
5
|
+
* Track `affected_files` for rewriter
|
6
|
+
* Fix `find_matching_nodes` for `current_node`
|
10
7
|
|
11
8
|
## 0.21.0 (2021-02-25)
|
12
9
|
|
data/lib/synvert/core.rb
CHANGED
@@ -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
|
-
.
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
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.
|
@@ -145,7 +145,9 @@ module Synvert::Core
|
|
145
145
|
# @return [Array] helper methods.
|
146
146
|
# @!attribute [r] warnings
|
147
147
|
# @return [Array<Synvert::Core::Rewriter::Warning>] warning messages.
|
148
|
-
|
148
|
+
# @!attribute [r] affected_files
|
149
|
+
# @return [Set] affected fileds
|
150
|
+
attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files
|
149
151
|
|
150
152
|
# Initialize a rewriter.
|
151
153
|
# When a rewriter is initialized, it is also registered.
|
@@ -161,6 +163,7 @@ module Synvert::Core
|
|
161
163
|
@helpers = []
|
162
164
|
@sub_snippets = []
|
163
165
|
@warnings = []
|
166
|
+
@affected_files = Set.new
|
164
167
|
self.class.register(@group, @name, self)
|
165
168
|
end
|
166
169
|
|
@@ -188,6 +191,13 @@ module Synvert::Core
|
|
188
191
|
@warnings << warning
|
189
192
|
end
|
190
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
|
+
|
191
201
|
#######
|
192
202
|
# DSL #
|
193
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
|
-
|
28
|
-
|
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
|
@@ -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
|
-
.
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
126
|
-
|
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
|
@@ -46,8 +46,8 @@ module Synvert::Core
|
|
46
46
|
current_node.children.each do |child_node|
|
47
47
|
matching_nodes << child_node if child_node.match? @rules
|
48
48
|
end
|
49
|
-
|
50
|
-
matching_nodes << current_node
|
49
|
+
elsif current_node.match? @rules
|
50
|
+
matching_nodes << current_node
|
51
51
|
end
|
52
52
|
else
|
53
53
|
current_node.each do |child_node|
|
data/lib/synvert/core/version.rb
CHANGED
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.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-
|
11
|
+
date: 2021-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -209,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
209
209
|
- !ruby/object:Gem::Version
|
210
210
|
version: '0'
|
211
211
|
requirements: []
|
212
|
-
rubygems_version: 3.
|
212
|
+
rubygems_version: 3.0.3
|
213
213
|
signing_key:
|
214
214
|
specification_version: 4
|
215
215
|
summary: convert ruby code to better syntax.
|