synvert-core 1.27.0 → 1.28.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6505a54441fe208f10c8c57b79d3ba5dab41fe7bc484d5ebaeb39da041775c9b
4
- data.tar.gz: a198d79b76780f320d159a379eb8cc05e30e74419a8759d37f9c2eb9fe83c902
3
+ metadata.gz: 5d42815151ad1672667f0bc09cc57083b0620722a8416f2d6e30ebb1f6faabe5
4
+ data.tar.gz: 51ebfcd1b6022a91e1739974d89c6172f7358ebb3ddae69132408a6f07b76564
5
5
  SHA512:
6
- metadata.gz: f76e819dc63a12e5c7e50f77f5f96e104b22181d1748a897a445b11b7fd1ca1f8342855c4f3911cd0848747c3581d18784bf7bc2670bef866084fea1bff82811
7
- data.tar.gz: 77ffc3abe94e91b73d2e4ffa06455f4378687d833e0911f4716a1ea9cd5a7149bdac4af32c23e3183c7a6a784a064b12dcafc4171afdd083a911eb9fe899344c
6
+ metadata.gz: 2bf88532f763fa4c3e4b3c7f12d914ad383c838c8b99ca2faf897e50390e409f941b7b59a8f0dde7350d3c1d29bacc972c8d36abb5aee814ff2337eb2dfc9409
7
+ data.tar.gz: d5ebb445329da6a3b93d34174150238753b299e0a67b7600a0286e19971cb81a789f3a1017e78729439b68e6ceea0f8cf402cfd1e0eab2d51b360f8f5d6755d8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.28.0 (2023-06-01)
4
+
5
+ * Add `Synvert::Core::Helper`
6
+ * Add `call_helper` dsl in rewriter
7
+
8
+ ## 1.27.1 (2023-05-30)
9
+
10
+ * Puts error message when rescue `Parser::SyntaxError`
11
+ * Update `node_mutation` to 1.18.2
12
+
3
13
  ## 1.27.0 (2023-05-16)
4
14
 
5
15
  * Configure `parser`, can be `parser` or `syntax_tree`
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.27.0)
4
+ synvert-core (1.28.0)
5
5
  activesupport (< 7.0.0)
6
- node_mutation (>= 1.18.0)
6
+ node_mutation (>= 1.18.2)
7
7
  node_query (>= 1.13.2)
8
8
  parallel
9
9
  parser
10
- parser_node_ext (>= 1.1.0)
10
+ parser_node_ext (>= 1.1.1)
11
11
  syntax_tree
12
12
  syntax_tree_ext (>= 0.3.1)
13
13
 
@@ -50,7 +50,7 @@ GEM
50
50
  method_source (1.0.0)
51
51
  minitest (5.18.0)
52
52
  nenv (0.3.0)
53
- node_mutation (1.18.0)
53
+ node_mutation (1.18.2)
54
54
  node_query (1.13.2)
55
55
  notiffany (0.1.3)
56
56
  nenv (~> 0.1)
@@ -58,7 +58,7 @@ GEM
58
58
  parallel (1.23.0)
59
59
  parser (3.2.2.1)
60
60
  ast (~> 2.4.1)
61
- parser_node_ext (1.1.0)
61
+ parser_node_ext (1.1.1)
62
62
  parser
63
63
  prettier_print (1.2.1)
64
64
  pry (0.14.1)
data/README.md CHANGED
@@ -69,6 +69,7 @@ DSLs are as follows
69
69
  * [remove_file](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#remove_file-instance_method) - remove a file
70
70
  * [helper_method](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#helper_method-instance_method) - define a helper method
71
71
  * [add_snippet](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#add_snippet-instance_method) - call another rewriter
72
+ * [call_helper](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#call_helper-instance_method) - call a shared rewriter
72
73
  * [redo_until_no_change](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#redo_until_no_change-instance_method) - run the snippet until no change
73
74
 
74
75
  Scopes:
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Synvert::Core
4
+ # Helper is used to define shared snippet.
5
+ class Helper
6
+ attr_reader :name, :block
7
+
8
+ class << self
9
+ # Register a helper with its name.
10
+ #
11
+ # @param name [String] the unique rewriter name.
12
+ # @param helper [Synvert::Core::Helper] the helper to register.
13
+ def register(name, helper)
14
+ name = name.to_s
15
+ helpers[name] = helper
16
+ end
17
+
18
+ # Fetch a helper by name.
19
+ #
20
+ # @param name [String] rewrtier name.
21
+ # @return [Synvert::Core::Helper] the matching helper.
22
+ def fetch(name)
23
+ name = name.to_s
24
+ helpers[name]
25
+ end
26
+
27
+ # Get all available helpers
28
+ #
29
+ # @return [Hash<String, Synvert::Core::Helper>]
30
+ def availables
31
+ helpers
32
+ end
33
+
34
+ # Clear all registered helpers.
35
+ def clear
36
+ helpers.clear
37
+ end
38
+
39
+ private
40
+
41
+ def helpers
42
+ @helpers ||= {}
43
+ end
44
+ end
45
+
46
+ # Initialize a Helper.
47
+ # When a helper is initialized, it is already registered.
48
+ #
49
+ # @param name [String] name of the helper.
50
+ # @yield defines the behaviors of the helper, block code won't be called when initialization.
51
+ def initialize(name, &block)
52
+ @name = name
53
+ @block = block
54
+ self.class.register(name, self)
55
+ end
56
+ end
57
+ end
@@ -47,7 +47,7 @@ module Synvert::Core
47
47
  puts @file_path if Configuration.show_run_process
48
48
 
49
49
  absolute_file_path = File.join(Configuration.root_path, @file_path)
50
- while true
50
+ loop do
51
51
  source = read_source(absolute_file_path)
52
52
  encoded_source = Engine.encode(File.extname(file_path), source)
53
53
  @current_mutation = NodeMutation.new(source)
@@ -66,9 +66,10 @@ module Synvert::Core
66
66
  write_source(absolute_file_path, result.new_source)
67
67
  end
68
68
  break unless result.conflicted?
69
- rescue Parser::SyntaxError
69
+ rescue Parser::SyntaxError => e
70
70
  puts "[Warn] file #{file_path} was not parsed correctly."
71
- # do nothing, iterate next file
71
+ puts e.message
72
+ break
72
73
  end
73
74
  end
74
75
  end
@@ -93,9 +94,9 @@ module Synvert::Core
93
94
  result = @current_mutation.test
94
95
  result.file_path = file_path
95
96
  result
96
- rescue Parser::SyntaxError
97
+ rescue Parser::SyntaxError => e
97
98
  puts "[Warn] file #{file_path} was not parsed correctly."
98
- # do nothing, iterate next file
99
+ puts e.message
99
100
  end
100
101
  end
101
102
 
@@ -57,7 +57,7 @@ module Synvert::Core
57
57
 
58
58
  # Get all available rewriters
59
59
  #
60
- # @return [Hash<String, Hash<String, Rewriter>>]
60
+ # @return [Hash<String, Hash<String, Synvert::Core::Rewriter>>]
61
61
  def availables
62
62
  rewriters
63
63
  end
@@ -253,13 +253,13 @@ module Synvert::Core
253
253
 
254
254
  if @options[:write_to_file]
255
255
  handle_one_file(Array(file_patterns)) do |file_path|
256
- instance = Rewriter::Instance.new(self, file_path, &block)
256
+ instance = Instance.new(self, file_path, &block)
257
257
  instance.process
258
258
  end
259
259
  else
260
260
  results =
261
261
  handle_one_file(Array(file_patterns)) do |file_path|
262
- instance = Rewriter::Instance.new(self, file_path, &block)
262
+ instance = Instance.new(self, file_path, &block)
263
263
  instance.test
264
264
  end
265
265
  merge_test_results(results)
@@ -357,6 +357,22 @@ module Synvert::Core
357
357
  @sub_snippets << rewriter
358
358
  end
359
359
 
360
+ # It calls a shared rewriter.
361
+ # @example
362
+ # Synvert::Rewriter.new 'rails', 'upgrade_6_0_to_6_1' do
363
+ # call_helper 'rails/set_load_defaults', options: { rails_version: '6.1' }
364
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/rails/set_load_defaults.rb', options: { rails_version: '6.1' }
365
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/rails/set_load_defaults.rb', options: { rails_version: '6.1' }
366
+ # end
367
+ # @param name [String] name of helper.
368
+ # @param options [Hash] options to pass to helper.
369
+ def call_helper(name, options: {})
370
+ helper = Synvert::Core::Helper.fetch(name) || Utils.eval_snippet(name)
371
+ return unless helper && helper.is_a?(Synvert::Core::Helper)
372
+
373
+ instance_exec(options, &helper.block)
374
+ end
375
+
360
376
  # It defines helper method for {Synvert::Core::Rewriter::Instance}.
361
377
  # @example
362
378
  # Synvert::Rewriter.new 'rails', 'convert_active_record_dirty_5_0_to_5_1' do
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.27.0'
5
+ VERSION = '1.28.0'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -11,6 +11,7 @@ module Synvert
11
11
  module Core
12
12
  autoload :Configuration, 'synvert/core/configuration'
13
13
  autoload :Rewriter, 'synvert/core/rewriter'
14
+ autoload :Helper, 'synvert/core/helper'
14
15
  autoload :Engine, 'synvert/core/engine'
15
16
  autoload :Utils, 'synvert/core/utils'
16
17
  autoload :Strategy, 'synvert/core/strategy'
@@ -20,6 +21,7 @@ end
20
21
 
21
22
  module Synvert
22
23
  Rewriter = Core::Rewriter
24
+ Helper = Core::Helper
23
25
  Strategy = Core::Strategy
24
26
 
25
27
  ALL_RUBY_FILES = %w[**/*.rb]
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
- require 'pp'
5
+ require 'pp' # rubocop:disable Lint/RedundantRequireStatement
6
6
  require 'synvert/core'
7
7
  require 'fakefs/spec_helpers'
8
8
 
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Synvert::Core
6
+ RSpec.describe Helper do
7
+ describe 'class methods' do
8
+ before :each do
9
+ described_class.clear
10
+ end
11
+
12
+ it 'registers and fetches' do
13
+ helper = described_class.new 'helper' do; end
14
+ expect(described_class.fetch('helper')).to eq helper
15
+ end
16
+
17
+ context 'available' do
18
+ it 'lists empty helpers' do
19
+ expect(described_class.availables).to eq({})
20
+ end
21
+
22
+ it 'registers and lists all available helpers' do
23
+ helper1 = Helper.new 'helper1' do; end
24
+ helper2 = Helper.new 'helper2' do; end
25
+ expect(Helper.availables).to eq({ 'helper1' => helper1, 'helper2' => helper2 })
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -309,6 +309,47 @@ module Synvert::Core
309
309
  end
310
310
  end
311
311
 
312
+ describe 'parses call_helper' do
313
+ it 'eval helper by name' do
314
+ block_receiver = nil
315
+ block_options = {}
316
+ helper =
317
+ Synvert::Helper.new 'helper' do |options|
318
+ block_receiver = self.class.name
319
+ block_options = options
320
+ end
321
+ rewriter =
322
+ Rewriter.new 'group', 'rewriter' do
323
+ call_helper('helper', options: { foo: 'bar' })
324
+ end
325
+ rewriter.process
326
+ expect(block_receiver).to eq 'Synvert::Core::Rewriter'
327
+ expect(block_options).to eq(foo: 'bar')
328
+ end
329
+
330
+ it 'adds snippet by http url' do
331
+ expect(Utils).to receive(:remote_snippet_exists?).with(URI.parse('http://synvert.net/foo/bar.rb')).and_return(true)
332
+ expect_any_instance_of(URI::HTTP).to receive(:open).and_return(StringIO.new("Synvert::Helper.new 'helper' do\nend"))
333
+ rewriter =
334
+ Rewriter.new 'group', 'rewriter' do
335
+ call_helper 'http://synvert.net/foo/bar.rb'
336
+ end
337
+ rewriter.process
338
+ expect(Synvert::Helper.fetch('helper')).not_to be_nil
339
+ end
340
+
341
+ it 'adds snippet by file path' do
342
+ expect(File).to receive(:exist?).and_return(true)
343
+ expect(File).to receive(:read).and_return("Synvert::Helper.new 'helper' do\nend")
344
+ rewriter =
345
+ Rewriter.new 'group', 'rewriter' do
346
+ call_helper '/home/richard/foo/bar.rb'
347
+ end
348
+ rewriter.process
349
+ expect(Synvert::Helper.fetch('helper')).not_to be_nil
350
+ end
351
+ end
352
+
312
353
  it 'parses helper_method' do
313
354
  rewriter =
314
355
  Rewriter.new 'group', 'name' do
@@ -21,9 +21,9 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_runtime_dependency "activesupport", "< 7.0.0"
23
23
  spec.add_runtime_dependency "node_query", ">= 1.13.2"
24
- spec.add_runtime_dependency "node_mutation", ">= 1.18.0"
24
+ spec.add_runtime_dependency "node_mutation", ">= 1.18.2"
25
25
  spec.add_runtime_dependency "parser"
26
- spec.add_runtime_dependency "parser_node_ext", ">= 1.1.0"
26
+ spec.add_runtime_dependency "parser_node_ext", ">= 1.1.1"
27
27
  spec.add_runtime_dependency "syntax_tree"
28
28
  spec.add_runtime_dependency "syntax_tree_ext", ">= 0.3.1"
29
29
  spec.add_runtime_dependency "parallel"
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.27.0
4
+ version: 1.28.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-05-18 00:00:00.000000000 Z
11
+ date: 2023-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 1.18.0
47
+ version: 1.18.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 1.18.0
54
+ version: 1.18.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: parser
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: 1.1.0
75
+ version: 1.1.1
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 1.1.0
82
+ version: 1.1.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: syntax_tree
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -148,6 +148,7 @@ files:
148
148
  - lib/synvert/core/engine/haml.rb
149
149
  - lib/synvert/core/engine/slim.rb
150
150
  - lib/synvert/core/errors.rb
151
+ - lib/synvert/core/helper.rb
151
152
  - lib/synvert/core/node_ext.rb
152
153
  - lib/synvert/core/rewriter.rb
153
154
  - lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
@@ -171,6 +172,7 @@ files:
171
172
  - spec/synvert/core/engine/erb_spec.rb
172
173
  - spec/synvert/core/engine/haml_spec.rb
173
174
  - spec/synvert/core/engine/slim_spec.rb
175
+ - spec/synvert/core/helper_spec.rb
174
176
  - spec/synvert/core/node_ext_spec.rb
175
177
  - spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
176
178
  - spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
@@ -207,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
209
  - !ruby/object:Gem::Version
208
210
  version: '0'
209
211
  requirements: []
210
- rubygems_version: 3.4.7
212
+ rubygems_version: 3.4.10
211
213
  signing_key:
212
214
  specification_version: 4
213
215
  summary: convert ruby code to better syntax.
@@ -217,6 +219,7 @@ test_files:
217
219
  - spec/synvert/core/engine/erb_spec.rb
218
220
  - spec/synvert/core/engine/haml_spec.rb
219
221
  - spec/synvert/core/engine/slim_spec.rb
222
+ - spec/synvert/core/helper_spec.rb
220
223
  - spec/synvert/core/node_ext_spec.rb
221
224
  - spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
222
225
  - spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb