node_mutation 1.17.1 → 1.18.1

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: d7b6c582369f095034122ac0dc0d1cafaba27a86049bb80c36b1d8f50dc8876c
4
- data.tar.gz: d81a61d2316dad6242b81df48b61752459638e44c03935af6f618b006f5fa339
3
+ metadata.gz: 402162b36464bad6b051c2d55da7e59cae24f3f92963bed6ae3610ab8529c52b
4
+ data.tar.gz: e0a9fc2d5a8af6d700daf7d4a1a134c17e30ad34e079386360caef12c93633cf
5
5
  SHA512:
6
- metadata.gz: 3f16a4e2b54ac9820c35e3d0bd31b2d3b6a23a08144e8a2486d8093fea95087e2b75a27924af69908ba4c6c6f88f0bcd56d5a9ea944fbb395ae422b18e0fff2b
7
- data.tar.gz: e2a1ea1260c204562d957a7d756811c565212e56c6660d7e03c90b15341b03728859fa3158fce7d51ec50ad30cbb7c070606698b548b6f4fa3ca9fe73a27235c
6
+ metadata.gz: aa89ffef035a9d5aca4cc2e8b68c6e2179ac6d228fd06e0805395f5a3cd447c49a249ba96a7910438a9b1a5faf5bb3a9f325126db87f8f4cd45dcac43cdbdc60
7
+ data.tar.gz: d303b85ef1f0b884e8b88f8fa9cb7547e9bf7bd3d619f5587e793affd68509447dddc6323f7a090cbaa55fbe7be8535df9fec2462aa6b43499eee868a3a53fbf
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.18.1 (2023-05-20)
4
+
5
+ * Support block/class/def/defs/module `body` in `child_node_range`
6
+ * Return nil if `arguments` is empty in `child_node_range`
7
+
8
+ ## 1.18.0 (2023-05-16)
9
+
10
+ * Rename `file_content` to `file_source`
11
+ * Add `SyntaxTreeAdapter#file_source`
12
+
3
13
  ## 1.17.1 (2023-05-16)
4
14
 
5
15
  * Require `parser` and `syntax_tree` in adapter
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.17.1)
4
+ node_mutation (1.18.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -108,6 +108,6 @@ class NodeMutation::Action
108
108
  #
109
109
  # @return [String]
110
110
  def file_source
111
- @file_source ||= NodeMutation.adapter.file_content(@node)
111
+ @file_source ||= NodeMutation.adapter.file_source(@node)
112
112
  end
113
113
  end
@@ -8,7 +8,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
8
8
  if node.is_a?(Array)
9
9
  return "" if node.empty?
10
10
 
11
- source = file_content(node.first)
11
+ source = file_source(node.first)
12
12
  source[node.first.loc.expression.begin_pos...node.last.loc.expression.end_pos]
13
13
  else
14
14
  node.loc.expression.source
@@ -28,7 +28,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
28
28
  end
29
29
  when Array
30
30
  if evaluated.size > 0
31
- file_source = file_content(evaluated.first)
31
+ file_source = file_source(evaluated.first)
32
32
  source = file_source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos]
33
33
  lines = source.split "\n"
34
34
  lines_count = lines.length
@@ -52,7 +52,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
52
52
  end
53
53
  end
54
54
 
55
- def file_content(node)
55
+ def file_source(node)
56
56
  node.loc.expression.source_buffer.source
57
57
  end
58
58
 
@@ -80,15 +80,32 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
80
80
 
81
81
  case [node.type, child_name.to_sym]
82
82
  when %i[block pipes], %i[def parentheses], %i[defs parentheses]
83
- NodeMutation::Struct::Range.new(
84
- node.arguments.first.loc.expression.begin_pos - 1,
85
- node.arguments.last.loc.expression.end_pos + 1
86
- )
83
+ if node.arguments.empty?
84
+ nil
85
+ else
86
+ NodeMutation::Struct::Range.new(
87
+ node.arguments.first.loc.expression.begin_pos - 1,
88
+ node.arguments.last.loc.expression.end_pos + 1
89
+ )
90
+ end
87
91
  when %i[block arguments], %i[def arguments], %i[defs arguments]
88
- NodeMutation::Struct::Range.new(
89
- node.arguments.first.loc.expression.begin_pos,
90
- node.arguments.last.loc.expression.end_pos
91
- )
92
+ if node.arguments.empty?
93
+ nil
94
+ else
95
+ NodeMutation::Struct::Range.new(
96
+ node.arguments.first.loc.expression.begin_pos,
97
+ node.arguments.last.loc.expression.end_pos
98
+ )
99
+ end
100
+ when %i[block body], %i[class body], %i[def body], %i[defs body], %i[module body]
101
+ if node.body.empty?
102
+ nil
103
+ else
104
+ NodeMutation::Struct::Range.new(
105
+ node.body.first.loc.expression.begin_pos,
106
+ node.body.last.loc.expression.end_pos
107
+ )
108
+ end
92
109
  when %i[class name], %i[const name], %i[cvar name], %i[def name], %i[defs name], %i[gvar name], %i[ivar name], %i[lvar name]
93
110
  NodeMutation::Struct::Range.new(node.loc.name.begin_pos, node.loc.name.end_pos)
94
111
  when %i[const double_colon]
@@ -162,7 +179,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
162
179
  end
163
180
 
164
181
  def get_indent(node)
165
- file_content(node).split("\n")[get_start_loc(node).line - 1][/\A */].size
182
+ file_source(node).split("\n")[get_start_loc(node).line - 1][/\A */].size
166
183
  end
167
184
 
168
185
  private
@@ -44,6 +44,10 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
44
44
  end
45
45
  end
46
46
 
47
+ def file_source(node)
48
+ node.source
49
+ end
50
+
47
51
  def child_node_range(node, child_name)
48
52
  child_node = child_node_by_name(node, child_name)
49
53
  return nil if child_node.nil?
@@ -23,8 +23,8 @@ class NodeMutation::Adapter
23
23
  # The file content of the ast node file
24
24
  # @param node [Node] ast node
25
25
  # @return file content
26
- def file_content(node)
27
- raise NotImplementedError, "file_content is not implemented"
26
+ def file_source(node)
27
+ raise NotImplementedError, "file_source is not implemented"
28
28
  end
29
29
 
30
30
  # Get the start/end range of the child node
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.17.1"
4
+ VERSION = "1.18.1"
5
5
  end
@@ -3,7 +3,7 @@ class NodeMutation::Adapter[T]
3
3
 
4
4
  def rewritten_source: (node: T, code: String) -> String
5
5
 
6
- def file_content: (node: T) -> String
6
+ def file_source: (node: T) -> String
7
7
 
8
8
  def child_node_range: (node: T, child_name: String) -> NodeMutation::Struct::Range
9
9
 
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.17.1
4
+ version: 1.18.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-05-15 00:00:00.000000000 Z
11
+ date: 2023-05-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ast node mutation apis
14
14
  email: