node_mutation 1.20.0 → 1.21.1
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 +9 -0
- data/Gemfile.lock +2 -2
- data/README.md +5 -0
- data/lib/node_mutation/action/{combined_action.rb → group_action.rb} +3 -5
- data/lib/node_mutation/action.rb +3 -1
- data/lib/node_mutation/helper.rb +1 -1
- data/lib/node_mutation/result.rb +1 -1
- data/lib/node_mutation/struct.rb +1 -1
- data/lib/node_mutation/version.rb +1 -1
- data/lib/node_mutation.rb +18 -18
- data/sig/node_mutation.rbs +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e659aa61170c013d38f7ae7fb18d7ce236b588cdd573d62af6a3eab683216a9f
|
4
|
+
data.tar.gz: 9fe3a31666deda95fef8cfd668e09b880d484e6763483ec573b3e5869e8b593b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1f3b204d8cb0e74e7d9afc52893e7b2930ac51104ae61975406d93de2c4e617fef2afd12e57e73f15efd6d9ad11a19a57c2adcad14893b99f778dd0ebbde0d5
|
7
|
+
data.tar.gz: a193a00b15323f8edcebd30dbd225c0e82a5a8831e4d3ae54a16aa211c0ba7e694283aea25353e081cb94e7db3a707c856de73837f6da04c1fa4700f522911e1
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# NodeMutation
|
2
2
|
|
3
|
+
## 1.21.1 (2023-10-01)
|
4
|
+
|
5
|
+
* Update `parser_node_ext` to 1.2.1
|
6
|
+
* Add `actions` to `NodeMutation::Struct::Action`
|
7
|
+
|
8
|
+
## 1.21.0 (2023-09-26)
|
9
|
+
|
10
|
+
* Rename `combine` dsl to `group`
|
11
|
+
|
3
12
|
## 1.20.0 (2023-09-24)
|
4
13
|
|
5
14
|
* Add `CombinedAction` to combine multiple actions.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
node_mutation (1.
|
4
|
+
node_mutation (1.21.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -37,7 +37,7 @@ GEM
|
|
37
37
|
parser (3.2.2.3)
|
38
38
|
ast (~> 2.4.1)
|
39
39
|
racc
|
40
|
-
parser_node_ext (1.2.
|
40
|
+
parser_node_ext (1.2.1)
|
41
41
|
parser
|
42
42
|
prettier_print (1.2.1)
|
43
43
|
pry (0.14.1)
|
data/README.md
CHANGED
@@ -51,6 +51,11 @@ mutation.replace_with node, 'create {{arguments}}'
|
|
51
51
|
mutation.wrap node, prefix: 'module Foo', suffix: 'end', newline: true
|
52
52
|
# no operation
|
53
53
|
mutation.noop
|
54
|
+
# group actions
|
55
|
+
mutation.group do
|
56
|
+
mutation.delete node, :message, :dot
|
57
|
+
mutation.replace node, 'receiver.caller.message', with: 'flat_map'
|
58
|
+
end
|
54
59
|
```
|
55
60
|
|
56
61
|
3. process actions and write the new source code to file:
|
@@ -1,14 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
4
|
-
class NodeMutation::
|
3
|
+
# GroupAction is compose of multiple actions.
|
4
|
+
class NodeMutation::GroupAction < NodeMutation::Action
|
5
5
|
DEFAULT_START = 2**30
|
6
6
|
|
7
|
-
attr_accessor :actions
|
8
|
-
|
9
7
|
def initialize
|
10
8
|
@actions = []
|
11
|
-
@type = :
|
9
|
+
@type = :group
|
12
10
|
end
|
13
11
|
|
14
12
|
def new_code
|
data/lib/node_mutation/action.rb
CHANGED
@@ -8,7 +8,9 @@ class NodeMutation::Action
|
|
8
8
|
# @return [Integer] end position
|
9
9
|
# @!attribute [rw] type
|
10
10
|
# @return [Symbol] action type, :insert, :replace or :delete
|
11
|
-
|
11
|
+
# @!attribute [rw] actions
|
12
|
+
# @return [Array<NodeMutation::Action>] child actions
|
13
|
+
attr_accessor :start, :end, :type, :actions
|
12
14
|
|
13
15
|
# Initialize an action.
|
14
16
|
#
|
data/lib/node_mutation/helper.rb
CHANGED
@@ -4,7 +4,7 @@ class NodeMutation::Helper
|
|
4
4
|
# It iterates over all actions, and calls the given block with each action.
|
5
5
|
def self.iterate_actions(actions, &block)
|
6
6
|
actions.each do |action|
|
7
|
-
if action.is_a?(NodeMutation::
|
7
|
+
if action.is_a?(NodeMutation::GroupAction)
|
8
8
|
iterate_actions(action.actions, &block)
|
9
9
|
else
|
10
10
|
block.call(action)
|
data/lib/node_mutation/result.rb
CHANGED
@@ -21,7 +21,7 @@ class NodeMutation::Result
|
|
21
21
|
def actions=(actions)
|
22
22
|
@actions =
|
23
23
|
actions.map { |action|
|
24
|
-
NodeMutation::Struct::Action.new(action.type, action.start, action.end, action.new_code)
|
24
|
+
NodeMutation::Struct::Action.new(action.type, action.start, action.end, action.new_code, action.actions)
|
25
25
|
}
|
26
26
|
end
|
27
27
|
|
data/lib/node_mutation/struct.rb
CHANGED
data/lib/node_mutation.rb
CHANGED
@@ -11,7 +11,7 @@ class NodeMutation
|
|
11
11
|
autoload :SyntaxTreeAdapter, "node_mutation/adapter/syntax_tree"
|
12
12
|
autoload :Action, 'node_mutation/action'
|
13
13
|
autoload :AppendAction, 'node_mutation/action/append_action'
|
14
|
-
autoload :
|
14
|
+
autoload :GroupAction, 'node_mutation/action/group_action'
|
15
15
|
autoload :DeleteAction, 'node_mutation/action/delete_action'
|
16
16
|
autoload :IndentAction, 'node_mutation/action/indent_action'
|
17
17
|
autoload :InsertAction, 'node_mutation/action/insert_action'
|
@@ -210,13 +210,13 @@ class NodeMutation
|
|
210
210
|
def wrap(node, prefix:, suffix:, newline: false)
|
211
211
|
if newline
|
212
212
|
indentation = NodeMutation.adapter.get_start_loc(node).column
|
213
|
-
|
213
|
+
group do
|
214
214
|
insert node, prefix + "\n" + (' ' * indentation), at: 'beginning'
|
215
215
|
insert node, "\n" + (' ' * indentation) + suffix, at: 'end'
|
216
216
|
indent node
|
217
217
|
end
|
218
218
|
else
|
219
|
-
|
219
|
+
group do
|
220
220
|
insert node, prefix, at: 'beginning'
|
221
221
|
insert node, suffix, at: 'end'
|
222
222
|
end
|
@@ -244,14 +244,14 @@ class NodeMutation
|
|
244
244
|
@actions << NoopAction.new(node).process
|
245
245
|
end
|
246
246
|
|
247
|
-
#
|
248
|
-
def
|
247
|
+
# group multiple actions
|
248
|
+
def group
|
249
249
|
current_actions = @actions
|
250
|
-
|
251
|
-
@actions =
|
250
|
+
group_action = GroupAction.new
|
251
|
+
@actions = group_action.actions
|
252
252
|
yield
|
253
253
|
@actions = current_actions
|
254
|
-
@actions <<
|
254
|
+
@actions << group_action.process
|
255
255
|
end
|
256
256
|
|
257
257
|
# Process actions and return the new source.
|
@@ -308,13 +308,13 @@ class NodeMutation
|
|
308
308
|
|
309
309
|
private
|
310
310
|
|
311
|
-
# It flattens a series of actions by removing any
|
311
|
+
# It flattens a series of actions by removing any GroupAction
|
312
312
|
# objects that contain only a single action. This is done recursively.
|
313
313
|
def flatten_actions(actions)
|
314
314
|
new_actions = []
|
315
315
|
actions.each do |action|
|
316
|
-
if action.is_a?(
|
317
|
-
new_actions <<
|
316
|
+
if action.is_a?(GroupAction)
|
317
|
+
new_actions << flatten_group_action(action)
|
318
318
|
else
|
319
319
|
new_actions << action
|
320
320
|
end
|
@@ -322,13 +322,13 @@ class NodeMutation
|
|
322
322
|
new_actions.compact
|
323
323
|
end
|
324
324
|
|
325
|
-
# It flattens a
|
326
|
-
def
|
325
|
+
# It flattens a group action.
|
326
|
+
def flatten_group_action(action)
|
327
327
|
if action.actions.empty?
|
328
328
|
nil
|
329
329
|
elsif action.actions.size == 1
|
330
|
-
if action.actions.first.is_a?(
|
331
|
-
|
330
|
+
if action.actions.first.is_a?(GroupAction)
|
331
|
+
flatten_group_action(action.actions.first)
|
332
332
|
else
|
333
333
|
action.actions.first
|
334
334
|
end
|
@@ -344,7 +344,7 @@ class NodeMutation
|
|
344
344
|
def sort_actions!(actions)
|
345
345
|
actions.sort_by! { |action| [action.start, action.end] }
|
346
346
|
actions.each do |action|
|
347
|
-
sort_actions!(action.actions) if action.is_a?(
|
347
|
+
sort_actions!(action.actions) if action.is_a?(GroupAction)
|
348
348
|
end
|
349
349
|
end
|
350
350
|
|
@@ -354,7 +354,7 @@ class NodeMutation
|
|
354
354
|
# @return [String] new source code
|
355
355
|
def rewrite_source(source, actions)
|
356
356
|
actions.reverse_each do |action|
|
357
|
-
if action.is_a?(
|
357
|
+
if action.is_a?(GroupAction)
|
358
358
|
source = rewrite_source(source, action.actions)
|
359
359
|
else
|
360
360
|
source[action.start...action.end] = action.new_code if action.new_code
|
@@ -385,7 +385,7 @@ class NodeMutation
|
|
385
385
|
j -= 1
|
386
386
|
end
|
387
387
|
actions.each do |action|
|
388
|
-
conflict_actions.concat(get_conflict_actions(action.actions)) if action.is_a?(
|
388
|
+
conflict_actions.concat(get_conflict_actions(action.actions)) if action.is_a?(GroupAction)
|
389
389
|
end
|
390
390
|
conflict_actions
|
391
391
|
end
|
data/sig/node_mutation.rbs
CHANGED
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.21.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-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ast node mutation apis
|
14
14
|
email:
|
@@ -28,8 +28,8 @@ files:
|
|
28
28
|
- lib/node_mutation.rb
|
29
29
|
- lib/node_mutation/action.rb
|
30
30
|
- lib/node_mutation/action/append_action.rb
|
31
|
-
- lib/node_mutation/action/combined_action.rb
|
32
31
|
- lib/node_mutation/action/delete_action.rb
|
32
|
+
- lib/node_mutation/action/group_action.rb
|
33
33
|
- lib/node_mutation/action/indent_action.rb
|
34
34
|
- lib/node_mutation/action/insert_action.rb
|
35
35
|
- lib/node_mutation/action/noop_action.rb
|
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '0'
|
75
75
|
requirements: []
|
76
|
-
rubygems_version: 3.4.
|
76
|
+
rubygems_version: 3.4.20
|
77
77
|
signing_key:
|
78
78
|
specification_version: 4
|
79
79
|
summary: ast node mutation apis
|