synvert-core 0.63.1 → 0.64.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -1
  3. data/README.md +73 -33
  4. data/lib/synvert/core/configuration.rb +12 -0
  5. data/lib/synvert/core/exceptions.rb +0 -4
  6. data/lib/synvert/core/node_ext.rb +188 -87
  7. data/lib/synvert/core/rewriter/action/append_action.rb +4 -3
  8. data/lib/synvert/core/rewriter/action/delete_action.rb +13 -6
  9. data/lib/synvert/core/rewriter/action/insert_action.rb +16 -7
  10. data/lib/synvert/core/rewriter/action/insert_after_action.rb +3 -2
  11. data/lib/synvert/core/rewriter/action/prepend_action.rb +3 -2
  12. data/lib/synvert/core/rewriter/action/remove_action.rb +16 -10
  13. data/lib/synvert/core/rewriter/action/replace_action.rb +13 -5
  14. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +18 -11
  15. data/lib/synvert/core/rewriter/action/replace_with_action.rb +6 -5
  16. data/lib/synvert/core/rewriter/action/wrap_action.rb +13 -5
  17. data/lib/synvert/core/rewriter/action.rb +20 -9
  18. data/lib/synvert/core/rewriter/any_value.rb +1 -0
  19. data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +4 -0
  20. data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +4 -0
  21. data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +4 -0
  22. data/lib/synvert/core/rewriter/condition.rb +11 -3
  23. data/lib/synvert/core/rewriter/gem_spec.rb +6 -3
  24. data/lib/synvert/core/rewriter/helper.rb +2 -2
  25. data/lib/synvert/core/rewriter/instance.rb +195 -94
  26. data/lib/synvert/core/rewriter/ruby_version.rb +4 -4
  27. data/lib/synvert/core/rewriter/scope/goto_scope.rb +5 -6
  28. data/lib/synvert/core/rewriter/scope/within_scope.rb +9 -4
  29. data/lib/synvert/core/rewriter/scope.rb +8 -0
  30. data/lib/synvert/core/rewriter/warning.rb +1 -1
  31. data/lib/synvert/core/rewriter.rb +90 -43
  32. data/lib/synvert/core/version.rb +1 -1
  33. data/lib/synvert/core.rb +0 -1
  34. data/spec/synvert/core/node_ext_spec.rb +0 -7
  35. data/spec/synvert/core/rewriter/action_spec.rb +0 -4
  36. data/spec/synvert/core/rewriter/gem_spec_spec.rb +1 -1
  37. data/spec/synvert/core/rewriter/instance_spec.rb +7 -17
  38. data/synvert-core-ruby.gemspec +2 -1
  39. metadata +20 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5739f84132a6ece2551028571b7e6cb458812524f51ea11de6175eb621592d21
4
- data.tar.gz: a55b00a9431cacbe740405294b351cd8c5a1face1f2c20ba3a61897316dc7bd4
3
+ metadata.gz: ea27da2bceb8ec0f2dd837e5aa80135860a2539c7e49170b2bd46af227d1043e
4
+ data.tar.gz: a901be4a294000eecfbbbf407416015cd9b38499ebc24c744147aebdeba5388f
5
5
  SHA512:
6
- metadata.gz: ba153f740ea4d281dde99359dcf864beb49bdac33db3833878eebb9ba020966bb8957426936e65f7fc00ce84b76f7bb79fe2c01a5984ea155c5a4f6845c6e5b7
7
- data.tar.gz: 6ff6c82355e35e6f86a42f01bcbd57b4e25d6c0ccef0079b89d4980bca26a983bf187526f9f5af150ee7a970d906b3cb22dd69b1af9f84263d3d8231d4673df7
6
+ metadata.gz: b9d0451d59718fe04e23d48a90e7e705fb455592d2636055064d9c5d7f6a1db20980ff1a38b147d77b16fdc8b059b95a8a95848c183a4115ddb1730541bf0757
7
+ data.tar.gz: 63246572cc0d9c3d44d7bb12f6d716e11b15fd1978376588e235a4c2f9b5e7849962ed4a81e38bd1a9f74c2cb877325148c486c9f3045ebf8c0aa80459d9b6aa
data/CHANGELOG.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.63.1 (2022-02-26)
3
+ ## 0.64.0 (2022-04-02)
4
4
 
5
5
  * Read absolute path of Gemfile.lock
6
+ * Remove unused `Node#to_s`
7
+ * Yardoc comments
8
+ * Drop `within_direct_node(rules)`, use `within_node(rules, { direct: true })` instead
6
9
 
7
10
  ## 0.63.0 (2022-02-26)
8
11
 
data/README.md CHANGED
@@ -5,36 +5,76 @@
5
5
  ![Main workflow](https://github.com/xinminlabs/synvert-core-ruby/actions/workflows/main.yml/badge.svg)
6
6
  [![Gem Version](https://badge.fury.io/rb/synvert-core.png)](http://badge.fury.io/rb/synvert-core)
7
7
 
8
- synvert-core-ruby provides a dsl to convert ruby source code.
9
-
10
- ## Installation
11
-
12
- Add this line to your application's Gemfile:
13
-
14
- gem 'synvert-core'
15
-
16
- And then execute:
17
-
18
- $ bundle
19
-
20
- Or install it yourself as:
21
-
22
- $ gem install synvert-core
23
-
24
-
25
- ## Documentation
26
-
27
- [Website][1]
28
-
29
- [RDoc][2]
30
-
31
- ## Contributing
32
-
33
- 1. Fork it ( https://github.com/[my-github-username]/synvert-core-ruby/fork )
34
- 2. Create your feature branch (`git checkout -b my-new-feature`)
35
- 3. Commit your changes (`git commit -am 'Add some feature'`)
36
- 4. Push to the branch (`git push origin my-new-feature`)
37
- 5. Create a new Pull Request
38
-
39
- [1]: https://synvert.xinminlabs.com
40
- [2]: https://rubydoc.info/github/xinminlabs/synvert-core-ruby/master/frames
8
+ Synvert core provides a set of DSLs to rewrite ruby code. e.g.
9
+
10
+ ```ruby
11
+ Synvert::Rewriter.new 'ruby', 'map_and_flatten_to_flat_map' do
12
+ description <<~EOS
13
+ It converts `map` and `flatten` to `flat_map`
14
+
15
+ ```ruby
16
+ enum.map do
17
+ # do something
18
+ end.flatten
19
+ ```
20
+
21
+ =>
22
+
23
+ ```ruby
24
+ enum.flat_map do
25
+ # do something
26
+ end
27
+ ```
28
+ EOS
29
+
30
+ within_files Synvert::ALL_RUBY_FILES do
31
+ with_node type: 'send', receiver: { type: 'block', caller: { type: 'send', message: 'map' } }, message: 'flatten', arguments: { size: 0 } do
32
+ delete :message, :dot
33
+ replace 'receiver.caller.message', with: 'flat_map'
34
+ end
35
+ end
36
+ end
37
+ ```
38
+
39
+ Want to see more examples, check out [synvert-snippets-ruby](https://github.com/xinminlabs/synvert-snippets-ruby).
40
+
41
+ Want to use the CLI, check out [synvert-ruby](https://github.com/xinminlabs/synvert-ruby).
42
+
43
+ DSLs are as follows
44
+
45
+ * [description](./Synvert/Core/Rewriter.html#description-instance_method) - set description of the rewriter
46
+ * [if_ruby](./Synvert/Core/Rewriter.html#if_ruby-instance_method) - check if ruby version is greater than or equal to the specified ruby version
47
+ * [if_gem](./Synvert/Core/Rewriter.html#if_gem-instance_method) - compare version of specified gem
48
+ * [within_files](./Synvert/Core/Rewriter.html#within_files-instance_method) - find specified files
49
+ * [within_file](./Synvert/Core/Rewriter.html#within_file-instance_method) - alias to within_files
50
+ * [add_file](./Synvert/Core/Rewriter.html#add_file-instance_method) - add a new file
51
+ * [remove_file](./Synvert/Core/Rewriter.html#remove_file-instance_method) - remove a file
52
+ * [helper_method](./Synvert/Core/Rewriter.html#helper_method-instance_method) - define a helper method
53
+ * [add_snippet](./Synvert/Core/Rewriter.html#add_snippet-instance_method) - call another rewriter
54
+ * [todo](./Synvert/Core/Rewriter.html#todo-instance_method) - set todo
55
+ * [redo_until_no_change](./Synvert/Core/Rewriter.html#redo_until_no_change-instance_method) - run the snippet until no change
56
+
57
+ Scopes:
58
+
59
+ * [within_node](./Synvert/Core/Rewriter/Instance.html#within_node-instance_method) - recursively find matching ast nodes
60
+ * [with_node](./Synvert/Core/Rewriter/Instance.html#with_node-instance_method) - alias to within_node
61
+ * [goto_node](./Synvert/Core/Rewriter/Instance.html#goto_node-instance_method) - go to a child node
62
+
63
+ Conditions:
64
+
65
+ * [if_exist_node](./Synvert/Core/Rewriter/Instance.html#if_exist_node-instance_method) - check if matching node exist in the child nodes
66
+ * [unless_exist_node](./Synvert/Core/Rewriter/Instance.html#unless_exist_node-instance_method) - check if matching node doesn't exist in the child nodes
67
+ * [if_only_exist_node](./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 rules
68
+
69
+ Actions:
70
+
71
+ * [append](./Synvert/Core/Rewriter/Instance.html#append-instance_method) - append the code to the bottom of current node body
72
+ * [prepend](./Synvert/Core/Rewriter/Instance.html#prepend-instance_method) - prepend the code to the bottom of current node body
73
+ * [insert](./Synvert/Core/Rewriter/Instance.html#insert-instance_method) - insert code
74
+ * [insert_after](./Synvert/Core/Rewriter/Instance.html#insert_after-instance_method) - insert the code next to the current node
75
+ * [replace](./Synvert/Core/Rewriter/Instance.html#replace-instance_method) - replace the code of specified child nodes
76
+ * [delete](./Synvert/Core/Rewriter/Instance.html#delete-instance_method) - delete the code specified child nodes
77
+ * [wrap](./Synvert/Core/Rewriter/Instance.html#wrap-instance_method) - wrap the current node with code
78
+ * [replace_with](./Synvert/Core/Rewriter/Instance.html#replace_with-instance_method) - replace the whole code of current node
79
+ * [warn](./Synvert/Core/Rewriter/Instance.html#warn-instance_method) - warn message
80
+ * [replace_erb_stmt_with_expr](./Synvert/Core/Rewriter/Instance.html#replace_erb_stmt_with_expr-instance_method) - replace erb stmt code to expr code
@@ -4,16 +4,28 @@ module Synvert::Core
4
4
  # Synvert global configuration.
5
5
  class Configuration
6
6
  class << self
7
+ # @!attribute [w] path
8
+ # @!attribute [w] skip_files
9
+ # @!attribute [w] show_run_process
7
10
  attr_writer :path, :skip_files, :show_run_process
8
11
 
12
+ # Get the path.
13
+ #
14
+ # @return [String] default is '.'
9
15
  def path
10
16
  @path || '.'
11
17
  end
12
18
 
19
+ # Get a list of skip files.
20
+ #
21
+ # @return [Array<String>] default is [].
13
22
  def skip_files
14
23
  @skip_files || []
15
24
  end
16
25
 
26
+ # Check if show run process.
27
+ #
28
+ # @return [Boolean] default is false
17
29
  def show_run_process
18
30
  @show_run_process || false
19
31
  end
@@ -5,10 +5,6 @@ module Synvert::Core
5
5
  class RewriterNotFound < RuntimeError
6
6
  end
7
7
 
8
- # Gemfile.lock not found exception.
9
- class GemfileLockNotFound < RuntimeError
10
- end
11
-
12
8
  # Method not supported exception.
13
9
  class MethodNotSupported < RuntimeError
14
10
  end