synvert-core 1.29.4 → 1.30.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc9784d9012764a97bb61fea6c37d32b82832e9591b8065403a01b2f1e18e610
4
- data.tar.gz: a7fcd9da422493f5e1b8d39fefe12050ef020b1b1580d99bfcbd3e6eb18e69ca
3
+ metadata.gz: a5923465a97b00d151c7877fb725f5f74c63975c9683d926a4bf14e2feae8fc8
4
+ data.tar.gz: 94ed7f17447b0d501243b6074f9b4c2e57b09c5954b013727dc052933ac5e8fc
5
5
  SHA512:
6
- metadata.gz: 9718d0a8a344fb024dc6c778e6aa45a7aafbdb21eb627324cb21e083ac0705813344abc23af72cff45465d2651ed2497438365d1685f60ff0b130b6dfedcede5
7
- data.tar.gz: 2bd452b4b303a2ee985c14f124074c09169dd19d2cc73041cf864298dd29543186645febee87b23017b4dbfc83a7fcafe08aae104c9d3886133b83b8b2769e91
6
+ metadata.gz: c0614ad3287376170341654f290599a0f3541dd26562124935c208d77be666a1073a439090731a42efe62806f4aac91238c0a5eaf79ce971515719ee34384863
7
+ data.tar.gz: 1335bdfaaa06f1d8f8cca811d3e0fd4a9d40ca1f1ac0597a42b110555be59976e299cf9147c785c8c8d4ad418251a7fa0c7f2de7967ef71d77c46cbf86548076
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.30.1 (2023-12-01)
4
+
5
+ * It is `parser` option instead of `adapter`
6
+
7
+ ## 1.30.0 (2023-11-27)
8
+
9
+ * Update `node_mutation` to 1.22.0
10
+ * Update `node_query` to 1.14.1
11
+ * Add `adapter` as `Rewriter::ReplaceErbStmtWithExprAction` parameter
12
+ * Initialize `NodeQuery` and `NodeMutation` with `adapter`
13
+ * Remove `ensure_current_adapter` as adapter is an instance variable of Rewriter
14
+
3
15
  ## 1.29.4 (2023-11-24)
4
16
 
5
17
  * 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.29.4)
4
+ synvert-core (1.30.1)
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.21.6)
54
- node_query (1.13.12)
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
- def initialize(node, erb_source)
12
- super(node, nil)
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 = NodeMutation.adapter.get_start(@node)
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
- # @!attribute [r] mutation_adapter
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 = ' ' * NodeMutation.adapter.get_start_loc(@current_node, to).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 = ' ' * NodeMutation.adapter.get_start_loc(@current_node, to).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.syntax_tree_parser?
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
- ensure_current_adapter do
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
- ensure_current_adapter do
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 syntax_tree_parser?
190
- @options[:parser] == Synvert::SYNTAX_TREE_PARSER
185
+ def parser
186
+ @options[:parser]
191
187
  end
192
188
 
193
189
  #######
@@ -199,20 +195,12 @@ module Synvert::Core
199
195
  # configure({ parser: Synvert::PARSER_PARSER })
200
196
  # configure({ strategy: 'allow_insert_at_same_position' })
201
197
  # @param options [Hash]
202
- # @option adapter [String] Synvert::PARSER_PARSER or Synvert::SYNTAX_TREE_PARSER
198
+ # @option parser [String] Synvert::PARSER_PARSER or Synvert::SYNTAX_TREE_PARSER
203
199
  # @option strategy [String] 'allow_insert_at_same_position'
204
200
  def configure(options)
205
201
  @options = @options.merge(options)
206
202
  if options[:parser] && ![Synvert::PARSER_PARSER, Synvert::SYNTAX_TREE_PARSER].include?(options[:parser])
207
- raise Errors::ParserNotSupported.new("Parser #{options[:adapter]} not supported")
208
- 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)
203
+ raise Errors::ParserNotSupported.new("Parser #{options[:parser]} not supported")
216
204
  end
217
205
  end
218
206
 
@@ -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.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.29.4'
5
+ VERSION = '1.30.1'
6
6
  end
7
7
  end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'parser/current'
4
+ require 'parser_node_ext'
5
+
3
6
  module ParserHelper
4
7
  def parser_parse(code)
5
8
  Parser::CurrentRuby.parse code
@@ -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 = Parser::CurrentRuby.parse(source).children.first
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 = Parser::CurrentRuby.parse(source).children.first
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
- instance.instance_variable_set(:@current_mutation, double)
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
- instance.instance_variable_set(:@current_mutation, double)
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
- instance.instance_variable_set(:@current_mutation, double)
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
- running_query_adapter = NodeQuery.adapter
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.29.4
4
+ version: 1.30.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: 2023-11-24 00:00:00.000000000 Z
11
+ date: 2023-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport