node_mutation 1.12.0 → 1.12.2

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: 97dd9f7362d033a9572811a001fcda89634580d2af15bd7736209dbb7228ba56
4
- data.tar.gz: e7d4eb04cd7c0d739622105b8aedf85bc8e1e72c8e4e278bf87d840d05787fea
3
+ metadata.gz: cf57adfc552f6b661bf4c08a35976f017dc7439c914cb1e419e656952d77bcef
4
+ data.tar.gz: ee142c7f5b1b8e2ae7a0b51a36dd8718232918c2fa57595e84470e284fe86411
5
5
  SHA512:
6
- metadata.gz: 0e44b67cb7c1dc8ef15ed16ac92c10400639704df32d0a06f2db40a68b8ea6259fe73d7c0dda2df8c5dd07c03137e95d7eca62ae0948c27b2e93b36fa5752cc3
7
- data.tar.gz: f07ebe6b3712ae3d13b9cf06b7b5dd3e45d68ee7cb7571a9e227f2318de16271f4db2aea0a1ec62a48bc71e6666f8c91e9fda00628cf32d685af083890f70b41
6
+ metadata.gz: 6a4106b2a6324eac65cb782cb3e3863b4544f7798bb87313205bb5b0cf62d55fc3ed75feb36161fe4810ca5ae030f26bb66347d5b1779a035123a5cddaff12b3
7
+ data.tar.gz: fa65851e47d51b26ca21efc76d4b16ebc1d3743e7dae9d616ba696dc2491e600077b934aea8f3c8db5345c9220ddbf7a883461058d0c73e381f1e5ef99f589db
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.12.2 (2023-03-25)
4
+
5
+ * `remove_whitespace` handles more cases
6
+
7
+ ## 1.12.1 (2023-03-23)
8
+
9
+ * Define `Action`, `Location` and `Range` in `NodeMutation::Struct`
10
+
3
11
  ## 1.12.0 (2023-03-23)
4
12
 
5
13
  * Support `{key}_pair` for a `hash` node
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.12.0)
4
+ node_mutation (1.12.2)
5
5
  erubis
6
6
 
7
7
  GEM
@@ -54,12 +54,15 @@ class NodeMutation::Action
54
54
  end
55
55
 
56
56
  # remove unused whitespace.
57
- # e.g. `foobar(foo, bar)`, if we remove `foo`, the whitespace should also be removed,
58
- # the code should be changed to `foobar(bar)`.
57
+ # e.g. `%i[foo bar]`, if we remove `foo`, the whitespace should also be removed,
58
+ # the code should be changed to `%i[bar]`.
59
59
  def remove_whitespace
60
- return unless [' ', '('].include?(file_source[@start - 1])
60
+ if ["\n", ';', ')', ']', '}', '|', nil].include?(file_source[@end]) && file_source[@start - 1] == ' '
61
+ @start -= 1
62
+ return
63
+ end
61
64
 
62
- while file_source[@end] == ' '
65
+ if [' ', '(', '[', '{', '|'].include?(file_source[@start - 1]) && file_source[@end] == ' '
63
66
  @end += 1
64
67
  end
65
68
  end
@@ -65,7 +65,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
65
65
  "#{direct_child_name} is not supported for #{get_source(node)}" unless child_node
66
66
  return child_node_range(child_node, nested_child_name) if nested_child_name
67
67
 
68
- return NodeMutation::Range.new(child_node.loc.expression.begin_pos, child_node.loc.expression.end_pos)
68
+ return NodeMutation::Struct::Range.new(child_node.loc.expression.begin_pos, child_node.loc.expression.end_pos)
69
69
  end
70
70
 
71
71
  raise NodeMutation::MethodNotSupported,
@@ -74,36 +74,39 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
74
74
  child_node = node.send(direct_child_name)
75
75
  return child_node_range(child_node, nested_child_name) if nested_child_name
76
76
 
77
- return NodeMutation::Range.new(child_node.loc.expression.begin_pos, child_node.loc.expression.end_pos)
77
+ return NodeMutation::Struct::Range.new(child_node.loc.expression.begin_pos, child_node.loc.expression.end_pos)
78
78
  end
79
79
 
80
80
  case [node.type, child_name.to_sym]
81
81
  when %i[block pipes], %i[def parentheses], %i[defs parentheses]
82
- NodeMutation::Range.new(
82
+ NodeMutation::Struct::Range.new(
83
83
  node.arguments.first.loc.expression.begin_pos - 1,
84
84
  node.arguments.last.loc.expression.end_pos + 1
85
85
  )
86
86
  when %i[block arguments], %i[def arguments], %i[defs arguments]
87
- NodeMutation::Range.new(node.arguments.first.loc.expression.begin_pos, node.arguments.last.loc.expression.end_pos)
87
+ NodeMutation::Struct::Range.new(
88
+ node.arguments.first.loc.expression.begin_pos,
89
+ node.arguments.last.loc.expression.end_pos
90
+ )
88
91
  when %i[class name], %i[const name], %i[def name], %i[defs name]
89
- NodeMutation::Range.new(node.loc.name.begin_pos, node.loc.name.end_pos)
92
+ NodeMutation::Struct::Range.new(node.loc.name.begin_pos, node.loc.name.end_pos)
90
93
  when %i[defs dot]
91
- NodeMutation::Range.new(node.loc.operator.begin_pos, node.loc.operator.end_pos) if node.loc.operator
94
+ NodeMutation::Struct::Range.new(node.loc.operator.begin_pos, node.loc.operator.end_pos) if node.loc.operator
92
95
  when %i[defs self]
93
- NodeMutation::Range.new(node.loc.operator.begin_pos - 'self'.length, node.loc.operator.begin_pos)
96
+ NodeMutation::Struct::Range.new(node.loc.operator.begin_pos - 'self'.length, node.loc.operator.begin_pos)
94
97
  when %i[lvasgn variable], %i[ivasgn variable], %i[cvasgn variable], %i[gvasgn variable]
95
- NodeMutation::Range.new(node.loc.name.begin_pos, node.loc.name.end_pos)
98
+ NodeMutation::Struct::Range.new(node.loc.name.begin_pos, node.loc.name.end_pos)
96
99
  when %i[send dot], %i[csend dot]
97
- NodeMutation::Range.new(node.loc.dot.begin_pos, node.loc.dot.end_pos) if node.loc.dot
100
+ NodeMutation::Struct::Range.new(node.loc.dot.begin_pos, node.loc.dot.end_pos) if node.loc.dot
98
101
  when %i[send message], %i[csend message]
99
102
  if node.loc.operator
100
- NodeMutation::Range.new(node.loc.selector.begin_pos, node.loc.operator.end_pos)
103
+ NodeMutation::Struct::Range.new(node.loc.selector.begin_pos, node.loc.operator.end_pos)
101
104
  else
102
- NodeMutation::Range.new(node.loc.selector.begin_pos, node.loc.selector.end_pos)
105
+ NodeMutation::Struct::Range.new(node.loc.selector.begin_pos, node.loc.selector.end_pos)
103
106
  end
104
107
  when %i[send parentheses], %i[csend parentheses]
105
108
  if node.loc.begin && node.loc.end
106
- NodeMutation::Range.new(node.loc.begin.begin_pos, node.loc.end.end_pos)
109
+ NodeMutation::Struct::Range.new(node.loc.begin.begin_pos, node.loc.end.end_pos)
107
110
  end
108
111
  else
109
112
  if node.type == :hash && child_name.to_s.end_with?('_pair')
@@ -112,7 +115,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
112
115
  "#{direct_child_name} is not supported for #{get_source(node)}" unless pair_node
113
116
  return child_node_range(pair, nested_child_name) if nested_child_name
114
117
 
115
- return NodeMutation::Range.new(pair_node.loc.expression.begin_pos, pair_node.loc.expression.end_pos)
118
+ return NodeMutation::Struct::Range.new(pair_node.loc.expression.begin_pos, pair_node.loc.expression.end_pos)
116
119
  end
117
120
 
118
121
  raise NodeMutation::MethodNotSupported,
@@ -126,7 +129,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
126
129
 
127
130
  if child_node.is_a?(Parser::AST::Node)
128
131
  return(
129
- NodeMutation::Range.new(child_node.loc.expression.begin_pos, child_node.loc.expression.end_pos)
132
+ NodeMutation::Struct::Range.new(child_node.loc.expression.begin_pos, child_node.loc.expression.end_pos)
130
133
  )
131
134
  end
132
135
 
@@ -134,7 +137,10 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
134
137
  return nil if child_node.empty?
135
138
 
136
139
  return(
137
- NodeMutation::Range.new(child_node.first.loc.expression.begin_pos, child_node.last.loc.expression.end_pos)
140
+ NodeMutation::Struct::Range.new(
141
+ child_node.first.loc.expression.begin_pos,
142
+ child_node.last.loc.expression.end_pos
143
+ )
138
144
  )
139
145
  end
140
146
  end
@@ -149,12 +155,12 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
149
155
 
150
156
  def get_start_loc(node)
151
157
  begin_loc = node.loc.expression.begin
152
- NodeMutation::Location.new(begin_loc.line, begin_loc.column)
158
+ NodeMutation::Struct::Location.new(begin_loc.line, begin_loc.column)
153
159
  end
154
160
 
155
161
  def get_end_loc(node)
156
162
  end_loc = node.loc.expression.end
157
- NodeMutation::Location.new(end_loc.line, end_loc.column)
163
+ NodeMutation::Struct::Location.new(end_loc.line, end_loc.column)
158
164
  end
159
165
 
160
166
  def get_indent(node)
@@ -1,41 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation::Result
4
- attr_accessor :file_path
4
+ attr_accessor :file_path, :new_source
5
+ attr_reader :actions
5
6
 
6
- def initialize(options)
7
- @options = options
7
+ def initialize(affected:, conflicted:)
8
+ @affected = affected
9
+ @conflicted = conflicted
10
+ @actions = []
8
11
  end
9
12
 
10
13
  def affected?
11
- @options[:affected]
14
+ @affected
12
15
  end
13
16
 
14
17
  def conflicted?
15
- @options[:conflicted]
18
+ @conflicted
16
19
  end
17
20
 
18
- def actions
19
- @options[:actions]
20
- end
21
-
22
- def new_source
23
- @options[:new_source]
21
+ def actions=(actions)
22
+ @actions = actions.map { |action| NodeMutation::Struct::Action.new(action.start, action.end, action.new_code) }
24
23
  end
25
24
 
26
25
  def to_json(*args)
27
- to_hash.to_json(*args)
28
- end
29
-
30
- def to_hash
31
- hash = { file_path: file_path }
32
- @options.each do |key, value|
33
- if key == :actions
34
- hash[:actions] = value.map { |action| { start: action.start, end: action.end, new_code: action.new_code } }
35
- else
36
- hash[key] = value
37
- end
38
- end
39
- hash
26
+ data = { affected: affected?, conflicted: conflicted? }
27
+ data[:new_source] = new_source if new_source
28
+ data[:actions] = actions unless actions.empty?
29
+ data.to_json(*args)
40
30
  end
41
31
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NodeMutation::Struct
4
+ Action = Struct.new(:start, :end, :new_code)
5
+ Location = Struct.new(:line, :column)
6
+ Range = Struct.new(:start, :end)
7
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.12.0"
4
+ VERSION = "1.12.2"
5
5
  end
data/lib/node_mutation.rb CHANGED
@@ -18,10 +18,9 @@ class NodeMutation
18
18
  autoload :ReplaceWithAction, 'node_mutation/action/replace_with_action'
19
19
  autoload :WrapAction, 'node_mutation/action/wrap_action'
20
20
  autoload :NoopAction, 'node_mutation/action/noop_action'
21
- autoload :Range, 'node_mutation/range'
22
- autoload :Location, 'node_mutation/location'
23
21
  autoload :Result, 'node_mutation/result'
24
22
  autoload :Strategy, 'node_mutation/strategy'
23
+ autoload :Struct, 'node_mutation/struct'
25
24
 
26
25
  attr_reader :actions
27
26
 
@@ -229,7 +228,9 @@ class NodeMutation
229
228
  @actions.reverse_each do |action|
230
229
  source[action.start...action.end] = action.new_code if action.new_code
231
230
  end
232
- NodeMutation::Result.new(affected: true, conflicted: !conflict_actions.empty?, new_source: source)
231
+ result = NodeMutation::Result.new(affected: true, conflicted: !conflict_actions.empty?)
232
+ result.new_source = source
233
+ result
233
234
  end
234
235
 
235
236
  # Test actions and return the actions.
@@ -241,7 +242,7 @@ class NodeMutation
241
242
  # @return {NodeMutation::Result}
242
243
  def test
243
244
  if @actions.length == 0
244
- return NodeMutation::Result.new(affected: false, conflicted: false, actions: [])
245
+ return NodeMutation::Result.new(affected: false, conflicted: false)
245
246
  end
246
247
 
247
248
  @actions.sort_by! { |action| [action.start, action.end] }
@@ -250,7 +251,9 @@ class NodeMutation
250
251
  raise ConflictActionError, "mutation actions are conflicted"
251
252
  end
252
253
 
253
- NodeMutation::Result.new(affected: true, conflicted: !conflict_actions.empty?, actions: @actions)
254
+ result = NodeMutation::Result.new(affected: true, conflicted: !conflict_actions.empty?)
255
+ result.actions = @actions
256
+ result
254
257
  end
255
258
 
256
259
  private
@@ -5,15 +5,15 @@ class NodeMutation::Adapter[T]
5
5
 
6
6
  def file_content: (node: T) -> String
7
7
 
8
- def child_node_range: (node: T, child_name: String) -> NodeMutation::Range
8
+ def child_node_range: (node: T, child_name: String) -> NodeMutation::Struct::Range
9
9
 
10
10
  def get_start: (node: T) -> Integer
11
11
 
12
12
  def get_end: (node: T) -> Integer
13
13
 
14
- def get_start_loc: (node: T) -> NodeMutation::Location
14
+ def get_start_loc: (node: T) -> NodeMutation::Struct::Location
15
15
 
16
- def get_end_loc: (node: T) -> NodeMutation::Location
16
+ def get_end_loc: (node: T) -> NodeMutation::Struct::Location
17
17
 
18
18
  def get_indent: (node: T) -> Integer
19
19
  end
@@ -1,5 +1,7 @@
1
1
  class NodeMutation::Result
2
2
  attr_accessor file_path (): String
3
+ attr_accessor new_source (): String
4
+ attr_accessor actions (): Array[NodeMutation::Struct::Action]
3
5
 
4
6
  def initialize: (source: String) -> NodeMutation::Result
5
7
 
@@ -7,9 +9,5 @@ class NodeMutation::Result
7
9
 
8
10
  def conflicted?: () -> bool
9
11
 
10
- def new_source: () -> String
11
-
12
- def actions: () -> Array[Hash]
13
-
14
12
  def to_json: (*args) -> String
15
13
  end
@@ -0,0 +1,7 @@
1
+ class NodeMutation::Strategy
2
+ KEEP_RUNNING: Integer
3
+
4
+ THROW_ERROR: Integer
5
+
6
+ ALLOW_INSERT_AT_SAME_POSITION: Integer
7
+ end
@@ -0,0 +1,17 @@
1
+ class NodeMutation::Struct
2
+ class Action < ::Struct
3
+ prop :start, Integer
4
+ prop :end, Integer
5
+ prop :new_code, String
6
+ end
7
+
8
+ class Location < ::Struct
9
+ prop :line, Integer
10
+ prop :column, Integer
11
+ end
12
+
13
+ class Range < ::Struct
14
+ prop :start, Integer
15
+ prop :end, Integer
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  module NodeMutation[T]
2
- VERSION: Stringo
2
+ VERSION: String
3
3
 
4
4
  class MethodNotSupported < StandardError
5
5
  end
@@ -7,10 +7,6 @@ module NodeMutation[T]
7
7
  class ConflictActionError < StandardError
8
8
  end
9
9
 
10
- KEEP_RUNNING: Integer
11
-
12
- THROW_ERROR: Integer
13
-
14
10
  attr_reader actions: Array[NodeMutation::Action]
15
11
 
16
12
  def self.configure: (options: { adapter: NodeMutation::Adapter, strategy: Integer, tab_width: Integer }) -> void
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.12.0
4
+ version: 1.12.2
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-23 00:00:00.000000000 Z
11
+ date: 2023-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubis
@@ -51,16 +51,17 @@ files:
51
51
  - lib/node_mutation/action/replace_with_action.rb
52
52
  - lib/node_mutation/action/wrap_action.rb
53
53
  - lib/node_mutation/adapter.rb
54
- - lib/node_mutation/location.rb
55
54
  - lib/node_mutation/parser_adapter.rb
56
- - lib/node_mutation/range.rb
57
55
  - lib/node_mutation/result.rb
58
56
  - lib/node_mutation/strategy.rb
57
+ - lib/node_mutation/struct.rb
59
58
  - lib/node_mutation/version.rb
60
59
  - node_mutation.gemspec
61
60
  - sig/node_mutation.rbs
62
61
  - sig/node_mutation/adapter.rbs
63
62
  - sig/node_mutation/result.rbs
63
+ - sig/node_mutation/strategy.rbs
64
+ - sig/node_mutation/struct.rbs
64
65
  homepage: https://github.com/xinminlabs/node-mutation-ruby
65
66
  licenses: []
66
67
  metadata:
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- NodeMutation::Location = Struct.new(:line, :column)
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- NodeMutation::Range = Struct.new(:start, :end)