node_mutation 1.8.2 → 1.9.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: be47289a83fa7295154056c8c6adc404f4f190d580a47dc78660dcfd082917d1
4
- data.tar.gz: 15f40478c899eb9b4e8fd809edc4ebec139f8d85a98bd7573103fee2b62ebde6
3
+ metadata.gz: 36b238099da8793f4d2e9df6c5e679ba4d4f7da2254c020606d73dfd93f3baf8
4
+ data.tar.gz: b44fbf6192513de334c03580a04b8fe30602697a2d8f7fc39a96f5776c046739
5
5
  SHA512:
6
- metadata.gz: 25e98cb3610598d9a30de2f880c071a04020c4507c8700826aa047f48179b340ad0e8e258f2301738717c5a7dd809ed9b78d75f0f1b3f9b3f243371c3d132fa5
7
- data.tar.gz: 0e615252351fa511f0e1be269c74fdd77bf1477ec97745bb5cfb747a9657356cdedde24357001fcfd4b879cb47b5daf603e7e3af0c82716156de1e531c68f2a3
6
+ metadata.gz: cbfb8d677df7896754afddf23e3fa880f6d6f16d646c9a93881b4e216c1a2adf83c4ba45a96dd4827aeb9800ea375ba5dd992a815bf2794e1b5d11206b956415
7
+ data.tar.gz: 3a0c2217d7b51a90bd2818a036e07b76f2e2df82295f5bed462e075198855325285513e344d97cc70ac0179b1fa6a77ca13f25558f12f0db982903cc52a65318
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.9.0 (2023-02-08)
4
+
5
+ * Configure `tab_width`
6
+ * Make use of `NodeMutation.tab_width`
7
+
3
8
  ## 1.8.2 (2023-01-17)
4
9
 
5
10
  * Drop `activesupport`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.8.2)
4
+ node_mutation (1.9.0)
5
5
  erubis
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -63,14 +63,34 @@ result.conflicted
63
63
  result.new_source
64
64
  ```
65
65
 
66
- ## Write Adapter
66
+ ## Configuration
67
+
68
+ ### adapter
67
69
 
68
70
  Different parsers, like parse and ripper, will generate different AST nodes, to make NodeMutation work for them all,
69
71
  we define an [Adapter](https://github.com/xinminlabs/node-mutation-ruby/blob/main/lib/node_mutation/adapter.rb) interface,
70
72
  if you implement the Adapter interface, you can set it as NodeMutation's adapter.
71
73
 
72
- ```typescript
73
- NodeMutation.configure({ adapter: ParserAdapter.new })
74
+ ```ruby
75
+ NodeMutation.configure(adapter: ParserAdapter.new) // default is ParserAdapter
76
+ ```
77
+
78
+ ### strategy
79
+
80
+ It provides 3 strategies to handle conflicts when processing actions:
81
+
82
+ 1. `Strategy.KEEP_RUNNING`: keep running and ignore the conflict action.
83
+ 2. `Strategy.THROW_ERROR`: throw error when conflict action is found.
84
+ 3. `Strategy.ALLOW_INSERT_AT_SAME_POSITION`: allow insert action at the same position.
85
+
86
+ ```ruby
87
+ NodeMutation.configure(strategy: Strategy.KEEP_RUNNING | Strategy.ALLOW_INSERT_AT_SAME_POSITION); // default is Strategy.THROW_ERROR
88
+ ```
89
+
90
+ ### tab_width
91
+
92
+ ```ruby
93
+ NodeMutation.configure(tab_width: 4); // default is 2
74
94
  ```
75
95
 
76
96
  ## Development
@@ -17,6 +17,6 @@ class NodeMutation::AppendAction < NodeMutation::Action
17
17
  # @param node [Parser::AST::Node]
18
18
  # @return [String] n times whitesphace
19
19
  def indent(node)
20
- ' ' * (NodeMutation.adapter.get_start_loc(node).column + DEFAULT_INDENT)
20
+ ' ' * (NodeMutation.adapter.get_start_loc(node).column + NodeMutation.tab_width)
21
21
  end
22
22
  end
@@ -20,6 +20,6 @@ class NodeMutation::PrependAction < NodeMutation::Action
20
20
  # @param node [Parser::AST::Node]
21
21
  # @return [String] n times whitesphace
22
22
  def indent(node)
23
- ' ' * (NodeMutation.adapter.get_start_loc(node).column + DEFAULT_INDENT)
23
+ ' ' * (NodeMutation.adapter.get_start_loc(node).column + NodeMutation.tab_width)
24
24
  end
25
25
  end
@@ -2,8 +2,6 @@
2
2
 
3
3
  # Action defines rewriter action, insert, replace or delete code.
4
4
  class NodeMutation::Action
5
- DEFAULT_INDENT = 2
6
-
7
5
  # @!attribute [r] start
8
6
  # @return [Integer] start position
9
7
  # @!attribute [r] end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.8.2"
4
+ VERSION = "1.9.0"
5
5
  end
data/lib/node_mutation.rb CHANGED
@@ -25,29 +25,42 @@ class NodeMutation
25
25
 
26
26
  attr_reader :actions
27
27
 
28
- # Configure NodeMutation
29
- # @param [Hash] options options to configure
30
- # @option options [NodeMutation::Adapter] :adapter the adpater
31
- def self.configure(options)
32
- if options[:adapter]
33
- @adapter = options[:adapter]
28
+ class <<self
29
+ # Configure NodeMutation
30
+ # @param [Hash] options options to configure
31
+ # @option options [NodeMutation::Adapter] :adapter the adpater
32
+ # @option options [NodeMutation::Strategy] :strategy the strategy
33
+ # @option options [Integer] :tab_width the tab width
34
+ def configure(options)
35
+ if options[:adapter]
36
+ @adapter = options[:adapter]
37
+ end
38
+ if options[:strategy]
39
+ @strategy = options[:strategy]
40
+ end
41
+ if options[:tab_width]
42
+ @tab_width = options[:tab_width]
43
+ end
34
44
  end
35
- if options[:strategy]
36
- @strategy = options[:strategy]
45
+
46
+ # Get the adapter
47
+ # @return [NodeMutation::Adapter] current adapter, by default is {NodeMutation::ParserAdapter}
48
+ def adapter
49
+ @adapter ||= ParserAdapter.new
37
50
  end
38
- end
39
51
 
40
- # Get the adapter
41
- # @return [NodeMutation::Adapter] current adapter, by default is {NodeMutation::ParserAdapter}
42
- def self.adapter
43
- @adapter ||= ParserAdapter.new
44
- end
52
+ # Get the strategy
53
+ # @return [Integer] current strategy, could be {NodeMutation::Strategy::KEEP_RUNNING} or {NodeMutation::Strategy::THROW_ERROR},
54
+ # by default is {NodeMutation::Strategy::KEEP_RUNNING}
55
+ def strategy
56
+ @strategy ||= Strategy::KEEP_RUNNING
57
+ end
45
58
 
46
- # Get the strategy
47
- # @return [Integer] current strategy, could be {NodeMutation::Strategy::KEEP_RUNNING} or {NodeMutation::Strategy::THROW_ERROR},
48
- # by default is {NodeMutation::Strategy::KEEP_RUNNING}
49
- def self.strategy
50
- @strategy ||= Strategy::KEEP_RUNNING
59
+ # Get tab width
60
+ # @return [Integer] tab width, by default is 2
61
+ def tab_width
62
+ @tab_width ||= 2
63
+ end
51
64
  end
52
65
 
53
66
  # Initialize a NodeMutation.
@@ -13,12 +13,14 @@ module NodeMutation[T]
13
13
 
14
14
  attr_reader actions: Array[NodeMutation::Action]
15
15
 
16
- def self.configure: (options: { adapter: NodeMutation::Adapter, strategy: Integer }) -> void
16
+ def self.configure: (options: { adapter: NodeMutation::Adapter, strategy: Integer, tab_width: Integer }) -> void
17
17
 
18
18
  def self.adapter: () -> NodeMutation::Adapter
19
19
 
20
20
  def self.strategy: () -> Integer
21
21
 
22
+ def self.tab_width: () -> Integer
23
+
22
24
  def initialize: (source: String) -> NodeMutation
23
25
 
24
26
  def append: (node: T, code: String) -> void
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: node_mutation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.2
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-17 00:00:00.000000000 Z
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubis