node_mutation 1.15.2 → 1.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fadeda3c2cf8d3f0d618b4a9996241619ab404fef419e87026a443c8078e06f
4
- data.tar.gz: 39d9939b21c96821ce056ad1983b301d16fc1d49c936ce082e3b208a6a93547c
3
+ metadata.gz: 14e76d23c3a0be62554de359db928e7f0e70fe75b6c13ecb0655add5af735333
4
+ data.tar.gz: 1777be0d1e8c4044fa175dcc8f9f9e3431283fdf039392cba9f8fe4fd656c676
5
5
  SHA512:
6
- metadata.gz: bdbfe2e830ef226679fa3556b52a23f67755afb2e33a7f8efaf5bd47e4f0e1d59f3b43af99bf4542587b2c9eee832a5309f0531689cc98cb954e38ca03bc0edc
7
- data.tar.gz: b9b9d033e38d84f4672cdb3630d44f8179af09ceb268b25d9e88fe3a86eff97d543f710d3f0c3ecbb3f8a2c22c45de2998a02b32b454dd2eff08d6d907ba4f4e
6
+ metadata.gz: ef64c7156966a2d80c4bf823e0c3330d8934cf52306a9f179bac7105b52c82e78bb9b0d958510165a541fd1fe550b3eec7c7e78eaf3aa7f3207a5b0df1f9e4e3
7
+ data.tar.gz: 969399df03dc8e3f5c15e55f37a3664bf0945a41e5ff244542319b8a2c20d6afff54dc3aee72b6b4e6c90c6ce84428b6008d14d3dc81e343c68296e78b387218
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.16.0 (2023-05-10)
4
+
5
+ * Support {key}_value for a hash node
6
+
7
+ ## 1.15.3 (2023-04-18)
8
+
9
+ * Add `type` to `Action`
10
+
3
11
  ## 1.15.2 (2023-04-17)
4
12
 
5
13
  * Support `const` `double_colon` in child_node_range
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.15.2)
4
+ node_mutation (1.16.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,6 +2,11 @@
2
2
 
3
3
  # AppendAction appends code to the bottom of node body.
4
4
  class NodeMutation::AppendAction < NodeMutation::Action
5
+ def initialize(node, code)
6
+ super(node, code)
7
+ @type = :insert
8
+ end
9
+
5
10
  private
6
11
 
7
12
  END_LENGTH = "\nend".length
@@ -11,6 +11,7 @@ class NodeMutation::DeleteAction < NodeMutation::Action
11
11
  super(node, nil)
12
12
  @selectors = selectors
13
13
  @and_comma = and_comma
14
+ @type = :delete
14
15
  end
15
16
 
16
17
  # The rewritten code, always empty string.
@@ -9,6 +9,7 @@ class NodeMutation::IndentAction < NodeMutation::Action
9
9
  def initialize(node, tab_size = 1)
10
10
  super(node, nil)
11
11
  @tab_size = tab_size
12
+ @type = :replace
12
13
  end
13
14
 
14
15
  # The rewritten source code with proper indent.
@@ -14,6 +14,7 @@ class NodeMutation::InsertAction < NodeMutation::Action
14
14
  @at = at
15
15
  @to = to
16
16
  @and_comma = and_comma
17
+ @type = :insert
17
18
  end
18
19
 
19
20
  # The rewritten source code.
@@ -2,6 +2,11 @@
2
2
 
3
3
  # PrependAction to prepend code to the top of node body.
4
4
  class NodeMutation::PrependAction < NodeMutation::Action
5
+ def initialize(node, code)
6
+ super(node, code)
7
+ @type = :insert
8
+ end
9
+
5
10
  private
6
11
 
7
12
  DO_LENGTH = ' do'.length
@@ -10,6 +10,7 @@ class NodeMutation::RemoveAction < NodeMutation::Action
10
10
  def initialize(node, and_comma: false)
11
11
  super(node, nil)
12
12
  @and_comma = and_comma
13
+ @type = :delete
13
14
  end
14
15
 
15
16
  # The rewritten code, always empty string.
@@ -10,6 +10,7 @@ class NodeMutation::ReplaceAction < NodeMutation::Action
10
10
  def initialize(node, *selectors, with:)
11
11
  super(node, with)
12
12
  @selectors = selectors
13
+ @type = :replace
13
14
  end
14
15
 
15
16
  # The rewritten source code.
@@ -2,6 +2,11 @@
2
2
 
3
3
  # ReplaceWithAction to replace code.
4
4
  class NodeMutation::ReplaceWithAction < NodeMutation::Action
5
+ def initialize(node, code)
6
+ super(node, code)
7
+ @type = :replace
8
+ end
9
+
5
10
  # The rewritten source code with proper indent.
6
11
  #
7
12
  # @return [String] rewritten code.
@@ -6,7 +6,9 @@ class NodeMutation::Action
6
6
  # @return [Integer] start position
7
7
  # @!attribute [rw] end
8
8
  # @return [Integer] end position
9
- attr_accessor :start, :end
9
+ # @!attribute [rw] type
10
+ # @return [Symbol] action type, :insert, :replace or :delete
11
+ attr_accessor :start, :end, :type
10
12
 
11
13
  # Initialize an action.
12
14
  #
@@ -120,6 +120,15 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
120
120
  return NodeMutation::Struct::Range.new(pair_node.loc.expression.begin_pos, pair_node.loc.expression.end_pos)
121
121
  end
122
122
 
123
+ if node.type == :hash && child_name.to_s.end_with?('_value')
124
+ pair_node = node.pairs.find { |pair| pair.key.to_value.to_s == child_name.to_s[0..-7] }
125
+ raise NodeMutation::MethodNotSupported,
126
+ "#{direct_child_name} is not supported for #{get_source(node)}" unless pair_node
127
+ return child_node_range(pair.value, nested_child_name) if nested_child_name
128
+
129
+ return NodeMutation::Struct::Range.new(pair_node.value.loc.expression.begin_pos, pair_node.value.loc.expression.end_pos)
130
+ end
131
+
123
132
  raise NodeMutation::MethodNotSupported,
124
133
  "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
125
134
 
@@ -206,6 +215,15 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
206
215
  return pair_node
207
216
  end
208
217
 
218
+ if node.is_a?(Parser::AST::Node) && node.type == :hash && direct_child_name.end_with?('_value')
219
+ pair_node = node.pairs.find { |pair| pair.key.to_value.to_s == direct_child_name[0..-7] }
220
+ raise NodeMutation::MethodNotSupported,
221
+ "#{direct_child_name} is not supported for #{get_source(node)}" unless pair_node
222
+ return child_node_by_name(pair_node.value, nested_child_name) if nested_child_name
223
+
224
+ return pair_node.value
225
+ end
226
+
209
227
  if node.respond_to?(direct_child_name)
210
228
  child_node = node.send(direct_child_name)
211
229
  elsif direct_child_name.include?('(') && direct_child_name.include?(')')
@@ -19,7 +19,7 @@ class NodeMutation::Result
19
19
  end
20
20
 
21
21
  def actions=(actions)
22
- @actions = actions.map { |action| NodeMutation::Struct::Action.new(action.start, action.end, action.new_code) }
22
+ @actions = actions.map { |action| NodeMutation::Struct::Action.new(action.type, action.start, action.end, action.new_code) }
23
23
  end
24
24
 
25
25
  def to_json(*args)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation::Struct
4
- Action = Struct.new(:start, :end, :new_code)
4
+ Action = Struct.new(:type, :start, :end, :new_code)
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.15.2"
4
+ VERSION = "1.16.0"
5
5
  end
@@ -1,5 +1,6 @@
1
1
  class NodeMutation::Struct
2
2
  class Action < ::Struct
3
+ prop :type, Symbol
3
4
  prop :start, Integer
4
5
  prop :end, Integer
5
6
  prop :new_code, String
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.15.2
4
+ version: 1.16.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-04-17 00:00:00.000000000 Z
11
+ date: 2023-05-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ast node mutation apis
14
14
  email: