node_mutation 1.8.2 → 1.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -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 +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c83e3715de9de4bc244ddbcf4486f78c51bdff021735f291d797d7e2728d3369
|
4
|
+
data.tar.gz: 6dae070e5a2ad3977a53c08f312ee240d3a9164b353e257d61bdaff0c492c542
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b10359865b08bbb672dcdcb567f27a1de4018e12f61615cb16fb4d9406af37be3443ccafafcd2d9347a0fab48f739fffa70e170eff13ad443272542a2148cfc8
|
7
|
+
data.tar.gz: 605c42f06b1b2f5d54c8714394f3874446e88f27016bcf361d2a067f36dad285db9056984fc1216d70c02237eb3e052799e16e9c065cf4e3fda14823734f0e6d
|
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].to_i
|
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.1
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubis
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
requirements: []
|
85
|
-
rubygems_version: 3.4.
|
85
|
+
rubygems_version: 3.4.6
|
86
86
|
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: ast node mutation apis
|