node_mutation 1.11.0 → 1.12.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: d41515d4be39619d95802e531496159773a1202de61c8fa3c3d269d2a72926da
4
- data.tar.gz: 2bd0415eec90cb45cdc2dcfacce1cf6529fcdf9f12e6fcd44054aa273e4c4b0b
3
+ metadata.gz: 97dd9f7362d033a9572811a001fcda89634580d2af15bd7736209dbb7228ba56
4
+ data.tar.gz: e7d4eb04cd7c0d739622105b8aedf85bc8e1e72c8e4e278bf87d840d05787fea
5
5
  SHA512:
6
- metadata.gz: 656c00012eac7a7b6d2229933fb7e08fd7f0f9a986e90f7db69cc5129097702a2850cdf5494c6f1dae3d6f7da9d0ba642ffd7eb4362517a5c0f465a0d49a52d3
7
- data.tar.gz: 4faced52fd71462a10c54970afb4412858c355c96bf8713e132f886535d3a1309aa792bc1f4e946c0549492c351c9750fa3ab1d05c3ecd0fda0d58f84c98427f
6
+ metadata.gz: 0e44b67cb7c1dc8ef15ed16ac92c10400639704df32d0a06f2db40a68b8ea6259fe73d7c0dda2df8c5dd07c03137e95d7eca62ae0948c27b2e93b36fa5752cc3
7
+ data.tar.gz: f07ebe6b3712ae3d13b9cf06b7b5dd3e45d68ee7cb7571a9e227f2318de16271f4db2aea0a1ec62a48bc71e6666f8c91e9fda00628cf32d685af083890f70b41
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.12.0 (2023-03-23)
4
+
5
+ * Support `{key}_pair` for a `hash` node
6
+
3
7
  ## 1.11.0 (2023-03-20)
4
8
 
5
9
  * Calculate position properly for `add_comma`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.11.0)
4
+ node_mutation (1.12.0)
5
5
  erubis
6
6
 
7
7
  GEM
@@ -17,41 +17,36 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
17
17
  def rewritten_source(node, code)
18
18
  code.gsub(/{{(.+?)}}/m) do
19
19
  old_code = Regexp.last_match(1)
20
- first_key = old_code.split('.').first
21
- if node.respond_to?(first_key)
22
- evaluated = child_node_by_name(node, old_code)
23
- case evaluated
24
- when Parser::AST::Node
25
- if evaluated.type == :args
26
- evaluated.loc.expression.source[1...-1]
20
+ evaluated = child_node_by_name(node, old_code)
21
+ case evaluated
22
+ when Parser::AST::Node
23
+ if evaluated.type == :args
24
+ evaluated.loc.expression.source[1...-1]
25
+ else
26
+ evaluated.loc.expression.source
27
+ end
28
+ when Array
29
+ if evaluated.size > 0
30
+ file_source = file_content(evaluated.first)
31
+ source = file_source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos]
32
+ lines = source.split "\n"
33
+ lines_count = lines.length
34
+ if lines_count > 1 && lines_count == evaluated.size
35
+ new_code = []
36
+ lines.each_with_index { |line, index|
37
+ new_code << (index == 0 ? line : line[evaluated.first.indent - 2..-1])
38
+ }
39
+ new_code.join("\n")
27
40
  else
28
- evaluated.loc.expression.source
41
+ source
29
42
  end
30
- when Array
31
- if evaluated.size > 0
32
- file_source = file_content(evaluated.first)
33
- source = file_source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos]
34
- lines = source.split "\n"
35
- lines_count = lines.length
36
- if lines_count > 1 && lines_count == evaluated.size
37
- new_code = []
38
- lines.each_with_index { |line, index|
39
- new_code << (index == 0 ? line : line[evaluated.first.indent - 2..-1])
40
- }
41
- new_code.join("\n")
42
- else
43
- source
44
- end
45
- end
46
- when String, Symbol, Integer, Float
47
- evaluated
48
- when NilClass
49
- ''
50
- else
51
- raise "can not parse \"#{code}\""
52
43
  end
44
+ when String, Symbol, Integer, Float
45
+ evaluated
46
+ when NilClass
47
+ ''
53
48
  else
54
- raise NodeMutation::MethodNotSupported, "#{first_key} is not supported for #{get_source(node)}"
49
+ raise "can not parse \"#{code}\""
55
50
  end
56
51
  end
57
52
  end
@@ -84,7 +79,10 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
84
79
 
85
80
  case [node.type, child_name.to_sym]
86
81
  when %i[block pipes], %i[def parentheses], %i[defs parentheses]
87
- NodeMutation::Range.new(node.arguments.first.loc.expression.begin_pos - 1, node.arguments.last.loc.expression.end_pos + 1)
82
+ NodeMutation::Range.new(
83
+ node.arguments.first.loc.expression.begin_pos - 1,
84
+ node.arguments.last.loc.expression.end_pos + 1
85
+ )
88
86
  when %i[block arguments], %i[def arguments], %i[defs arguments]
89
87
  NodeMutation::Range.new(node.arguments.first.loc.expression.begin_pos, node.arguments.last.loc.expression.end_pos)
90
88
  when %i[class name], %i[const name], %i[def name], %i[defs name]
@@ -108,6 +106,15 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
108
106
  NodeMutation::Range.new(node.loc.begin.begin_pos, node.loc.end.end_pos)
109
107
  end
110
108
  else
109
+ if node.type == :hash && child_name.to_s.end_with?('_pair')
110
+ pair_node = node.pairs.find { |pair| pair.key.to_value.to_s == child_name.to_s[0..-6] }
111
+ raise NodeMutation::MethodNotSupported,
112
+ "#{direct_child_name} is not supported for #{get_source(node)}" unless pair_node
113
+ return child_node_range(pair, nested_child_name) if nested_child_name
114
+
115
+ return NodeMutation::Range.new(pair_node.loc.expression.begin_pos, pair_node.loc.expression.end_pos)
116
+ end
117
+
111
118
  raise NodeMutation::MethodNotSupported,
112
119
  "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
113
120
 
@@ -178,6 +185,15 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
178
185
  return child_node
179
186
  end
180
187
 
188
+ if node.is_a?(Parser::AST::Node) && node.type == :hash && direct_child_name.end_with?('_pair')
189
+ pair_node = node.pairs.find { |pair| pair.key.to_value.to_s == direct_child_name[0..-6] }
190
+ raise NodeMutation::MethodNotSupported,
191
+ "#{direct_child_name} is not supported for #{get_source(node)}" unless pair_node
192
+ return child_node_by_name(pair_node, nested_child_name) if nested_child_name
193
+
194
+ return pair_node
195
+ end
196
+
181
197
  if node.respond_to?(direct_child_name)
182
198
  child_node = node.send(direct_child_name)
183
199
  elsif direct_child_name.include?('(') && direct_child_name.include?(')')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.11.0"
4
+ VERSION = "1.12.0"
5
5
  end
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.11.0
4
+ version: 1.12.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-03-20 00:00:00.000000000 Z
11
+ date: 2023-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubis