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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd49bceca35429738e6137151b99634e0ef83801214ba58e80c5e1d67b299b07
4
- data.tar.gz: ef1f95f7edcf89b73b3eeb11137e374c813460cd4c5c8c67b970a5b29acc4e47
3
+ metadata.gz: e659aa61170c013d38f7ae7fb18d7ce236b588cdd573d62af6a3eab683216a9f
4
+ data.tar.gz: 9fe3a31666deda95fef8cfd668e09b880d484e6763483ec573b3e5869e8b593b
5
5
  SHA512:
6
- metadata.gz: 71f84423b7cc2edd182ce760c984c1fef78f17ca267c72fb02f2eea9674dad5cf7c284180e03dbb888b5872e1157110e656d1f3587df17fe72140110594f0665
7
- data.tar.gz: 951e9c6ba7afb239dab12428b40509e7d66178fe4776a317ebe5757a8d142e4fabe59ed1e75c5bedc4fe6a08e531c5176112cd48131b91422d1366e9d36af89a
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.20.0)
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.0)
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
- # CombinedAction combines multiple actions.
4
- class NodeMutation::CombinedAction < NodeMutation::Action
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 = :combined
9
+ @type = :group
12
10
  end
13
11
 
14
12
  def new_code
@@ -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
- attr_accessor :start, :end, :type
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
  #
@@ -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::CombinedAction)
7
+ if action.is_a?(NodeMutation::GroupAction)
8
8
  iterate_actions(action.actions, &block)
9
9
  else
10
10
  block.call(action)
@@ -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
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation::Struct
4
- Action = Struct.new(:type, :start, :end, :new_code)
4
+ Action = Struct.new(:type, :start, :end, :new_code, :actions)
5
5
  Location = Struct.new(:line, :column)
6
6
  Range = Struct.new(:start, :end)
7
7
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.20.0"
4
+ VERSION = "1.21.1"
5
5
  end
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 :CombinedAction, 'node_mutation/action/combined_action'
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
- combine do
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
- combine do
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
- # Combine multiple actions
248
- def combine
247
+ # group multiple actions
248
+ def group
249
249
  current_actions = @actions
250
- combined_action = CombinedAction.new
251
- @actions = combined_action.actions
250
+ group_action = GroupAction.new
251
+ @actions = group_action.actions
252
252
  yield
253
253
  @actions = current_actions
254
- @actions << combined_action.process
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 CombinedAction
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?(CombinedAction)
317
- new_actions << flatten_combined_action(action)
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 combined action.
326
- def flatten_combined_action(action)
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?(CombinedAction)
331
- flatten_combined_action(action.actions.first)
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?(CombinedAction)
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?(CombinedAction)
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?(CombinedAction)
388
+ conflict_actions.concat(get_conflict_actions(action.actions)) if action.is_a?(GroupAction)
389
389
  end
390
390
  conflict_actions
391
391
  end
@@ -39,7 +39,7 @@ class NodeMutation[T]
39
39
 
40
40
  def noop: (node: T) -> void
41
41
 
42
- def combine: () { () -> void } -> void
42
+ def group: () { () -> void } -> void
43
43
 
44
44
  def process: () -> NodeMutation::Result
45
45
 
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.20.0
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-09-24 00:00:00.000000000 Z
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.18
76
+ rubygems_version: 3.4.20
77
77
  signing_key:
78
78
  specification_version: 4
79
79
  summary: ast node mutation apis