synvert-core 1.27.1 → 1.28.1

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: 76a57aa5fe7a0dfc34c17b5afb0be7657f08b29e7efa7aceb85350a5d7571260
4
- data.tar.gz: 6706bbbce5a521aaa00b625ae1c0052241485f7eb230f5c99af68466b0effc7e
3
+ metadata.gz: 4fb6d702f10d7ac7f1aace3ed75a7706e7c293475b216895505b2a6ec3d3d5ef
4
+ data.tar.gz: f9f1f1ef09c986afa9a6553778b4d099bad26486aad1eb316468d6832013c6a8
5
5
  SHA512:
6
- metadata.gz: 2b87be0f4fc6ce7aa88c96644182cb7c479a89104cee0a43779b1e2345cc1e24709e360970896890000042e2f80d8fd297eec435a499ebd28feb1abd283ff6f5
7
- data.tar.gz: bc08baeaa494b5307428ec5e2ce4e88d639f8262200fed27d8b5dae4da646e5c0b470be3ceb7f6e55650b4fdcd0daefcb48ca921fafa43763ce34ed91c24e08c
6
+ metadata.gz: b3fbd20c3d94848a6a0dd2986f56e9da445146aaa0c3c65439e5160ae7dafdac8adfc25a4f3c74ee6498f5f59fcaec4cc6584fcfd4a2c97884d872adac86f5b6
7
+ data.tar.gz: 5c0d934f77c21efbfc7be6ffecff8e39cfa749c2107cc00fdd989620356f0c0dcdc79cc177b35d80c534b4850797196502e3170e15edbb24da55dbe4674cf322
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.28.1 (2023-06-14)
4
+
5
+ * Do not use keyword options in `call_helper`
6
+ * Update `node_query` to 1.13.3 and `node_mutation` to 1.18.3
7
+ * Update `parser_node_ext` to 1.2.0 and `syntax_tree_ext` to 0.6.0
8
+
9
+ ## 1.28.0 (2023-06-01)
10
+
11
+ * Add `Synvert::Core::Helper`
12
+ * Add `call_helper` dsl in rewriter
13
+
3
14
  ## 1.27.1 (2023-05-30)
4
15
 
5
16
  * Puts error message when rescue `Parser::SyntaxError`
data/Gemfile.lock CHANGED
@@ -1,15 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.27.1)
4
+ synvert-core (1.28.1)
5
5
  activesupport (< 7.0.0)
6
- node_mutation (>= 1.18.2)
7
- node_query (>= 1.13.2)
6
+ node_mutation (>= 1.18.3)
7
+ node_query (>= 1.13.3)
8
8
  parallel
9
9
  parser
10
- parser_node_ext (>= 1.1.1)
10
+ parser_node_ext (>= 1.2.0)
11
11
  syntax_tree
12
- syntax_tree_ext (>= 0.3.1)
12
+ syntax_tree_ext (>= 0.6.0)
13
13
 
14
14
  GEM
15
15
  remote: https://rubygems.org/
@@ -41,7 +41,7 @@ GEM
41
41
  guard (~> 2.1)
42
42
  guard-compat (~> 1.1)
43
43
  rspec (>= 2.99.0, < 4.0)
44
- i18n (1.13.0)
44
+ i18n (1.14.1)
45
45
  concurrent-ruby (~> 1.0)
46
46
  listen (3.7.1)
47
47
  rb-fsevent (~> 0.10, >= 0.10.3)
@@ -50,20 +50,22 @@ 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.2)
54
- node_query (1.13.2)
53
+ node_mutation (1.18.3)
54
+ node_query (1.13.3)
55
55
  notiffany (0.1.3)
56
56
  nenv (~> 0.1)
57
57
  shellany (~> 0.0)
58
58
  parallel (1.23.0)
59
- parser (3.2.2.1)
59
+ parser (3.2.2.3)
60
60
  ast (~> 2.4.1)
61
- parser_node_ext (1.1.1)
61
+ racc
62
+ parser_node_ext (1.2.0)
62
63
  parser
63
64
  prettier_print (1.2.1)
64
65
  pry (0.14.1)
65
66
  coderay (~> 1.1)
66
67
  method_source (~> 1.0)
68
+ racc (1.7.1)
67
69
  rake (13.0.6)
68
70
  rb-fsevent (0.11.1)
69
71
  rb-inotify (0.10.1)
@@ -84,7 +86,7 @@ GEM
84
86
  shellany (0.0.1)
85
87
  syntax_tree (6.1.1)
86
88
  prettier_print (>= 1.2.0)
87
- syntax_tree_ext (0.3.1)
89
+ syntax_tree_ext (0.6.0)
88
90
  syntax_tree
89
91
  thor (1.2.1)
90
92
  tzinfo (2.0.6)
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
@@ -41,12 +41,13 @@ module Synvert::Core
41
41
  attr_accessor :current_node
42
42
 
43
43
  # Process the instance.
44
- # It finds specified files, for each file, it executes the block code, rewrites the original code,
44
+ # It executes the block code, rewrites the original code,
45
45
  # then writes the code back to the original file.
46
46
  def process
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
+ # It keeps running until no conflict.
50
51
  loop do
51
52
  source = read_source(absolute_file_path)
52
53
  encoded_source = Engine.encode(File.extname(file_path), source)
@@ -75,7 +76,7 @@ module Synvert::Core
75
76
  end
76
77
 
77
78
  # Test the instance.
78
- # It finds specified files, for each file, it executes the block code, tests the original code,
79
+ # It executes the block code, tests the original code,
79
80
  # then returns the actions.
80
81
  def test
81
82
  absolute_file_path = File.join(Configuration.root_path, file_path)
@@ -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
@@ -236,7 +236,7 @@ module Synvert::Core
236
236
  @gem_spec = Rewriter::GemSpec.new(name, version)
237
237
  end
238
238
 
239
- # It finds specified files.
239
+ # It finds specified files, and for each file, it will delegate to {Synvert::Core::Rewriter::Instance} to rewrite code.
240
240
  # It creates a {Synvert::Core::Rewriter::Instance} to rewrite code.
241
241
  # @example
242
242
  # Synvert::Rewriter.new 'rspec', 'be_close_to_be_within' do
@@ -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', rails_version: '6.1'
364
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/rails/set_load_defaults.rb', rails_version: '6.1'
365
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/rails/set_load_defaults.rb', 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.1'
5
+ VERSION = '1.28.1'
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]
@@ -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', 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
@@ -20,11 +20,11 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_runtime_dependency "activesupport", "< 7.0.0"
23
- spec.add_runtime_dependency "node_query", ">= 1.13.2"
24
- spec.add_runtime_dependency "node_mutation", ">= 1.18.2"
23
+ spec.add_runtime_dependency "node_query", ">= 1.13.3"
24
+ spec.add_runtime_dependency "node_mutation", ">= 1.18.3"
25
25
  spec.add_runtime_dependency "parser"
26
- spec.add_runtime_dependency "parser_node_ext", ">= 1.1.1"
26
+ spec.add_runtime_dependency "parser_node_ext", ">= 1.2.0"
27
27
  spec.add_runtime_dependency "syntax_tree"
28
- spec.add_runtime_dependency "syntax_tree_ext", ">= 0.3.1"
28
+ spec.add_runtime_dependency "syntax_tree_ext", ">= 0.6.0"
29
29
  spec.add_runtime_dependency "parallel"
30
30
  end
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.1
4
+ version: 1.28.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-05-30 00:00:00.000000000 Z
11
+ date: 2023-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -30,28 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.13.2
33
+ version: 1.13.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 1.13.2
40
+ version: 1.13.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: node_mutation
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 1.18.2
47
+ version: 1.18.3
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.2
54
+ version: 1.18.3
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.1
75
+ version: 1.2.0
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.1
82
+ version: 1.2.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: syntax_tree
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: 0.3.1
103
+ version: 0.6.0
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: 0.3.1
110
+ version: 0.6.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: parallel
113
113
  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.10
212
+ rubygems_version: 3.4.13
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