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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +23 -3
- data/lib/node_mutation/action/append_action.rb +1 -1
- data/lib/node_mutation/action/prepend_action.rb +1 -1
- data/lib/node_mutation/action.rb +0 -2
- data/lib/node_mutation/version.rb +1 -1
- data/lib/node_mutation.rb +32 -19
- data/sig/node_mutation.rbs +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36b238099da8793f4d2e9df6c5e679ba4d4f7da2254c020606d73dfd93f3baf8
|
4
|
+
data.tar.gz: b44fbf6192513de334c03580a04b8fe30602697a2d8f7fc39a96f5776c046739
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbfb8d677df7896754afddf23e3fa880f6d6f16d646c9a93881b4e216c1a2adf83c4ba45a96dd4827aeb9800ea375ba5dd992a815bf2794e1b5d11206b956415
|
7
|
+
data.tar.gz: 3a0c2217d7b51a90bd2818a036e07b76f2e2df82295f5bed462e075198855325285513e344d97cc70ac0179b1fa6a77ca13f25558f12f0db982903cc52a65318
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -63,14 +63,34 @@ result.conflicted
|
|
63
63
|
result.new_source
|
64
64
|
```
|
65
65
|
|
66
|
-
##
|
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
|
-
```
|
73
|
-
NodeMutation.configure(
|
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 +
|
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 +
|
23
|
+
' ' * (NodeMutation.adapter.get_start_loc(node).column + NodeMutation.tab_width)
|
24
24
|
end
|
25
25
|
end
|
data/lib/node_mutation/action.rb
CHANGED
data/lib/node_mutation.rb
CHANGED
@@ -25,29 +25,42 @@ class NodeMutation
|
|
25
25
|
|
26
26
|
attr_reader :actions
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
36
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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.
|
data/sig/node_mutation.rbs
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2023-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubis
|