parser_node_ext 0.1.0 → 0.2.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: c0d6b0cc2e9e72c692cbc566acb7ef8cd180f428b5f473ce82457e26bdffc661
4
- data.tar.gz: 37de56dc2293bb387ca423291bf7a6fa84be97ebf21c1652aba095b76cc9817d
3
+ metadata.gz: b640f12cdf44e564f22af212354d86e2e76329cb796f68f843ba1ff151bfe1cc
4
+ data.tar.gz: 17cfcb04b5f079946160fffb800cd919654cf1a837d48629b7102e8941c88dfb
5
5
  SHA512:
6
- metadata.gz: 998948687de1e0555ae1f8d2e69452c382a4320d69f5106d75ba6855a5ef0b3718bf3a28acf36a3f1a1f0e4fc86f19ed7f5d8c9e482719ddf32d26fdf3887b82
7
- data.tar.gz: f339dfc3553d68ddffa96ffd4903832346cc1d29cb679eb1ec21dbe94a64551af9535de1f0d1548b90ef8e49df017cf8efa3a2f0503268cb4a9dcbdc49ed109a
6
+ metadata.gz: 76d8308f1debc8b852d7a6d2bb3f065b6266dbd8a46d844f30135bc0f39ea755afcd7e3685ad25b85fc999bbe497daad1edfa35e26dc9961ba68d92da3d8b1ca
7
+ data.tar.gz: 1f1d833611f71a5c1c18e58444d799c5e3d491a50dea28495bf14113c47d11014aa7d955f3164a42d89b5c994dee3e911206d1e193e3e99be0d41140ce91e23c
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.2.0 (2022-06-27)
4
+
5
+ * Add `Node#to_value`
6
+ * Add `Node#to_source`
7
+ * Support `xxx_value` and `xxx_source` for `hash` node
8
+
9
+ ## 0.1.0 (2022-06-26)
10
+
11
+ * Abstract from synvert-core
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- parser_node_ext (0.1.0)
4
+ parser_node_ext (0.2.0)
5
5
  parser
6
6
 
7
7
  GEM
@@ -28,6 +28,7 @@ GEM
28
28
 
29
29
  PLATFORMS
30
30
  x86_64-darwin-21
31
+ x86_64-linux
31
32
 
32
33
  DEPENDENCIES
33
34
  parser_node_ext!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ParserNodeExt
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -203,6 +203,113 @@ module ParserNodeExt
203
203
  raise Synvert::Core::MethodNotSupported, "keys is not handled for #{debug_info}"
204
204
  end
205
205
  end
206
+
207
+ # Check if :hash node contains specified key.
208
+ # @example
209
+ # node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
210
+ # node.key?(:foo) # true
211
+ # @param [Symbol, String] key value.
212
+ # @return [Boolean] true if specified key exists.
213
+ # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
214
+ def key?(key)
215
+ if :hash == type
216
+ children.any? { |pair_node| pair_node.key.to_value == key }
217
+ else
218
+ raise Synvert::Core::MethodNotSupported, "key? is not handled for #{debug_info}"
219
+ end
220
+ end
221
+
222
+ # Get :hash value node according to specified key.
223
+ # @example
224
+ # node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
225
+ # node.hash_value(:foo) # s(:sym, :bar)
226
+ # @param [Symbol, String] key value.
227
+ # @return [Parser::AST::Node] hash value of node.
228
+ # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
229
+ def hash_value(key)
230
+ if :hash == type
231
+ value_node = children.find { |pair_node| pair_node.key.to_value == key }
232
+ value_node&.value
233
+ else
234
+ raise Synvert::Core::MethodNotSupported, "hash_value is not handled for #{debug_info}"
235
+ end
236
+ end
237
+
238
+ # Return the exact value of node.
239
+ # It supports :array, :begin, :erange, :false, :float, :irange, :int, :str, :sym and :true nodes.
240
+ # @example
241
+ # node # s(:array, s(:str, "str"), s(:sym, :str))
242
+ # node.to_value # ['str', :str]
243
+ # @return [Object] exact value.
244
+ # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
245
+ def to_value
246
+ case type
247
+ when :int, :float, :str, :sym
248
+ children.last
249
+ when :true
250
+ true
251
+ when :false
252
+ false
253
+ when :nil
254
+ nil
255
+ when :array
256
+ children.map(&:to_value)
257
+ when :irange
258
+ (children.first.to_value..children.last.to_value)
259
+ when :erange
260
+ (children.first.to_value...children.last.to_value)
261
+ when :begin
262
+ children.first.to_value
263
+ else
264
+ self
265
+ end
266
+ end
267
+
268
+ # Get the source code of node.
269
+ #
270
+ # @return [String] source code.
271
+ def to_source
272
+ loc.expression&.source
273
+ end
274
+
275
+ # Respond key value and source for hash node, e.g.
276
+ # @example
277
+ # node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
278
+ # node.foo_value # :bar
279
+ # node.foo_source # ":bar"
280
+ def method_missing(method_name, *args, &block)
281
+ if :args == type && children.respond_to?(method_name)
282
+ return children.send(method_name, *args, &block)
283
+ elsif :hash == type && method_name.to_s.include?('_value')
284
+ key = method_name.to_s.sub('_value', '')
285
+ return hash_value(key.to_sym)&.to_value if key?(key.to_sym)
286
+ return hash_value(key.to_s)&.to_value if key?(key.to_s)
287
+
288
+ return nil
289
+ elsif :hash == type && method_name.to_s.include?('_source')
290
+ key = method_name.to_s.sub('_source', '')
291
+ return hash_value(key.to_sym)&.to_source if key?(key.to_sym)
292
+ return hash_value(key.to_s)&.to_source if key?(key.to_s)
293
+
294
+ return nil
295
+ end
296
+
297
+ super
298
+ end
299
+
300
+ def respond_to_missing?(method_name, *args)
301
+ if :args == type && children.respond_to?(method_name)
302
+ return true
303
+ elsif :hash == type && method_name.to_s.include?('_value')
304
+ key = method_name.to_s.sub('_value', '')
305
+ return true if key?(key.to_sym) || key?(key.to_s)
306
+ elsif :hash == type && method_name.to_s.include?('_source')
307
+ key = method_name.to_s.sub('_source', '')
308
+ return true if key?(key.to_sym) || key?(key.to_s)
309
+ end
310
+
311
+ super
312
+ end
206
313
  end
207
314
  end
208
315
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parser_node_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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-06-26 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -33,6 +33,7 @@ extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
35
  - ".rspec"
36
+ - CHANGELOG.md
36
37
  - Gemfile
37
38
  - Gemfile.lock
38
39
  - README.md