node_mutation 1.6.1 → 1.7.0

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: 1a919cd77334f1c935135667972c64670f1513a31c63dde2a71405dcb165077f
4
- data.tar.gz: ce7faaade885a41905b1371b04cb2c94962cd044404f754a6bd1d0537b6c871a
3
+ metadata.gz: eb6a1cb69d0fdb6691831450fae1c126d25ae38495f5b68725b7500708534671
4
+ data.tar.gz: 643999af6c57ea3553668945af32e49074605940c7372c6292272c34271672a7
5
5
  SHA512:
6
- metadata.gz: ee3c31054d31ba1b997e366f3c2ac0fd4d6b3587f9f49e59d12d72a23cb8b8d4b1f12dcfb7f5d72440cbd867b4ed5ef01e19cfcdfbaa11a9c3d4b7db53c047e6
7
- data.tar.gz: 2295e27670dbaa6be22ef320cd3e13cbb69e86fcf2f8824f8be4ada1607deb98fa7d6d1891bb3a4360debd37abf5876f155276ddd7bf45b0aac6667df67920af
6
+ metadata.gz: 8088d0b9d08569fe3653a99c6a19ac7c5701b42a8e2d90668a5d1ea8cff354f5fe2f197ef319825f56ba0eeaef74380999a54fe84169818bc35bcc0263d11688
7
+ data.tar.gz: 55725a4fb619d3679531e4af3bbb098994b472b84b6aedecd5d88e9310959eda1c7ea261c76654b73963eb388cd786523bc1d58e561196b6a75cd024b912fcb4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.7.0 (2022-10-25)
4
+
5
+ * Add a new strategy `ALLOW_INSERT_AT_SAME_POSITION`
6
+
7
+ ## 1.6.2 (2022-10-25)
8
+
9
+ * Mark same position as conflict action
10
+
3
11
  ## 1.6.1 (2022-10-24)
4
12
 
5
13
  * Better error message when node does not respond to a key
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.6.1)
4
+ node_mutation (1.7.0)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
 
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NodeMutation::Strategy
4
+ KEEP_RUNNING = 0b1
5
+ THROW_ERROR = 0b10
6
+ ALLOW_INSERT_AT_SAME_POSITION = 0b100
7
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.6.1"
4
+ VERSION = "1.7.0"
5
5
  end
data/lib/node_mutation.rb CHANGED
@@ -9,9 +9,6 @@ class NodeMutation
9
9
  class MethodNotSupported < StandardError; end
10
10
  class ConflictActionError < StandardError; end
11
11
 
12
- KEEP_RUNNING = 1
13
- THROW_ERROR = 2
14
-
15
12
  autoload :Adapter, "node_mutation/adapter"
16
13
  autoload :ParserAdapter, "node_mutation/parser_adapter"
17
14
  autoload :Action, 'node_mutation/action'
@@ -25,6 +22,7 @@ class NodeMutation
25
22
  autoload :WrapAction, 'node_mutation/action/wrap_action'
26
23
  autoload :NoopAction, 'node_mutation/action/noop_action'
27
24
  autoload :Result, 'node_mutation/result'
25
+ autoload :Strategy, 'node_mutation/strategy'
28
26
 
29
27
  attr_reader :actions
30
28
 
@@ -47,10 +45,10 @@ class NodeMutation
47
45
  end
48
46
 
49
47
  # Get the strategy
50
- # @return [Integer] current strategy, could be {NodeMutation::KEEP_RUNNING} or {NodeMutation::THROW_ERROR},
51
- # by default is {NodeMutation::KEEP_RUNNING}
48
+ # @return [Integer] current strategy, could be {NodeMutation::Strategy::KEEP_RUNNING} or {NodeMutation::Strategy::THROW_ERROR},
49
+ # by default is {NodeMutation::Strategy::KEEP_RUNNING}
52
50
  def self.strategy
53
- @strategy ||= KEEP_RUNNING
51
+ @strategy ||= Strategy::KEEP_RUNNING
54
52
  end
55
53
 
56
54
  # Initialize a NodeMutation.
@@ -214,7 +212,7 @@ class NodeMutation
214
212
  source = +@source
215
213
  @actions.sort_by! { |action| [action.start, action.end] }
216
214
  conflict_actions = get_conflict_actions
217
- if conflict_actions.size > 0 && NodeMutation.strategy == THROW_ERROR
215
+ if conflict_actions.size > 0 && strategy?(Strategy::THROW_ERROR)
218
216
  raise ConflictActionError, "mutation actions are conflicted"
219
217
  end
220
218
  @actions.reverse_each do |action|
@@ -242,7 +240,7 @@ class NodeMutation
242
240
  conflict_actions = []
243
241
  @actions.sort_by! { |action| [action.start, action.end] }
244
242
  conflict_actions = get_conflict_actions
245
- if conflict_actions.size > 0 && NodeMutation.strategy == THROW_ERROR
243
+ if conflict_actions.size > 0 && strategy?(Strategy::THROW_ERROR)
246
244
  raise ConflictActionError, "mutation actions are conflicted"
247
245
  end
248
246
  NodeMutation::Result.new(
@@ -262,19 +260,29 @@ class NodeMutation
262
260
  conflict_actions = []
263
261
  return [] if i < 0
264
262
 
265
- start = @actions[i].start
263
+ begin_pos = @actions[i].start
264
+ end_pos = @actions[i].end
266
265
  while j > -1
267
- if start < @actions[j].end
266
+ # if we have two insert actions at same position.
267
+ same_position = begin_pos == @actions[j].start && begin_pos == end_pos && @actions[j].start == @actions[j].end
268
+ # if we have two actions with overlapped range.
269
+ overlapped_position = begin_pos < @actions[j].end
270
+ if (!strategy?(Strategy::ALLOW_INSERT_AT_SAME_POSITION) && same_position) || overlapped_position
268
271
  conflict_actions << @actions.delete_at(j)
269
272
  else
270
273
  i = j
271
- start = @actions[i].start
274
+ begin_pos = @actions[i].start
275
+ end_pos = @actions[i].end
272
276
  end
273
277
  j -= 1
274
278
  end
275
279
  conflict_actions
276
280
  end
277
281
 
282
+ def strategy?(strategy)
283
+ NodeMutation.strategy & strategy == strategy
284
+ end
285
+
278
286
  def format_actions(actions)
279
287
  actions.map { |action| OpenStruct.new(start: action.start, end: action.end, new_code: action.new_code ) }
280
288
  end
@@ -13,11 +13,11 @@ module NodeMutation[T]
13
13
 
14
14
  attr_reader actions: Array[NodeMutation::Action]
15
15
 
16
- def self.configure: (options: { adapter: NodeMutation::Adapter, strategry: Integer }) -> void
16
+ def self.configure: (options: { adapter: NodeMutation::Adapter, strategy: Integer }) -> void
17
17
 
18
18
  def self.adapter: () -> NodeMutation::Adapter
19
19
 
20
- def self.strategry: () -> Integer
20
+ def self.strategy: () -> Integer
21
21
 
22
22
  def initialize: (source: String) -> NodeMutation
23
23
 
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.6.1
4
+ version: 1.7.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: 2022-10-23 00:00:00.000000000 Z
11
+ date: 2022-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -69,6 +69,7 @@ files:
69
69
  - lib/node_mutation/engine/erb.rb
70
70
  - lib/node_mutation/parser_adapter.rb
71
71
  - lib/node_mutation/result.rb
72
+ - lib/node_mutation/strategy.rb
72
73
  - lib/node_mutation/version.rb
73
74
  - node_mutation.gemspec
74
75
  - sig/node_mutation.rbs