synvert-core 1.29.4 → 1.30.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 +8 -0
- data/Gemfile.lock +3 -3
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +5 -3
- data/lib/synvert/core/rewriter/condition.rb +1 -1
- data/lib/synvert/core/rewriter/instance.rb +21 -11
- data/lib/synvert/core/rewriter/scope/within_scope.rb +1 -1
- data/lib/synvert/core/rewriter.rb +4 -28
- data/lib/synvert/core/version.rb +1 -1
- data/spec/support/parser_helper.rb +3 -0
- data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +4 -4
- data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +4 -6
- data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +4 -6
- data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +3 -5
- data/spec/synvert/core/rewriter/instance_spec.rb +7 -7
- data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +1 -1
- data/spec/synvert/core/rewriter_spec.rb +1 -6
- 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: 8288215f49d9d6c0ac3e63959b51ec12124f0a2b77120e372945db08f9cfd10c
|
4
|
+
data.tar.gz: 7021cea657ad978f66beeaa1c6f35db19f391f686ec712a1d58bdb8c58613baf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2de497cbdd7b4e0659c4d42352148dd0350f1eb260734b4c0c03ffbc71d892eae215596fc9e1951c2d599a2832359e04b8f0ceb2ab3610150e0f84b315bfe41d
|
7
|
+
data.tar.gz: 8be5121b526499685c02be3b95049d38b79ae8257aa393822148d5e082b75dd41973550e33527c9572c5cd288654b6e84e27b26ebf6436665d178bbb883918c5
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.30.0 (2023-11-27)
|
4
|
+
|
5
|
+
* Update `node_mutation` to 1.22.0
|
6
|
+
* Update `node_query` to 1.14.1
|
7
|
+
* Add `adapter` as `Rewriter::ReplaceErbStmtWithExprAction` parameter
|
8
|
+
* Initialize NodeQuery and NodeMutation with adapter
|
9
|
+
* Remove `ensure_current_adapter` as adapter is an instance variable of Rewriter
|
10
|
+
|
3
11
|
## 1.29.4 (2023-11-24)
|
4
12
|
|
5
13
|
* Update `node_mutation` to 1.21.4
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
synvert-core (1.
|
4
|
+
synvert-core (1.30.0)
|
5
5
|
activesupport (< 7.0.0)
|
6
6
|
node_mutation (>= 1.21.6)
|
7
7
|
node_query (>= 1.13.12)
|
@@ -50,8 +50,8 @@ GEM
|
|
50
50
|
method_source (1.0.0)
|
51
51
|
minitest (5.20.0)
|
52
52
|
nenv (0.3.0)
|
53
|
-
node_mutation (1.
|
54
|
-
node_query (1.
|
53
|
+
node_mutation (1.22.0)
|
54
|
+
node_query (1.14.1)
|
55
55
|
notiffany (0.1.3)
|
56
56
|
nenv (~> 0.1)
|
57
57
|
shellany (~> 0.0)
|
@@ -8,8 +8,10 @@ module Synvert::Core
|
|
8
8
|
# Initialize a ReplaceErbStmtWithExprAction.
|
9
9
|
#
|
10
10
|
# @param node [Synvert::Core::Rewriter::Node]
|
11
|
-
|
12
|
-
|
11
|
+
# @param erb_source [String]
|
12
|
+
# @param adapter [NodeMutation::Adapter]
|
13
|
+
def initialize(node, erb_source, adapter:)
|
14
|
+
super(node, nil, adapter: adapter)
|
13
15
|
@erb_source = erb_source
|
14
16
|
@type = :insert
|
15
17
|
end
|
@@ -25,7 +27,7 @@ module Synvert::Core
|
|
25
27
|
|
26
28
|
# Calculate the begin the end positions.
|
27
29
|
def calculate_position
|
28
|
-
@start =
|
30
|
+
@start = @adapter.get_start(@node)
|
29
31
|
loop do
|
30
32
|
@start -= 1
|
31
33
|
break if @erb_source[@start] == '%'
|
@@ -10,7 +10,7 @@ module Synvert::Core
|
|
10
10
|
# @yield run when condition matches
|
11
11
|
def initialize(instance, nql_or_rules, &block)
|
12
12
|
@instance = instance
|
13
|
-
@node_query = NodeQuery.new(nql_or_rules)
|
13
|
+
@node_query = NodeQuery.new(nql_or_rules, adapter: instance.parser)
|
14
14
|
@block = block
|
15
15
|
end
|
16
16
|
|
@@ -35,9 +35,7 @@ module Synvert::Core
|
|
35
35
|
# @return file path
|
36
36
|
# @!attribute [rw] current_node
|
37
37
|
# @return current ast node
|
38
|
-
|
39
|
-
# @return NodeMutation Adapter
|
40
|
-
attr_reader :file_path, :current_node, :mutation_adapter
|
38
|
+
attr_reader :file_path, :current_node
|
41
39
|
attr_accessor :current_node
|
42
40
|
|
43
41
|
# Process the instance.
|
@@ -51,9 +49,8 @@ module Synvert::Core
|
|
51
49
|
loop do
|
52
50
|
source = read_source(absolute_file_path)
|
53
51
|
encoded_source = Engine.encode(File.extname(file_path), source)
|
54
|
-
@current_mutation = NodeMutation.new(source)
|
52
|
+
@current_mutation = NodeMutation.new(source, adapter: @rewriter.parser)
|
55
53
|
@current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source)
|
56
|
-
@mutation_adapter = NodeMutation.adapter
|
57
54
|
begin
|
58
55
|
node = parse_code(@file_path, encoded_source)
|
59
56
|
|
@@ -81,10 +78,9 @@ module Synvert::Core
|
|
81
78
|
def test
|
82
79
|
absolute_file_path = File.join(Configuration.root_path, file_path)
|
83
80
|
source = read_source(absolute_file_path)
|
84
|
-
@current_mutation = NodeMutation.new(source)
|
81
|
+
@current_mutation = NodeMutation.new(source, adapter: @rewriter.parser)
|
85
82
|
encoded_source = Engine.encode(File.extname(file_path), source)
|
86
83
|
@current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source)
|
87
|
-
@mutation_adapter = NodeMutation.adapter
|
88
84
|
begin
|
89
85
|
node = parse_code(file_path, encoded_source)
|
90
86
|
|
@@ -108,6 +104,20 @@ module Synvert::Core
|
|
108
104
|
@current_node
|
109
105
|
end
|
110
106
|
|
107
|
+
# Get rewriter's adapter.
|
108
|
+
#
|
109
|
+
# @return [String] adapter
|
110
|
+
def parser
|
111
|
+
@rewriter.parser
|
112
|
+
end
|
113
|
+
|
114
|
+
# Gent current_mutation's adapter.
|
115
|
+
#
|
116
|
+
# @return [NodeMutation::Adapter]
|
117
|
+
def mutation_adapter
|
118
|
+
@current_mutation.adapter
|
119
|
+
end
|
120
|
+
|
111
121
|
# Set current_node to node and process.
|
112
122
|
#
|
113
123
|
# @param node [Parser::AST::Node] node set to current_node
|
@@ -279,7 +289,7 @@ module Synvert::Core
|
|
279
289
|
# @param to [String] where to insert, if it is nil, will insert to current node.
|
280
290
|
# @param and_comma [Boolean] insert extra comma.
|
281
291
|
def insert_after(code, to: nil, and_comma: false)
|
282
|
-
column = ' ' *
|
292
|
+
column = ' ' * @current_mutation.adapter.get_start_loc(@current_node, to).column
|
283
293
|
@current_mutation.insert(@current_node, "\n#{column}#{code}", at: 'end', to: to, and_comma: and_comma)
|
284
294
|
end
|
285
295
|
|
@@ -296,7 +306,7 @@ module Synvert::Core
|
|
296
306
|
# @param to [String] where to insert, if it is nil, will insert to current node.
|
297
307
|
# @param and_comma [Boolean] insert extra comma.
|
298
308
|
def insert_before(code, to: nil, and_comma: false)
|
299
|
-
column = ' ' *
|
309
|
+
column = ' ' * @current_mutation.adapter.get_start_loc(@current_node, to).column
|
300
310
|
@current_mutation.insert(@current_node, "#{code}\n#{column}", at: 'beginning', to: to, and_comma: and_comma)
|
301
311
|
end
|
302
312
|
|
@@ -313,7 +323,7 @@ module Synvert::Core
|
|
313
323
|
def replace_erb_stmt_with_expr
|
314
324
|
absolute_file_path = File.join(Configuration.root_path, @file_path)
|
315
325
|
erb_source = read_source(absolute_file_path)
|
316
|
-
action = Rewriter::ReplaceErbStmtWithExprAction.new(@current_node, erb_source)
|
326
|
+
action = Rewriter::ReplaceErbStmtWithExprAction.new(@current_node, erb_source, adapter: @current_mutation.adapter)
|
317
327
|
add_action(action)
|
318
328
|
end
|
319
329
|
|
@@ -480,7 +490,7 @@ module Synvert::Core
|
|
480
490
|
# @param encoded_source [String] encoded source code
|
481
491
|
# @return [Node] ast node for file
|
482
492
|
def parse_code(file_path, encoded_source)
|
483
|
-
if @rewriter.
|
493
|
+
if @rewriter.parser == Synvert::SYNTAX_TREE_PARSER
|
484
494
|
parse_code_by_syntax_tree(file_path, encoded_source)
|
485
495
|
else
|
486
496
|
parse_code_by_parser(file_path, encoded_source)
|
@@ -14,7 +14,7 @@ module Synvert::Core
|
|
14
14
|
super(instance, &block)
|
15
15
|
|
16
16
|
@options = { including_self: true, stop_at_first_match: false, recursive: true }.merge(options)
|
17
|
-
@node_query = NodeQuery.new(nql_or_rules)
|
17
|
+
@node_query = NodeQuery.new(nql_or_rules, adapter: instance.parser)
|
18
18
|
end
|
19
19
|
|
20
20
|
# Find out the matching nodes.
|
@@ -129,9 +129,7 @@ module Synvert::Core
|
|
129
129
|
# It will call the block.
|
130
130
|
def process
|
131
131
|
@affected_files = Set.new
|
132
|
-
|
133
|
-
instance_eval(&@block)
|
134
|
-
end
|
132
|
+
instance_eval(&@block)
|
135
133
|
|
136
134
|
process if !@affected_files.empty? && @redo_until_no_change # redo
|
137
135
|
end
|
@@ -146,9 +144,7 @@ module Synvert::Core
|
|
146
144
|
def test
|
147
145
|
@options[:write_to_file] = false
|
148
146
|
@affected_files = Set.new
|
149
|
-
|
150
|
-
instance_eval(&@block)
|
151
|
-
end
|
147
|
+
instance_eval(&@block)
|
152
148
|
|
153
149
|
if !@affected_files.empty? && @redo_until_no_change # redo
|
154
150
|
test
|
@@ -186,8 +182,8 @@ module Synvert::Core
|
|
186
182
|
@affected_files.add(file_path)
|
187
183
|
end
|
188
184
|
|
189
|
-
def
|
190
|
-
@options[:parser]
|
185
|
+
def parser
|
186
|
+
@options[:parser]
|
191
187
|
end
|
192
188
|
|
193
189
|
#######
|
@@ -206,14 +202,6 @@ module Synvert::Core
|
|
206
202
|
if options[:parser] && ![Synvert::PARSER_PARSER, Synvert::SYNTAX_TREE_PARSER].include?(options[:parser])
|
207
203
|
raise Errors::ParserNotSupported.new("Parser #{options[:adapter]} not supported")
|
208
204
|
end
|
209
|
-
|
210
|
-
if syntax_tree_parser?
|
211
|
-
NodeQuery.configure(adapter: NodeQuery::SyntaxTreeAdapter.new)
|
212
|
-
NodeMutation.configure(adapter: NodeMutation::SyntaxTreeAdapter.new)
|
213
|
-
else
|
214
|
-
NodeQuery.configure(adapter: NodeQuery::ParserAdapter.new)
|
215
|
-
NodeMutation.configure(adapter: NodeMutation::ParserAdapter.new)
|
216
|
-
end
|
217
205
|
end
|
218
206
|
|
219
207
|
# It sets description of the rewrite or get description.
|
@@ -417,18 +405,6 @@ module Synvert::Core
|
|
417
405
|
|
418
406
|
private
|
419
407
|
|
420
|
-
# Ensure back to current adapter after running a rewriter.
|
421
|
-
def ensure_current_adapter
|
422
|
-
current_query_adapter = NodeQuery.adapter
|
423
|
-
current_mutation_adapter = NodeMutation.adapter
|
424
|
-
begin
|
425
|
-
yield
|
426
|
-
ensure
|
427
|
-
NodeQuery.configure(adapter: current_query_adapter)
|
428
|
-
NodeMutation.configure(adapter: current_mutation_adapter)
|
429
|
-
end
|
430
|
-
end
|
431
|
-
|
432
408
|
# Handle one file.
|
433
409
|
# @param file_patterns [String] file patterns to find files.
|
434
410
|
# @yield [file_path] block to handle file.
|
data/lib/synvert/core/version.rb
CHANGED
@@ -8,8 +8,8 @@ module Synvert::Core
|
|
8
8
|
subject {
|
9
9
|
erb_source = "<% form_for post do |f| %>\n<% end %>"
|
10
10
|
source = Engine::Erb.encode(erb_source)
|
11
|
-
node =
|
12
|
-
described_class.new(node, erb_source).process
|
11
|
+
node = parser_parse(source).children.first
|
12
|
+
described_class.new(node, erb_source, adapter: NodeMutation::ParserAdapter.new).process
|
13
13
|
}
|
14
14
|
|
15
15
|
it 'gets start' do
|
@@ -29,8 +29,8 @@ module Synvert::Core
|
|
29
29
|
subject {
|
30
30
|
erb_source = "<%form_for post do |f|%>\n<%end%>"
|
31
31
|
source = Engine::Erb.encode(erb_source)
|
32
|
-
node =
|
33
|
-
described_class.new(node, erb_source).process
|
32
|
+
node = parser_parse(source).children.first
|
33
|
+
described_class.new(node, erb_source, adapter: NodeMutation::ParserAdapter.new).process
|
34
34
|
}
|
35
35
|
|
36
36
|
it 'gets start' do
|
@@ -3,17 +3,15 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
|
-
describe Rewriter::IfExistCondition do
|
7
|
-
let(:source) {
|
8
|
-
'
|
6
|
+
RSpec.describe Rewriter::IfExistCondition do
|
7
|
+
let(:source) { <<~EOS }
|
9
8
|
RSpec.configure do |config|
|
10
9
|
config.include EmailSpec::Helpers
|
11
10
|
config.include EmailSpec::Methods
|
12
11
|
end
|
13
|
-
|
14
|
-
}
|
12
|
+
EOS
|
15
13
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
16
|
-
let(:instance) { double(current_node: node) }
|
14
|
+
let(:instance) { double(current_node: node, parser: :parser) }
|
17
15
|
|
18
16
|
describe '#process' do
|
19
17
|
it 'call block if match anything' do
|
@@ -4,22 +4,20 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::IfOnlyExistCondition do
|
7
|
-
let(:source) {
|
8
|
-
'
|
7
|
+
let(:source) { <<~EOS }
|
9
8
|
RSpec.configure do |config|
|
10
9
|
config.include EmailSpec::Helpers
|
11
10
|
config.include EmailSpec::Methods
|
12
11
|
end
|
13
|
-
|
14
|
-
}
|
12
|
+
EOS
|
15
13
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
16
|
-
let(:instance) { double(current_node: node) }
|
14
|
+
let(:instance) { double(current_node: node, parser: :parser) }
|
17
15
|
|
18
16
|
describe '#process' do
|
19
17
|
it 'gets matching nodes' do
|
20
18
|
source = ' RSpec.configure do |config| config.include EmailSpec::Helpers end '
|
21
19
|
node = Parser::CurrentRuby.parse(source)
|
22
|
-
instance = double(current_node: node)
|
20
|
+
instance = double(current_node: node, parser: :parser)
|
23
21
|
run = false
|
24
22
|
condition =
|
25
23
|
Rewriter::IfOnlyExistCondition.new instance,
|
@@ -4,16 +4,14 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::UnlessExistCondition do
|
7
|
-
let(:source) {
|
8
|
-
'
|
7
|
+
let(:source) { <<~EOS }
|
9
8
|
RSpec.configure do |config|
|
10
9
|
config.include EmailSpec::Helpers
|
11
10
|
config.include EmailSpec::Methods
|
12
11
|
end
|
13
|
-
|
14
|
-
}
|
12
|
+
EOS
|
15
13
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
16
|
-
let(:instance) { double(current_node: node) }
|
14
|
+
let(:instance) { double(current_node: node, parser: :parser) }
|
17
15
|
|
18
16
|
describe '#process' do
|
19
17
|
it 'call block if match anything' do
|
@@ -148,9 +148,8 @@ module Synvert::Core
|
|
148
148
|
end
|
149
149
|
|
150
150
|
it 'parses insert_after' do
|
151
|
-
|
151
|
+
expect(@current_mutation).to receive_message_chain(:adapter, :get_start_loc, :column).and_return(2)
|
152
152
|
instance.current_node = double
|
153
|
-
expect(NodeMutation).to receive_message_chain(:adapter, :get_start_loc, :column).and_return(2)
|
154
153
|
expect(instance.instance_variable_get(:@current_mutation)).to receive(:insert).with(
|
155
154
|
instance.current_node,
|
156
155
|
"\n Foobar",
|
@@ -162,9 +161,8 @@ module Synvert::Core
|
|
162
161
|
end
|
163
162
|
|
164
163
|
it 'parses insert_before' do
|
165
|
-
|
164
|
+
expect(@current_mutation).to receive_message_chain(:adapter, :get_start_loc, :column).and_return(2)
|
166
165
|
instance.current_node = double
|
167
|
-
expect(NodeMutation).to receive_message_chain(:adapter, :get_start_loc, :column).and_return(2)
|
168
166
|
expect(instance.instance_variable_get(:@current_mutation)).to receive(:insert).with(
|
169
167
|
instance.current_node,
|
170
168
|
"Foobar\n ",
|
@@ -176,7 +174,8 @@ module Synvert::Core
|
|
176
174
|
end
|
177
175
|
|
178
176
|
it 'parses replace_erb_stmt_with_expr' do
|
179
|
-
|
177
|
+
adapter = NodeMutation::ParserAdapter.new
|
178
|
+
instance.instance_variable_set(:@current_mutation, double(adapter: adapter))
|
180
179
|
instance.current_node = double
|
181
180
|
action = double
|
182
181
|
erb_source = '<% form_for @post do |f| %><% end %>'
|
@@ -184,7 +183,8 @@ module Synvert::Core
|
|
184
183
|
expect(instance.instance_variable_get(:@current_mutation)).to receive(:actions).and_return([])
|
185
184
|
expect(Rewriter::ReplaceErbStmtWithExprAction).to receive(:new).with(
|
186
185
|
instance.current_node,
|
187
|
-
erb_source
|
186
|
+
erb_source,
|
187
|
+
adapter: adapter
|
188
188
|
).and_return(action)
|
189
189
|
expect(action).to receive(:process)
|
190
190
|
instance.replace_erb_stmt_with_expr
|
@@ -275,7 +275,7 @@ module Synvert::Core
|
|
275
275
|
end
|
276
276
|
|
277
277
|
it 'adds action' do
|
278
|
-
mutation = NodeMutation.new("")
|
278
|
+
mutation = NodeMutation.new("", adapter: :parser)
|
279
279
|
instance.instance_variable_set(:@current_mutation, mutation)
|
280
280
|
action = double
|
281
281
|
expect(action).to receive(:process).and_return(action)
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::WithinScope do
|
7
7
|
let(:instance) {
|
8
|
-
rewriter = Rewriter.new('foo', 'bar')
|
8
|
+
rewriter = Rewriter.new('foo', 'bar') { configure(parser: PARSER_PARSER) }
|
9
9
|
Rewriter::Instance.new(rewriter, 'file pattern')
|
10
10
|
}
|
11
11
|
let(:source) { <<~EOS }
|
@@ -6,25 +6,20 @@ module Synvert::Core
|
|
6
6
|
RSpec.describe Rewriter do
|
7
7
|
describe '#configure' do
|
8
8
|
it 'parses parser' do
|
9
|
-
running_query_adapter = nil
|
10
9
|
running_mutation_adapter = nil
|
11
10
|
rewriter =
|
12
11
|
Rewriter.new 'group', 'name' do
|
13
12
|
configure parser: 'syntax_tree'
|
14
13
|
|
15
14
|
within_files '**/*.rb' do
|
16
|
-
|
17
|
-
running_mutation_adapter = NodeMutation.adapter
|
15
|
+
running_mutation_adapter = mutation_adapter
|
18
16
|
end
|
19
17
|
end
|
20
18
|
input = "class Foobar\nend"
|
21
19
|
FakeFS do
|
22
20
|
File.write("code.rb", input)
|
23
21
|
rewriter.process
|
24
|
-
expect(running_query_adapter).to be_instance_of(NodeQuery::SyntaxTreeAdapter)
|
25
22
|
expect(running_mutation_adapter).to be_instance_of(NodeMutation::SyntaxTreeAdapter)
|
26
|
-
expect(NodeQuery.adapter).to be_instance_of(NodeQuery::ParserAdapter)
|
27
|
-
expect(NodeMutation.adapter).to be_instance_of(NodeMutation::ParserAdapter)
|
28
23
|
end
|
29
24
|
end
|
30
25
|
|
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: 1.
|
4
|
+
version: 1.30.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: 2023-11-
|
11
|
+
date: 2023-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|