synvert-core 1.33.3 → 1.35.0

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: a0b171e345173ce6e1519cc68c1fd4aca91e796d56554ba63875c0402b3b202f
4
- data.tar.gz: 218dcbcacb00ff39d4de981dc76178c552081577fe4cfaeadad291a1b42b7d46
3
+ metadata.gz: 73d980abab80a00253b41153d665ee2036c4111d8c68fad0884e767e121f20a9
4
+ data.tar.gz: 3f83634246155bc0be00a09ff1ba4c43e31160e9b9ce8337e3c20f39b40392f1
5
5
  SHA512:
6
- metadata.gz: 6c96410cb92044bfeca2ebbc07cd2340973bd3320a10060c4bdad4a95db48d31d62e358a9929c40639904633dd9cd09238d34b3ce877b51d2af1a7a920cb8747
7
- data.tar.gz: '0702947b02c043b28fc52456497e03834e94407514ccc34ae9be9f562a885cffbd9f08f0e598fe0df6d03e9c36bc4f521c1169079de748c0fadc082f76865bac'
6
+ metadata.gz: c28dc61a913dfe6b7f65ac76cad9eed4ff71c83b09c5ae26901bb8333707a00d050062f2bdc60ceec32f0a9dafbc6f5bd9a49af687781e1f815a469abb8fe5c0
7
+ data.tar.gz: 3a126aa7dfca96796d8b07aeb666630cf30b12252e3f08f6e5f98b09e998634db0b9f708029ffc975348ca1b713e1e03f317a5740498be2fa6121c20b347f92d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.35.0 (2024-04-07)
4
+
5
+ * Update `prism_ext` to 0.3.0
6
+ * Update `syntax_tree_ext` to 0.8.1
7
+ * Update `parser_node_ext` to 1.3.0
8
+ * Update `node_query` to 1.15.2
9
+ * Update `node_mutation` to 1.24.3
10
+ * Remove `if_only_exist_node` dsl
11
+
12
+ ## 1.34.0 (2024-02-29)
13
+
14
+ * Add `with_configurations` dsl
15
+ * Add `save_data` dsl
16
+ * Add `load_data` dsl
17
+
3
18
  ## 1.33.3 (2024-02-28)
4
19
 
5
20
  * Preserve current parser
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.33.3)
4
+ synvert-core (1.35.0)
5
5
  activesupport (< 7.0.0)
6
- node_mutation (>= 1.23.2)
6
+ node_mutation (>= 1.24.1)
7
7
  node_query (>= 1.15.1)
8
8
  parallel
9
9
  parser
@@ -43,17 +43,17 @@ GEM
43
43
  guard (~> 2.1)
44
44
  guard-compat (~> 1.1)
45
45
  rspec (>= 2.99.0, < 4.0)
46
- i18n (1.14.1)
46
+ i18n (1.14.4)
47
47
  concurrent-ruby (~> 1.0)
48
48
  listen (3.7.1)
49
49
  rb-fsevent (~> 0.10, >= 0.10.3)
50
50
  rb-inotify (~> 0.9, >= 0.9.10)
51
51
  lumberjack (1.2.8)
52
52
  method_source (1.0.0)
53
- minitest (5.22.2)
53
+ minitest (5.22.3)
54
54
  nenv (0.3.0)
55
- node_mutation (1.23.3)
56
- node_query (1.15.1)
55
+ node_mutation (1.24.3)
56
+ node_query (1.15.2)
57
57
  notiffany (0.1.3)
58
58
  nenv (~> 0.1)
59
59
  shellany (~> 0.0)
@@ -61,11 +61,11 @@ GEM
61
61
  parser (3.3.0.5)
62
62
  ast (~> 2.4.1)
63
63
  racc
64
- parser_node_ext (1.2.2)
64
+ parser_node_ext (1.3.0)
65
65
  parser
66
66
  prettier_print (1.2.1)
67
- prism (0.24.0)
68
- prism_ext (0.2.3)
67
+ prism (0.25.0)
68
+ prism_ext (0.3.0)
69
69
  prism
70
70
  pry (0.14.1)
71
71
  coderay (~> 1.1)
@@ -91,7 +91,7 @@ GEM
91
91
  shellany (0.0.1)
92
92
  syntax_tree (6.2.0)
93
93
  prettier_print (>= 1.2.0)
94
- syntax_tree_ext (0.7.2)
94
+ syntax_tree_ext (0.8.1)
95
95
  syntax_tree
96
96
  thor (1.2.1)
97
97
  tzinfo (2.0.6)
data/README.md CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  <img src="https://synvert.net/img/logo_96.png" alt="logo" width="32" height="32" />
4
4
 
5
- [![AwesomeCode Status for xinminlabs/synvert-core-ruby](https://awesomecode.io/projects/033f7f02-7b22-41c3-a902-fca37f1ec72a/status)](https://awesomecode.io/repos/xinminlabs/synvert-core-ruby)
6
- ![Main workflow](https://github.com/xinminlabs/synvert-core-ruby/actions/workflows/main.yml/badge.svg)
5
+ [![AwesomeCode Status for synvert-hq/synvert-core-ruby](https://awesomecode.io/projects/033f7f02-7b22-41c3-a902-fca37f1ec72a/status)](https://awesomecode.io/repos/synvert-hq/synvert-core-ruby)
6
+ [![Build Status](https://github.com/synvert-hq/synvert-core-ruby/actions/workflows/main.yml/badge.svg)](https://github.com/synvert-hq/synvert-core-ruby/actions/workflows/main.yml)
7
+ [![Gem Version](https://img.shields.io/gem/v/synvert-core.svg)](https://rubygems.org/gems/synvert-core)
7
8
 
8
9
  Synvert core provides a set of DSLs to rewrite ruby code. e.g.
9
10
 
@@ -53,64 +54,66 @@ Synvert::Rewriter.new 'factory_bot', 'convert_factory_girl_to_factory_bot' do
53
54
  end
54
55
  ```
55
56
 
56
- Want to see more examples, check out [synvert-snippets-ruby](https://github.com/xinminlabs/synvert-snippets-ruby).
57
+ Want to see more examples, check out [synvert-snippets-ruby](https://github.com/synvert-hq/synvert-snippets-ruby).
57
58
 
58
- Want to use the CLI, check out [synvert-ruby](https://github.com/xinminlabs/synvert-ruby).
59
+ Want to use the CLI, check out [synvert-ruby](https://github.com/synvert-hq/synvert-ruby).
59
60
 
60
61
  DSLs are as follows
61
62
 
62
- * [configure](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#configure-instance_method) - configure the rewriter
63
- * [description](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#description-instance_method) - describe what the rewriter does
64
- * [if_ruby](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#if_ruby-instance_method) - check if ruby version is greater than or equal to the specified ruby version
65
- * [if_gem](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#if_gem-instance_method) - compare version of specified gem
66
- * [within_files](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#within_files-instance_method) - find specified files
67
- * [within_file](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#within_file-instance_method) - alias to within_files
68
- * [add_file](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#add_file-instance_method) - add a new file
69
- * [remove_file](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#remove_file-instance_method) - remove a file
70
- * [helper_method](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#helper_method-instance_method) - define a helper method
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
63
+ * [configure](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#configure-instance_method) - configure the rewriter
64
+ * [description](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#description-instance_method) - describe what the rewriter does
65
+ * [if_ruby](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#if_ruby-instance_method) - check if ruby version is greater than or equal to the specified ruby version
66
+ * [if_gem](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#if_gem-instance_method) - compare version of specified gem
67
+ * [within_files](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#within_files-instance_method) - find specified files
68
+ * [within_file](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#within_file-instance_method) - alias to within_files
69
+ * [add_file](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#add_file-instance_method) - add a new file
70
+ * [remove_file](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#remove_file-instance_method) - remove a file
71
+ * [helper_method](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#helper_method-instance_method) - define a helper method
72
+ * [add_snippet](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#add_snippet-instance_method) - call another rewriter
73
+ * [call_helper](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#call_helper-instance_method) - call a shared rewriter
74
+ * [with_configurations](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#with_configurations-instance_method) - execute a block of code with temporary configurations
75
+ * [save_data](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#save_data-instance_method) - save data
76
+ * [load_data](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#load_data-instance_method) - load data
73
77
 
74
78
  Scopes:
75
79
 
76
- * [within_node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#within_node-instance_method) - recursively find matching ast nodes
77
- * [with_node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#with_node-instance_method) - alias to within_node
78
- * [find_node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#find_node-instance_method) - alias to within_node
79
- * [goto_node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#goto_node-instance_method) - go to a child node
80
+ * [within_node](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#within_node-instance_method) - recursively find matching ast nodes
81
+ * [with_node](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#with_node-instance_method) - alias to within_node
82
+ * [find_node](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#find_node-instance_method) - alias to within_node
83
+ * [goto_node](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#goto_node-instance_method) - go to a child node
80
84
 
81
85
  Conditions:
82
86
 
83
- * [if_exist_node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#if_exist_node-instance_method) - check if matching node exist in the child nodes
84
- * [unless_exist_node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#unless_exist_node-instance_method) - check if matching node doesn't exist in the child nodes
85
- * [if_only_exist_node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#if_only_exist_node-instance_method) - check if current node has only one child node and the child node matches
87
+ * [if_exist_node](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#if_exist_node-instance_method) - check if matching node exist in the child nodes
88
+ * [unless_exist_node](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#unless_exist_node-instance_method) - check if matching node doesn't exist in the child nodes
86
89
 
87
90
  Actions:
88
91
 
89
- * [append](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#append-instance_method) - append the code to the bottom of current node body
90
- * [prepend](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#prepend-instance_method) - prepend the code to the bottom of current node body
91
- * [indent](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#indent-instance_method) - indent code
92
- * [insert](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert-instance_method) - insert code
93
- * [insert_after](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert_after-instance_method) - insert the code next to the current node
94
- * [insert_before](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert_before-instance_method) - insert the code previous to the current node
95
- * [replace](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#replace-instance_method) - replace the code of specified child nodes
96
- * [delete](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#delete-instance_method) - delete the code in specified child nodes
97
- * [remove](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#remove-instance_method) - remove the whole code of current node
98
- * [wrap](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#wrap-instance_method) - wrap the current node with prefix and suffix code
99
- * [replace_with](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#replace_with-instance_method) - replace the whole code of current node
100
- * [warn](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#warn-instance_method) - warn message
101
- * [replace_erb_stmt_with_expr](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#replace_erb_stmt_with_expr-instance_method) - replace erb stmt code to expr code
102
- * [noop](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#noop-instance_method) - no operation
103
- * [group](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#group-instance_method) - group actions
104
- * [add_action](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#add_action-instance_method) - add custom action
92
+ * [append](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#append-instance_method) - append the code to the bottom of current node body
93
+ * [prepend](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#prepend-instance_method) - prepend the code to the bottom of current node body
94
+ * [indent](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#indent-instance_method) - indent code
95
+ * [insert](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert-instance_method) - insert code
96
+ * [insert_after](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert_after-instance_method) - insert the code next to the current node
97
+ * [insert_before](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert_before-instance_method) - insert the code previous to the current node
98
+ * [replace](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#replace-instance_method) - replace the code of specified child nodes
99
+ * [delete](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#delete-instance_method) - delete the code in specified child nodes
100
+ * [remove](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#remove-instance_method) - remove the whole code of current node
101
+ * [wrap](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#wrap-instance_method) - wrap the current node with prefix and suffix code
102
+ * [replace_with](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#replace_with-instance_method) - replace the whole code of current node
103
+ * [warn](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#warn-instance_method) - warn message
104
+ * [replace_erb_stmt_with_expr](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#replace_erb_stmt_with_expr-instance_method) - replace erb stmt code to expr code
105
+ * [noop](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#noop-instance_method) - no operation
106
+ * [group](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#group-instance_method) - group actions
107
+ * [add_action](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#add_action-instance_method) - add custom action
105
108
 
106
109
  Others:
107
110
 
108
- * [wrap_with_quotes](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#wrap_with_quotes-instance_method) - wrap string code with single or doulbe quotes
109
- * [add_leading_spaces](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#add_leading_spaces-instance_method) - add leading spaces before the string code
111
+ * [wrap_with_quotes](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#wrap_with_quotes-instance_method) - wrap string code with single or doulbe quotes
112
+ * [add_leading_spaces](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#add_leading_spaces-instance_method) - add leading spaces before the string code
110
113
 
111
114
 
112
115
  Attributes:
113
116
 
114
- * [file_path](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#file_path-instance_method) - current file path
115
- * [node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#node-instance_method) - current ast node
116
- * [mutation_adapter](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#mutation_adapter-instance_method) - [mutation adapter](https://xinminlabs.github.io/node-mutation-ruby/NodeMutation/Adapter.html) to get some helper methods
117
+ * [file_path](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#file_path-instance_method) - current file path
118
+ * [node](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#node-instance_method) - current ast node
119
+ * [mutation_adapter](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#mutation_adapter-instance_method) - [mutation adapter](https://synvert-hq.github.io/node-mutation-ruby/NodeMutation/Adapter.html) to get some helper methods
@@ -100,6 +100,32 @@ module Synvert::Core
100
100
  def test_result
101
101
  @test_result || 'actions'
102
102
  end
103
+
104
+ # Temporarily sets the specified configurations, executes the given block, and then restores the original configurations.
105
+ #
106
+ # @param configurations [Hash] The configurations to be set temporarily.
107
+ # @yield The block of code to be executed.
108
+ #
109
+ # @example
110
+ # with_temporary_configurations({ number_of_workers: 1 }) do
111
+ # # Code to be executed with temporary configurations
112
+ # end
113
+ def with_temporary_configurations(configurations, &block)
114
+ old_instance_variables = instance_variables.reduce({}) do |hash, var|
115
+ hash[var] = instance_variable_get(var)
116
+ hash
117
+ end
118
+
119
+ configurations.each do |variable, value|
120
+ instance_variable_set("@#{variable}", value)
121
+ end
122
+
123
+ block.call
124
+
125
+ old_instance_variables.each do |var, value|
126
+ instance_variable_set(var, value)
127
+ end
128
+ end
103
129
  end
104
130
  end
105
131
  end
@@ -48,8 +48,8 @@ module Synvert::Core
48
48
 
49
49
  absolute_file_path = File.join(Configuration.root_path, @file_path)
50
50
  # It keeps running until no conflict,
51
- # it will try 10 times at maximum.
52
- 10.times do
51
+ # it will try 5 times at maximum.
52
+ 5.times do
53
53
  source = read_source(absolute_file_path)
54
54
  encoded_source = Engine.encode(File.extname(file_path), source)
55
55
  @current_mutation = NodeMutation.new(source, adapter: @rewriter.parser)
@@ -216,21 +216,6 @@ module Synvert::Core
216
216
  Rewriter::UnlessExistCondition.new(self, nql_or_rules, &block).process
217
217
  end
218
218
 
219
- # It creates a {Synvert::Core::Rewriter::IfOnlyExistCondition} to check
220
- # if current node has only one child node and the child node matches,
221
- # if so, then continue operating on each matching ast node.
222
- # @example
223
- # # it { should matcher }
224
- # with_node type: 'block', caller: { message: 'it' } do
225
- # if_only_exist_node type: 'send', receiver: nil, message: 'should' do
226
- # end
227
- # end
228
- # @param nql_or_rules [String|Hash] nql or rules to check mathing ast nodes.
229
- # @param block [Block] block code to continue operating on the matching nodes.
230
- def if_only_exist_node(nql_or_rules, &block)
231
- Rewriter::IfOnlyExistCondition.new(self, nql_or_rules, &block).process
232
- end
233
-
234
219
  # It appends the code to the bottom of current node body.
235
220
  # @example
236
221
  # # def teardown
@@ -24,7 +24,6 @@ module Synvert::Core
24
24
  autoload :Condition, 'synvert/core/rewriter/condition'
25
25
  autoload :IfExistCondition, 'synvert/core/rewriter/condition/if_exist_condition'
26
26
  autoload :UnlessExistCondition, 'synvert/core/rewriter/condition/unless_exist_condition'
27
- autoload :IfOnlyExistCondition, 'synvert/core/rewriter/condition/if_only_exist_condition'
28
27
 
29
28
  autoload :Helper, 'synvert/core/rewriter/helper'
30
29
 
@@ -346,8 +345,8 @@ module Synvert::Core
346
345
  # add_snippet 'minitest/assert_kind_of'
347
346
  # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_match.rb'
348
347
  # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_nil.rb'
349
- # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_silent.rb'
350
- # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_truthy.rb'
348
+ # add_snippet 'https://github.com/synvert-hq/synvert-snippets-ruby/blob/main/lib/minitest/assert_silent.rb'
349
+ # add_snippet 'https://github.com/synvert-hq/synvert-snippets-ruby/blob/main/lib/minitest/assert_truthy.rb'
351
350
  # end
352
351
  # @param group [String] group of another rewriter, if there's no name parameter, the group can be http url, file path or snippet name.
353
352
  # @param name [String] name of another rewriter.
@@ -379,7 +378,7 @@ module Synvert::Core
379
378
  # Synvert::Rewriter.new 'rails', 'upgrade_6_0_to_6_1' do
380
379
  # call_helper 'rails/set_load_defaults', rails_version: '6.1'
381
380
  # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/rails/set_load_defaults.rb', rails_version: '6.1'
382
- # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/rails/set_load_defaults.rb', rails_version: '6.1'
381
+ # add_snippet 'https://github.com/synvert-hq/synvert-snippets-ruby/blob/main/lib/rails/set_load_defaults.rb', rails_version: '6.1'
383
382
  # end
384
383
  # @param name [String] name of helper.
385
384
  # @param options [Hash] options to pass to helper.
@@ -409,6 +408,30 @@ module Synvert::Core
409
408
  @helpers << { name: name, block: block }
410
409
  end
411
410
 
411
+ # Executes a block of code with temporary configurations.
412
+ #
413
+ # @param configurations [Hash] The temporary configurations to apply.
414
+ # @yield The block of code to execute.
415
+ def with_configurations(configurations, &block)
416
+ Configuration.with_temporary_configurations(configurations, &block)
417
+ end
418
+
419
+ # Saves data with a given key and value.
420
+ #
421
+ # @param key [Symbol] the key to identify the data
422
+ # @param value [Object] the value to be saved
423
+ def save_data(key, value)
424
+ Synvert::Core.instance_variable_set("@#{key}", value)
425
+ end
426
+
427
+ # Loads data with the given key.
428
+ #
429
+ # @param key [Symbol] the key of the data to be loaded
430
+ # @return [Object] the data loaded
431
+ def load_data(key)
432
+ Synvert::Core.instance_variable_get("@#{key}")
433
+ end
434
+
412
435
  private
413
436
 
414
437
  # Handle one file.
@@ -80,7 +80,7 @@ module Synvert::Core
80
80
  end
81
81
 
82
82
  def remote_snippet_url(snippet_name)
83
- "https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/#{snippet_name}.rb"
83
+ "https://github.com/synvert-hq/synvert-snippets-ruby/blob/main/lib/#{snippet_name}.rb"
84
84
  end
85
85
 
86
86
  def format_url(url)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.33.3'
5
+ VERSION = '1.35.0'
6
6
  end
7
7
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ RSpec.describe Configuration do
5
+ after do
6
+ Configuration.number_of_workers = nil
7
+ Configuration.strict = nil
8
+ end
9
+
10
+ describe '.with_temporary_configurations' do
11
+ it 'temporarily sets instance variables and restores them after block execution' do
12
+ Configuration.number_of_workers = 4
13
+ Configuration.strict = true
14
+
15
+ Configuration.with_temporary_configurations(number_of_workers: 1, strict: false) do
16
+ expect(Configuration.number_of_workers).to eq(1)
17
+ expect(Configuration.strict).to eq(false)
18
+ end
19
+
20
+ expect(Configuration.number_of_workers).to eq(4)
21
+ expect(Configuration.strict).to eq(true)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -91,16 +91,6 @@ module Synvert::Core
91
91
  instance.unless_exist_node(type: 'send', message: 'create', &block)
92
92
  end
93
93
 
94
- it 'parses if_only_exist_node' do
95
- condition = double
96
- block = proc {}
97
- expect(Rewriter::IfOnlyExistCondition).to receive(:new)
98
- .with(instance, { type: 'send', message: 'create' }, &block)
99
- .and_return(condition)
100
- expect(condition).to receive(:process)
101
- instance.if_only_exist_node(type: 'send', message: 'create', &block)
102
- end
103
-
104
94
  it 'parses append' do
105
95
  instance.instance_variable_set(:@current_mutation, double)
106
96
  instance.current_node = double
@@ -395,6 +395,18 @@ module Synvert::Core
395
395
  expect(instance.dynamic_helper('arg1', 'arg2')).to eq 'dynamic result'
396
396
  end
397
397
 
398
+ it 'parses save_data and load_data' do
399
+ data = nil
400
+ rewriter =
401
+ Rewriter.new 'group', 'name' do
402
+ hash = { foo: 'bar' }
403
+ save_data :object, hash
404
+ data = load_data :object
405
+ end
406
+ rewriter.process
407
+ expect(data).to eq({ foo: 'bar' })
408
+ end
409
+
398
410
  describe 'class methods' do
399
411
  before :each do
400
412
  Rewriter.clear
@@ -45,7 +45,7 @@ module Synvert::Core
45
45
  expect(File).to receive(:exist?).with("group/name").and_return(false)
46
46
  expect(described_class).to receive(:default_snippets_home).and_return('/home/richard/.synvert-ruby')
47
47
  expect(File).to receive(:exist?).with("/home/richard/.synvert-ruby/lib/group/name.rb").and_return(false)
48
- expect(described_class).to receive(:remote_snippet_exists?).with(URI.parse("https://raw.githubusercontent.com/xinminlabs/synvert-snippets-ruby/main/lib/group/name.rb")).and_return(true)
48
+ expect(described_class).to receive(:remote_snippet_exists?).with(URI.parse("https://raw.githubusercontent.com/synvert-hq/synvert-snippets-ruby/main/lib/group/name.rb")).and_return(true)
49
49
  expect_any_instance_of(URI::HTTP).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'name' do\nend"))
50
50
  rewriter = described_class.eval_snippet('group/name')
51
51
  expect(rewriter.group).to eq 'group'
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ["flyerhzm@gmail.com"]
12
12
  spec.summary = 'convert ruby code to better syntax.'
13
13
  spec.description = 'convert ruby code to better syntax automatically.'
14
- spec.homepage = "https://github.com/xinminlabs/synvert-core-ruby"
14
+ spec.homepage = "https://github.com/synvert-hq/synvert-core-ruby"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0")
@@ -21,7 +21,7 @@ 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.15.1"
24
- spec.add_runtime_dependency "node_mutation", ">= 1.23.2"
24
+ spec.add_runtime_dependency "node_mutation", ">= 1.24.1"
25
25
  spec.add_runtime_dependency "parser"
26
26
  spec.add_runtime_dependency "parser_node_ext", ">= 1.2.2"
27
27
  spec.add_runtime_dependency "syntax_tree"
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.33.3
4
+ version: 1.35.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: 2024-02-28 00:00:00.000000000 Z
11
+ date: 2024-04-07 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.23.2
47
+ version: 1.24.1
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.23.2
54
+ version: 1.24.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: parser
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -181,7 +181,6 @@ files:
181
181
  - lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
182
182
  - lib/synvert/core/rewriter/condition.rb
183
183
  - lib/synvert/core/rewriter/condition/if_exist_condition.rb
184
- - lib/synvert/core/rewriter/condition/if_only_exist_condition.rb
185
184
  - lib/synvert/core/rewriter/condition/unless_exist_condition.rb
186
185
  - lib/synvert/core/rewriter/gem_spec.rb
187
186
  - lib/synvert/core/rewriter/helper.rb
@@ -196,13 +195,13 @@ files:
196
195
  - lib/synvert/core/version.rb
197
196
  - spec/spec_helper.rb
198
197
  - spec/support/parser_helper.rb
198
+ - spec/synvert/core/configuration_spec.rb
199
199
  - spec/synvert/core/engine/erb_spec.rb
200
200
  - spec/synvert/core/engine/haml_spec.rb
201
201
  - spec/synvert/core/engine/slim_spec.rb
202
202
  - spec/synvert/core/helper_spec.rb
203
203
  - spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
204
204
  - spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
205
- - spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb
206
205
  - spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb
207
206
  - spec/synvert/core/rewriter/condition_spec.rb
208
207
  - spec/synvert/core/rewriter/gem_spec_spec.rb
@@ -216,7 +215,7 @@ files:
216
215
  - spec/synvert/core/rewriter_spec.rb
217
216
  - spec/synvert/core/utils_spec.rb
218
217
  - synvert-core-ruby.gemspec
219
- homepage: https://github.com/xinminlabs/synvert-core-ruby
218
+ homepage: https://github.com/synvert-hq/synvert-core-ruby
220
219
  licenses:
221
220
  - MIT
222
221
  metadata: {}
@@ -242,13 +241,13 @@ summary: convert ruby code to better syntax.
242
241
  test_files:
243
242
  - spec/spec_helper.rb
244
243
  - spec/support/parser_helper.rb
244
+ - spec/synvert/core/configuration_spec.rb
245
245
  - spec/synvert/core/engine/erb_spec.rb
246
246
  - spec/synvert/core/engine/haml_spec.rb
247
247
  - spec/synvert/core/engine/slim_spec.rb
248
248
  - spec/synvert/core/helper_spec.rb
249
249
  - spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
250
250
  - spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
251
- - spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb
252
251
  - spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb
253
252
  - spec/synvert/core/rewriter/condition_spec.rb
254
253
  - spec/synvert/core/rewriter/gem_spec_spec.rb
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # IfOnlyExistCondition checks if node has only one child node and the child node matches.
5
- class Rewriter::IfOnlyExistCondition < Rewriter::Condition
6
- private
7
-
8
- # check if only have one child node and the child node matches.
9
- #
10
- # @return [Boolean]
11
- def match?
12
- target_node.body.size == 1 && @node_query.match_node?(target_node.body.first)
13
- end
14
- end
15
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- module Synvert::Core
6
- describe Rewriter::IfOnlyExistCondition do
7
- let(:source) { <<~EOS }
8
- RSpec.configure do |config|
9
- config.include EmailSpec::Helpers
10
- config.include EmailSpec::Methods
11
- end
12
- EOS
13
- let(:node) { Parser::CurrentRuby.parse(source) }
14
- let(:instance) { double(current_node: node, parser: :parser) }
15
-
16
- describe '#process' do
17
- it 'gets matching nodes' do
18
- source = ' RSpec.configure do |config| config.include EmailSpec::Helpers end '
19
- node = Parser::CurrentRuby.parse(source)
20
- instance = double(current_node: node, parser: :parser)
21
- run = false
22
- condition =
23
- Rewriter::IfOnlyExistCondition.new instance,
24
- type: 'send',
25
- message: 'include',
26
- arguments: ['EmailSpec::Helpers'] do
27
- run = true
28
- end
29
- condition.process
30
- expect(run).to be_truthy
31
- end
32
-
33
- it 'not call block if does not match' do
34
- run = false
35
- condition =
36
- Rewriter::IfOnlyExistCondition.new instance,
37
- type: 'send',
38
- message: 'include',
39
- arguments: ['EmailSpec::Helpers'] do
40
- run = true
41
- end
42
- condition.process
43
- expect(run).to be_falsey
44
- end
45
- end
46
- end
47
- end