parser_node_ext 1.0.0 → 1.1.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: 9650ac2c4cec4440ffc72ee28d8904f0583c6d9dfef3890060f5b52adeb28553
4
- data.tar.gz: e3b0dc597dfeca91c977646ced5398673483de2161311bb3f7b9077d29802d9e
3
+ metadata.gz: 9c326faea53723b13d4a991011fb260e874a0d86788308c4c4a919ede8d5680b
4
+ data.tar.gz: 1892e4c21ec1e1c8d4f2fc8c8b4cc78f502a8d20bcf4ee3cd823aa717415122e
5
5
  SHA512:
6
- metadata.gz: 9bd6453cfbe47d33bd9326368d6ff8741178980aaa15e34ed9f5cccbfbd900fa70b007b825802ee36de49ba324ac22d13f38138f4639653d236af4b7347138c3
7
- data.tar.gz: 8c477d4181a64b667a317c8d12924bd504604465f32381a9265f9604c4759b73b2fbffff70c8db5de9cabc27a93f54c5a94d212daa9d79cc3bf9f66b4784eda9
6
+ metadata.gz: 810a25e6c68cfc13791501a5bf1d7a3264a31a90b4af25ba4d109373bd18240fa88c577a194540ebb7835da756e0f1ad584ad19d6318165e802c68e49167b6eb
7
+ data.tar.gz: cba51e45acbf600057a22ac27fd21fe1a980fc6d7b8278312815c4b8c87dd89ab7b9b2b67af81994078f4dd4cf8f34e0755ef59357ee090cc0852a87740b1b26
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.1.0 (2023-05-15)
4
+
5
+ * hash `xxx_value` returns the node rather than the value
6
+ * Support `xxx_pair` for `hash` node
7
+
3
8
  ## 1.0.0 (2023-02-12)
4
9
 
5
10
  * Support almost all of nodes
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- parser_node_ext (1.0.0)
4
+ parser_node_ext (1.1.0)
5
5
  parser
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ GEM
9
9
  specs:
10
10
  ast (2.4.2)
11
11
  diff-lcs (1.5.0)
12
- parser (3.2.1.0)
12
+ parser (3.2.2.1)
13
13
  ast (~> 2.4.1)
14
14
  rake (13.0.6)
15
15
  rspec (3.11.0)
data/README.md CHANGED
@@ -1,8 +1,15 @@
1
1
  # ParserNodeExt
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/parser_node_ext`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ It assigns names to the child nodes of the [parser](https://rubygems.org/gems/parser).
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ It also adds some helpers
6
+
7
+ ```ruby
8
+ # node is a hash node
9
+ node.foo_pair # get the pair node of hash foo key
10
+ node.foo_value # get the value node of the hash foo key
11
+ node.foo_source # get the source of the value node of the hash foo key
12
+ ```
6
13
 
7
14
  ## Installation
8
15
 
@@ -22,7 +29,10 @@ Or install it yourself as:
22
29
 
23
30
  ## Usage
24
31
 
25
- TODO: Write usage instructions here
32
+ ```ruby
33
+ require 'parser/current'
34
+ require 'parser_node_ext'
35
+ ```
26
36
 
27
37
  ## Development
28
38
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ParserNodeExt
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -367,12 +367,27 @@ module ParserNodeExt
367
367
  end
368
368
  end
369
369
 
370
+ # Get :hash pair node according to specified key.
371
+ # @example
372
+ # node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
373
+ # node.hash_pair(:foo) # s(:pair, s(:sym, :foo), s(:sym, :bar))
374
+ # @param [Symbol, String] key value.
375
+ # @return [Parser::AST::Node] hash pair node.
376
+ # @raise [MethodNotSupported] if calls on other node.
377
+ def hash_pair(key)
378
+ if :hash == type
379
+ children.find { |pair_node| pair_node.key.to_value == key }
380
+ else
381
+ raise MethodNotSupported, "hash_pair is not supported for #{self}"
382
+ end
383
+ end
384
+
370
385
  # Get :hash value node according to specified key.
371
386
  # @example
372
387
  # node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
373
388
  # node.hash_value(:foo) # s(:sym, :bar)
374
389
  # @param [Symbol, String] key value.
375
- # @return [Parser::AST::Node] hash value of node.
390
+ # @return [Parser::AST::Node] hash value node.
376
391
  # @raise [MethodNotSupported] if calls on other node.
377
392
  def hash_value(key)
378
393
  if :hash == type
@@ -450,18 +465,24 @@ module ParserNodeExt
450
465
  def method_missing(method_name, *args, &block)
451
466
  if :args == type && children.respond_to?(method_name)
452
467
  return children.send(method_name, *args, &block)
453
- elsif :hash == type && method_name.to_s.include?('_value')
454
- key = method_name.to_s.sub('_value', '')
455
- return hash_value(key.to_sym)&.to_value if key?(key.to_sym)
456
- return hash_value(key.to_s)&.to_value if key?(key.to_s)
468
+ elsif :hash == type && method_name.to_s.end_with?('_pair')
469
+ key = method_name.to_s[0..-6]
470
+ return hash_pair(key.to_sym) if key?(key.to_sym)
471
+ return hash_pair(key.to_s) if key?(key.to_s)
457
472
 
458
473
  return nil
459
- elsif :hash == type && method_name.to_s.include?('_source')
460
- key = method_name.to_s.sub('_source', '')
474
+ elsif :hash == type && method_name.to_s.end_with?('_value')
475
+ key = method_name.to_s[0..-7]
476
+ return hash_value(key.to_sym) if key?(key.to_sym)
477
+ return hash_value(key.to_s) if key?(key.to_s)
478
+
479
+ return nil
480
+ elsif :hash == type && method_name.to_s.end_with?('_source')
481
+ key = method_name.to_s[0..-8]
461
482
  return hash_value(key.to_sym)&.to_source if key?(key.to_sym)
462
483
  return hash_value(key.to_s)&.to_source if key?(key.to_s)
463
484
 
464
- return nil
485
+ return ''
465
486
  end
466
487
 
467
488
  super
@@ -470,12 +491,15 @@ module ParserNodeExt
470
491
  def respond_to_missing?(method_name, *args)
471
492
  if :args == type && children.respond_to?(method_name)
472
493
  return true
473
- elsif :hash == type && method_name.to_s.include?('_value')
474
- key = method_name.to_s.sub('_value', '')
475
- return true if key?(key.to_sym) || key?(key.to_s)
476
- elsif :hash == type && method_name.to_s.include?('_source')
477
- key = method_name.to_s.sub('_source', '')
478
- return true if key?(key.to_sym) || key?(key.to_s)
494
+ elsif :hash == type && method_name.to_s.end_with?('_pair')
495
+ key = method_name.to_s[0..-6]
496
+ return key?(key.to_sym) || key?(key.to_s)
497
+ elsif :hash == type && method_name.to_s.end_with?('_value')
498
+ key = method_name.to_s[0..-7]
499
+ return key?(key.to_sym) || key?(key.to_s)
500
+ elsif :hash == type && method_name.to_s.end_with?('_source')
501
+ key = method_name.to_s[0..-8]
502
+ return key?(key.to_sym) || key?(key.to_s)
479
503
  end
480
504
 
481
505
  super
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: 1.0.0
4
+ version: 1.1.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: 2023-02-12 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  requirements: []
66
- rubygems_version: 3.4.1
66
+ rubygems_version: 3.4.10
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: extend parser node