node_mutation 1.2.1 → 1.3.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: 16d1f6952b16d815e9cee50df011c99075ffd17bc17531efc8b6a99c39909d4a
4
- data.tar.gz: 3f558c1b9e171310775fec6ccdb3a1fe0321aa0ce94b23ec8c861e3ae496dd31
3
+ metadata.gz: 9fc988a54ceb1a8c68ac77bde94852da2cda3f1101b965462e246cbfac992fb5
4
+ data.tar.gz: 0b22656df417e7d26390cc43f4de427dc6fb21bceedcc5904385465bb9006300
5
5
  SHA512:
6
- metadata.gz: 7221a70883e380a9f88dda96db69890349dde212580f63fac75d02a71bb558587d4ccf7e6b33547b0b44846ee493fd7ed924e13eb7cc414b27979c3fb354876e
7
- data.tar.gz: 7d1cc7ab27508b202589d22bd6affc07345d19ad12701b7a3aea2d9bc06125a43e17870f787a539a16faaf1ed7db14a13c28b4b8a46d0afaa3b1d2fe701c4ef8
6
+ metadata.gz: bb90cac28bce6d02bd74687e2e4003f5864b91be42d443f9de9df464f7cbdf65b4cdc758a9deeefa14a8448b0a88200501bc9e9119872380223ff9dc3b6f5b56
7
+ data.tar.gz: 566fe2e70aebcbf33e37ac499ccb2a90b0614f6bf695b197096dde6a25cd0b63c3d146de25b274c922f90c8bb8fb7b5143c5fdf8836e7f53877f75f54b4f95d2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.3.1 (2022-09-15)
4
+
5
+ * Add `NodeMutation::Result.to_hash`
6
+
7
+ ## 1.3.0 (2022-09-15)
8
+
9
+ * Add `NodeMutation#test`
10
+
3
11
  ## 1.2.1 (2022-07-22)
4
12
 
5
13
  * Fix `child_node_range` for const name
data/Gemfile CHANGED
@@ -13,4 +13,3 @@ gem "parser"
13
13
  gem "parser_node_ext"
14
14
  gem "guard"
15
15
  gem "guard-rspec"
16
- gem "pp"
data/Gemfile.lock CHANGED
@@ -1,18 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.2.1)
5
- activesupport
4
+ node_mutation (1.3.1)
5
+ activesupport (< 7.0.0)
6
6
  erubis
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activesupport (7.0.3.1)
11
+ activesupport (6.1.7)
12
12
  concurrent-ruby (~> 1.0, >= 1.0.2)
13
13
  i18n (>= 1.6, < 2)
14
14
  minitest (>= 5.1)
15
15
  tzinfo (~> 2.0)
16
+ zeitwerk (~> 2.3)
16
17
  ast (2.4.2)
17
18
  coderay (1.1.3)
18
19
  concurrent-ruby (1.1.10)
@@ -41,7 +42,7 @@ GEM
41
42
  rb-inotify (~> 0.9, >= 0.9.10)
42
43
  lumberjack (1.2.8)
43
44
  method_source (1.0.0)
44
- minitest (5.16.2)
45
+ minitest (5.16.3)
45
46
  nenv (0.3.0)
46
47
  notiffany (0.1.3)
47
48
  nenv (~> 0.1)
@@ -50,9 +51,6 @@ GEM
50
51
  ast (~> 2.4.1)
51
52
  parser_node_ext (0.4.0)
52
53
  parser
53
- pp (0.3.0)
54
- prettyprint
55
- prettyprint (0.1.1)
56
54
  pry (0.14.1)
57
55
  coderay (~> 1.1)
58
56
  method_source (~> 1.0)
@@ -77,6 +75,7 @@ GEM
77
75
  thor (1.2.1)
78
76
  tzinfo (2.0.5)
79
77
  concurrent-ruby (~> 1.0)
78
+ zeitwerk (2.6.0)
80
79
 
81
80
  PLATFORMS
82
81
  x86_64-darwin-21
@@ -88,7 +87,6 @@ DEPENDENCIES
88
87
  node_mutation!
89
88
  parser
90
89
  parser_node_ext
91
- pp
92
90
  rake (~> 13.0)
93
91
  rspec (~> 3.0)
94
92
 
@@ -13,7 +13,17 @@ class NodeMutation::Result
13
13
  @options[:conflicted]
14
14
  end
15
15
 
16
+ def actions
17
+ @options[:actions]
18
+ end
19
+
16
20
  def new_source
17
21
  @options[:new_source]
18
22
  end
23
+
24
+ def to_hash
25
+ @options.each_pair.with_object({}) do |(key, value), hash|
26
+ hash[key] = value.is_a?(Array) ? value.map { |action| action.to_h } : value
27
+ end
28
+ end
19
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.2.1"
4
+ VERSION = "1.3.1"
5
5
  end
data/lib/node_mutation.rb CHANGED
@@ -207,18 +207,16 @@ class NodeMutation
207
207
  @actions << WrapAction.new(node, with: with).process
208
208
  end
209
209
 
210
- # Read the source code from file path,
211
- # rewrite the source code based on all actions,
212
- # then write the new source code back to the file.
210
+ # Process actions and return the new source.
213
211
  #
214
212
  # If there's an action range conflict,
215
213
  # it will raise a ConflictActionError if strategy is set to THROW_ERROR,
216
214
  # it will process all non conflicted actions and return `{ conflict: true }`
217
215
  # if strategy is set to KEEP_RUNNING.
218
- # @return {{conflict: Boolean}} if actions are conflicted
216
+ # @return {NodeMutation::Result}
219
217
  def process
220
218
  if @actions.length == 0
221
- return NodeMutation::Result.new(affected: false)
219
+ return NodeMutation::Result.new(affected: false, conflicted: false)
222
220
  end
223
221
 
224
222
  conflict_actions = []
@@ -238,6 +236,31 @@ class NodeMutation
238
236
  )
239
237
  end
240
238
 
239
+ # Test actions and return the actions.
240
+ #
241
+ # If there's an action range conflict,
242
+ # it will raise a ConflictActionError if strategy is set to THROW_ERROR,
243
+ # it will process all non conflicted actions and return `{ conflict: true }`
244
+ # if strategy is set to KEEP_RUNNING.
245
+ # @return {NodeMutation::Result}
246
+ def test
247
+ if @actions.length == 0
248
+ return NodeMutation::Result.new(affected: false, conflicted: false, actions: [])
249
+ end
250
+
251
+ conflict_actions = []
252
+ @actions.sort_by! { |action| [action.start, action.end] }
253
+ conflict_actions = get_conflict_actions
254
+ if conflict_actions.size > 0 && NodeMutation.strategy == THROW_ERROR
255
+ raise ConflictActionError, "mutation actions are conflicted"
256
+ end
257
+ NodeMutation::Result.new(
258
+ affected: true,
259
+ conflicted: !conflict_actions.empty?,
260
+ actions: @actions
261
+ )
262
+ end
263
+
241
264
  private
242
265
 
243
266
  # It changes source code from bottom to top, and it can change source code twice at the same time,
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.require_paths = ["lib"]
30
30
 
31
31
  # Uncomment to register a new dependency of your gem
32
- spec.add_dependency "activesupport"
32
+ spec.add_dependency "activesupport", "< 7.0.0"
33
33
  spec.add_dependency "erubis"
34
34
 
35
35
  # For more information and examples about making a new gem, check out our
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: node_mutation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.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-07-22 00:00:00.000000000 Z
11
+ date: 2022-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "<"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 7.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "<"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 7.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: erubis
29
29
  requirement: !ruby/object:Gem::Requirement