node_mutation 1.6.2 → 1.7.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: 7b2edcdadf3b7283b4446c8b0da41cb72daa4da3c180042b9e896973a8132cba
4
- data.tar.gz: 3fc7cfe62f0e7ea6699b1d3bb9a918f14d1d451bd045b5d82cf09237db3a5c5e
3
+ metadata.gz: eb6a1cb69d0fdb6691831450fae1c126d25ae38495f5b68725b7500708534671
4
+ data.tar.gz: 643999af6c57ea3553668945af32e49074605940c7372c6292272c34271672a7
5
5
  SHA512:
6
- metadata.gz: 7d04f31b6f9983e66555cfa640c52af13546cb066468b657db99f6dffcff5d1513a26a6f3bcb58c433b6a700e8db544e638807e9e3ccd30578a979fec0cc58e4
7
- data.tar.gz: a35fb59ca28655346782ea46410b34784c965f06b00f66ba6b70a5647695a20e5cfcdf4010a32cf7f4fbf9c44ca3d1e4a37db1860a46f16db1ee8149994bb05c
6
+ metadata.gz: 8088d0b9d08569fe3653a99c6a19ac7c5701b42a8e2d90668a5d1ea8cff354f5fe2f197ef319825f56ba0eeaef74380999a54fe84169818bc35bcc0263d11688
7
+ data.tar.gz: 55725a4fb619d3679531e4af3bbb098994b472b84b6aedecd5d88e9310959eda1c7ea261c76654b73963eb388cd786523bc1d58e561196b6a75cd024b912fcb4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.7.0 (2022-10-25)
4
+
5
+ * Add a new strategy `ALLOW_INSERT_AT_SAME_POSITION`
6
+
3
7
  ## 1.6.2 (2022-10-25)
4
8
 
5
9
  * Mark same position as conflict action
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.6.2)
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.2"
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(
@@ -269,7 +267,7 @@ class NodeMutation
269
267
  same_position = begin_pos == @actions[j].start && begin_pos == end_pos && @actions[j].start == @actions[j].end
270
268
  # if we have two actions with overlapped range.
271
269
  overlapped_position = begin_pos < @actions[j].end
272
- if same_position || overlapped_position
270
+ if (!strategy?(Strategy::ALLOW_INSERT_AT_SAME_POSITION) && same_position) || overlapped_position
273
271
  conflict_actions << @actions.delete_at(j)
274
272
  else
275
273
  i = j
@@ -281,6 +279,10 @@ class NodeMutation
281
279
  conflict_actions
282
280
  end
283
281
 
282
+ def strategy?(strategy)
283
+ NodeMutation.strategy & strategy == strategy
284
+ end
285
+
284
286
  def format_actions(actions)
285
287
  actions.map { |action| OpenStruct.new(start: action.start, end: action.end, new_code: action.new_code ) }
286
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: node_mutation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -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