node_mutation 1.23.3 → 1.24.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: e941ef481546d5db16679d319312e0541c63b61cf5dceefcef91b97625e9ee18
4
- data.tar.gz: e0638f659b75f11cbfadcbddb2a108e50b2e0055918a608b352661a54f5b66b6
3
+ metadata.gz: 6e67a8d6feebbc0ca9e6dd85003a976250c3718e8dd6f7192ef0847b2cc1385f
4
+ data.tar.gz: c4b1d58061ac4661ac1eeb85303438f87d4ff563d8181a0fea8c8ac24e88dde3
5
5
  SHA512:
6
- metadata.gz: 5546f83edb1e5cbf6550a9400bacee3d478ead1f8898572be71aaa1e136ffb4fe62aea1927f5eae517d8cbc171b3371700fda96ca8ec3b581e38b5a257194467
7
- data.tar.gz: cdd3a1497c5e6a4e451cdba7af3b55287a3859a4b435b7a8c63108851b1599babd17e78c68d91126ae382c6ecfc98ce2d982d6f871a0ab7c828e9685d3a1cce5
6
+ metadata.gz: c99a8e8fce9fa6c610e93aeaa9aa5ad9fe89c5bb6cac19f216c34872c9bd57e2900d0b5e566a5f4e04c967f5383e8ace02d945cb94146602717b110a09ddbcb2
7
+ data.tar.gz: ff6f52635d513523c5bf8d617e7e277ee71166e8f0cbee60fe3a94ffe379062e687128cc3378c791940e211a863e8d9585cd07c9e26db95f98ff1a58455b3906
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.24.1 (2024-03-05)
4
+
5
+ * Adjust `start` and `end` position of `AppendAction` and `PrependAction`
6
+
7
+ ## 1.24.0 (2024-03-03)
8
+
9
+ * Remove `Adapter#get_indent`
10
+
3
11
  ## 1.23.3 (2024-02-20)
4
12
 
5
13
  * `child_node_range` supports `LocalVariableReadNode#name`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.23.3)
4
+ node_mutation (1.24.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -12,13 +12,27 @@ class NodeMutation::AppendAction < NodeMutation::Action
12
12
  @type = :insert
13
13
  end
14
14
 
15
+ # The rewritten source code with proper indent.
16
+ #
17
+ # @return [String] rewritten code.
18
+ def new_code
19
+ if rewritten_source.split("\n").length > 1
20
+ "\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }.join("\n") + "\n"
21
+ else
22
+ indent(@node) + rewritten_source + "\n"
23
+ end
24
+ end
25
+
15
26
  private
16
27
 
17
28
  END_LENGTH = "\nend".length
18
29
 
19
30
  # Calculate the begin the end positions.
20
31
  def calculate_position
21
- @start = @adapter.get_end(@node) - @adapter.get_start_loc(@node).column - END_LENGTH
32
+ node_end = @adapter.get_end(@node)
33
+ node_source = @adapter.get_source(@node)
34
+ last_line = node_source.split("\n").last
35
+ @start = node_end - last_line.length
22
36
  @end = @start
23
37
  end
24
38
 
@@ -12,6 +12,17 @@ class NodeMutation::PrependAction < NodeMutation::Action
12
12
  @type = :insert
13
13
  end
14
14
 
15
+ # The rewritten source code with proper indent.
16
+ #
17
+ # @return [String] rewritten code.
18
+ def new_code
19
+ if rewritten_source.split("\n").length > 1
20
+ "\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }.join("\n") + "\n"
21
+ else
22
+ indent(@node) + rewritten_source + "\n"
23
+ end
24
+ end
25
+
15
26
  private
16
27
 
17
28
  # Calculate the begin and end positions.
@@ -19,7 +30,7 @@ class NodeMutation::PrependAction < NodeMutation::Action
19
30
  node_start = @adapter.get_start(@node)
20
31
  node_source = @adapter.get_source(@node)
21
32
  first_line = node_source.split("\n").first
22
- @start = first_line.end_with?("do") ? node_start + first_line.rindex("do") + "do".length : node_start + first_line.length
33
+ @start = node_start + first_line.length + "\n".length
23
34
  @end = @start
24
35
  end
25
36
 
@@ -31,18 +31,6 @@ class NodeMutation::Action
31
31
  self
32
32
  end
33
33
 
34
- # The rewritten source code with proper indent.
35
- #
36
- # @return [String] rewritten code.
37
- def new_code
38
- if rewritten_source.split("\n").length > 1
39
- "\n\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }
40
- .join("\n")
41
- else
42
- "\n" + indent(@node) + rewritten_source
43
- end
44
- end
45
-
46
34
  def to_struct
47
35
  to_struct_actions = @actions ? @actions.map(&:to_struct) : nil
48
36
  NodeMutation::Struct::Action.new(@type, @start, @end, new_code, to_struct_actions)
@@ -82,7 +82,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
82
82
  if lines_count > 1 && lines_count == evaluated.size
83
83
  new_code = []
84
84
  lines.each_with_index { |line, index|
85
- new_code << (index == 0 ? line : line[get_indent(evaluated.first) - NodeMutation.tab_width..-1])
85
+ new_code << (index == 0 ? line : line[get_start_loc(evaluated.first).column - NodeMutation.tab_width..-1])
86
86
  }
87
87
  new_code.join("\n")
88
88
  else
@@ -253,10 +253,6 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
253
253
  NodeMutation::Struct::Location.new(end_loc.line, end_loc.column)
254
254
  end
255
255
 
256
- def get_indent(node)
257
- file_source(node).split("\n")[get_start_loc(node).line - 1][/\A */].size
258
- end
259
-
260
256
  private
261
257
 
262
258
  def child_node_by_name(node, child_name)
@@ -74,7 +74,7 @@ class NodeMutation::PrismAdapter < NodeMutation::Adapter
74
74
  if lines_count > 1 && lines_count == evaluated.size
75
75
  new_code = []
76
76
  lines.each_with_index { |line, index|
77
- new_code << (index == 0 ? line : line[get_indent(evaluated.first) - NodeMutation.tab_width..-1])
77
+ new_code << (index == 0 ? line : line[get_start_loc(evaluated.first).column - NodeMutation.tab_width..-1])
78
78
  }
79
79
  new_code.join("\n")
80
80
  else
@@ -186,10 +186,6 @@ class NodeMutation::PrismAdapter < NodeMutation::Adapter
186
186
  NodeMutation::Struct::Location.new(node.location.end_line, node.location.end_column)
187
187
  end
188
188
 
189
- def get_indent(node)
190
- node.location.start_column
191
- end
192
-
193
189
  private
194
190
 
195
191
  def child_node_by_name(node, child_name)
@@ -74,7 +74,7 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
74
74
  if lines_count > 1 && lines_count == evaluated.size
75
75
  new_code = []
76
76
  lines.each_with_index { |line, index|
77
- new_code << (index == 0 ? line : line[get_indent(evaluated.first) - NodeMutation.tab_width..-1])
77
+ new_code << (index == 0 ? line : line[get_start_loc(evaluated.first).column - NodeMutation.tab_width..-1])
78
78
  }
79
79
  new_code.join("\n")
80
80
  else
@@ -185,10 +185,6 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
185
185
  NodeMutation::Struct::Location.new(node.location.end_line, node.location.end_column)
186
186
  end
187
187
 
188
- def get_indent(node)
189
- node.location.start_column
190
- end
191
-
192
188
  private
193
189
 
194
190
  def child_node_by_name(node, child_name)
@@ -66,11 +66,4 @@ class NodeMutation::Adapter
66
66
  def get_end_loc(node, child_name = nil)
67
67
  raise NotImplementedError, "get_end_loc is not implemented"
68
68
  end
69
-
70
- # Get indent of ast node
71
- # @param node [Node] ast node
72
- # @return [Number] indent
73
- def get_indent(node)
74
- raise NotImplementedError, "get_indent is not implemented"
75
- end
76
69
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.23.3"
4
+ VERSION = "1.24.1"
5
5
  end
@@ -14,6 +14,4 @@ class NodeMutation::Adapter[T]
14
14
  def get_start_loc: (node: T, ?child_name: String) -> NodeMutation::Struct::Location
15
15
 
16
16
  def get_end_loc: (node: T, ?child_name: String) -> NodeMutation::Struct::Location
17
-
18
- def get_indent: (node: T) -> Integer
19
17
  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.23.3
4
+ version: 1.24.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: 2024-02-20 00:00:00.000000000 Z
11
+ date: 2024-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ast node mutation apis
14
14
  email: