node_mutation 1.6.2 → 1.7.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: 7b2edcdadf3b7283b4446c8b0da41cb72daa4da3c180042b9e896973a8132cba
4
- data.tar.gz: 3fc7cfe62f0e7ea6699b1d3bb9a918f14d1d451bd045b5d82cf09237db3a5c5e
3
+ metadata.gz: 9dffabe82f3653964c889ce43ccf443348973e9868c1c7565327ae253d0be08d
4
+ data.tar.gz: 1b72a6361aea2795c06881754bb8822caf069d8f0b54e9f9e0e8685f70de4b21
5
5
  SHA512:
6
- metadata.gz: 7d04f31b6f9983e66555cfa640c52af13546cb066468b657db99f6dffcff5d1513a26a6f3bcb58c433b6a700e8db544e638807e9e3ccd30578a979fec0cc58e4
7
- data.tar.gz: a35fb59ca28655346782ea46410b34784c965f06b00f66ba6b70a5647695a20e5cfcdf4010a32cf7f4fbf9c44ca3d1e4a37db1860a46f16db1ee8149994bb05c
6
+ metadata.gz: bc5969cdfdf5654fe4c6886b5d177e356d2072b1369bd0eb7c75b59d60d3388b1dfaf69a435e13e5d1e46006ec9368e1aedeca4b82af29e61bb1525d19a2c428
7
+ data.tar.gz: 6db8711936e7f6772bf44afefda33c8969ea9ffdd3db05a80fcc414958efb2b22b24ce373d1c45950f8118b13de72fdc091b6b2d6213d3912ab7a34704078de1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.7.1 (2022-10-26)
4
+
5
+ * Return empty string for `NilClass` in `ParserAdapter#rewritten_source`
6
+
7
+ ## 1.7.0 (2022-10-25)
8
+
9
+ * Add a new strategy `ALLOW_INSERT_AT_SAME_POSITION`
10
+
3
11
  ## 1.6.2 (2022-10-25)
4
12
 
5
13
  * 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.1)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
 
@@ -41,6 +41,8 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
41
41
  end
42
42
  when String, Symbol, Integer, Float
43
43
  evaluated
44
+ when NilClass
45
+ ''
44
46
  else
45
47
  raise "can not parse \"#{code}\""
46
48
  end
@@ -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.1"
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,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.2
4
+ version: 1.7.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: 2022-10-25 00:00:00.000000000 Z
11
+ date: 2022-10-26 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