node_mutation 1.2.0 → 1.3.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: c27aac4f89bdddee8f7298469d2f26ad17f38c8ed6eb4e76bf4d7af0ea729f98
4
- data.tar.gz: da1d0b9a9b830890583d88b0b74cca800eb991c4eb86e9b232eabd75678538b0
3
+ metadata.gz: e39eb120d51bf9d1839013e1e0b408c1d9f8eefc2eb2767e594f83e01171d323
4
+ data.tar.gz: 63688a3c83e512004a010d29a373c91f8b542fe8ce1f8ccd16c89ebd8b059103
5
5
  SHA512:
6
- metadata.gz: 12d58838fec06d87e0ffad8fce9a1879221726fa57cf15e0e3c4d08541d7a7ef0fff0249601814edc9bad95e73cc5eac46a9f91a55816fee8c3773b0e397b74e
7
- data.tar.gz: '02870efef507899a92a021a8532c175c0ba230815f76eba8391421b97cd628697a035b31bd17110564aa1c273f78db795a7f1a9d775742917abce56ee703efee'
6
+ metadata.gz: b1089cb5497135200f0b935fb2c4a3c2537581b5c53bedd8c2ca9392fc6a989de764f116f90040628b4f06f24a8cf29998d1ad7330a37fe4d29026b71d558c2e
7
+ data.tar.gz: dfa661d9080738bf60f486ea05eba4f7fea7bb8320d7a76953ccdb00498d71f6c8070917ff2c0169aa351e9276f7317f1cbd7044c5e3039e88b12feabc0f53cd
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.3.0 (2022-09-15)
4
+
5
+ * Add `NodeMutation#test`
6
+
7
+ ## 1.2.1 (2022-07-22)
8
+
9
+ * Fix `child_node_range` for const name
10
+ * Update `parser_node_ext` to 0.4.0
11
+
3
12
  ## 1.2.0 (2022-07-02)
4
13
 
5
14
  * Return new source instead of writing file
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.0)
5
- activesupport
4
+ node_mutation (1.3.0)
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)
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)
@@ -34,25 +35,22 @@ GEM
34
35
  guard (~> 2.1)
35
36
  guard-compat (~> 1.1)
36
37
  rspec (>= 2.99.0, < 4.0)
37
- i18n (1.10.0)
38
+ i18n (1.12.0)
38
39
  concurrent-ruby (~> 1.0)
39
40
  listen (3.7.1)
40
41
  rb-fsevent (~> 0.10, >= 0.10.3)
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.1)
45
+ minitest (5.16.3)
45
46
  nenv (0.3.0)
46
47
  notiffany (0.1.3)
47
48
  nenv (~> 0.1)
48
49
  shellany (~> 0.0)
49
50
  parser (3.1.2.0)
50
51
  ast (~> 2.4.1)
51
- parser_node_ext (0.2.0)
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)
@@ -75,8 +73,9 @@ GEM
75
73
  rspec-support (3.11.0)
76
74
  shellany (0.0.1)
77
75
  thor (1.2.1)
78
- tzinfo (2.0.4)
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
 
@@ -77,7 +77,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
77
77
  start: node.arguments.first.loc.expression.begin_pos,
78
78
  end: node.arguments.last.loc.expression.end_pos
79
79
  )
80
- when %i[class name], %i[def name], %i[defs name]
80
+ when %i[class name], %i[const name], %i[def name], %i[defs name]
81
81
  OpenStruct.new(start: node.loc.name.begin_pos, end: node.loc.name.end_pos)
82
82
  when %i[defs dot]
83
83
  OpenStruct.new(start: node.loc.operator.begin_pos, end: node.loc.operator.end_pos) if node.loc.operator
@@ -171,4 +171,4 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
171
171
  return child_node
172
172
  end
173
173
  end
174
- end
174
+ end
@@ -13,6 +13,10 @@ 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
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.0
4
+ version: 1.3.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-07-02 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