parser_node_ext 1.0.0 → 1.1.1

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: fac844753c169838574a48c56d9ea740d7de80bbe80e2d5edf8dc199c59ea512
4
+ data.tar.gz: 0caa0e5dba4f6769ad2d01ed011a01c2c7266d29adf102508482f185421fdef4
5
5
  SHA512:
6
- metadata.gz: 9bd6453cfbe47d33bd9326368d6ff8741178980aaa15e34ed9f5cccbfbd900fa70b007b825802ee36de49ba324ac22d13f38138f4639653d236af4b7347138c3
7
- data.tar.gz: 8c477d4181a64b667a317c8d12924bd504604465f32381a9265f9604c4759b73b2fbffff70c8db5de9cabc27a93f54c5a94d212daa9d79cc3bf9f66b4784eda9
6
+ metadata.gz: af230faaea69ad75bd6c4afddc4b0cce04498fb173c260f175164e954377ee7198721cb19ddf3eba764ba9da12b42b6b5764dd2b687342f277d7bff37b8f1444
7
+ data.tar.gz: 989b25efea51c0b4d732e9db822e1450c6b67e7f079af6de96fe71849f48141c894980e7d402eeca56a6014bb227d418e9cccde92adc2385a7f8949b398d2059
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.1.1 (2023-05-19)
4
+
5
+ * Remove rbs
6
+
7
+ ## 1.1.0 (2023-05-15)
8
+
9
+ * hash `xxx_value` returns the node rather than the value
10
+ * Support `xxx_pair` for `hash` node
11
+
3
12
  ## 1.0.0 (2023-02-12)
4
13
 
5
14
  * 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.1)
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.1"
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.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: 2023-02-12 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -41,7 +41,6 @@ files:
41
41
  - lib/parser_node_ext.rb
42
42
  - lib/parser_node_ext/version.rb
43
43
  - parser_node_ext.gemspec
44
- - sig/parser_node_ext.rbs
45
44
  homepage: https://github.com/xinminlabs/parser_node_ext
46
45
  licenses: []
47
46
  metadata:
@@ -63,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
62
  - !ruby/object:Gem::Version
64
63
  version: '0'
65
64
  requirements: []
66
- rubygems_version: 3.4.1
65
+ rubygems_version: 3.4.10
67
66
  signing_key:
68
67
  specification_version: 4
69
68
  summary: extend parser node
@@ -1,38 +0,0 @@
1
- module ParserNodeExt
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
-
5
- def arguments: () -> Array[Parser::AST::Node]
6
- def arguments_count: () -> Integer
7
- def begin: () -> Parser::AST::Node
8
- def body: () -> Array[Parser::AST::Node]
9
- def caller: () -> Parser::AST::Node
10
- def elements: () -> Array[Parser::AST::Node]
11
- def else_statement: () -> Parser::AST::Node
12
- def exceptions: () -> Array[Parser::AST::Node]
13
- def end: () -> Parser::AST::Node
14
- def ensure_body: () -> Array[Parser::AST::Node]
15
- def expression: () -> Parser::AST::Node
16
- def old_name: () -> Symbol
17
- def options: () -> Parser::AST::Node
18
- def operator: () -> Symbol
19
- def if_statement: () -> Parser::AST::Node
20
- def in_statements: () -> Array[Parser::AST::Node]
21
- def key: () -> Parser::AST::Node
22
- def guard: () -> Parser::AST::Node
23
- def left_value: () -> Parser::AST::Node | Symbol
24
- def message: () -> Symbol
25
- def name: () -> Parser::AST::Node | Symbol
26
- def new_name: () -> Symbol
27
- def pairs: () -> Array[Parser::AST::Node]
28
- def parent_class: () -> Parser::AST::Node
29
- def receiver: () -> Parser::AST::Node
30
- def rescue_bodies: () -> Array[Parser::AST::Node]
31
- def right_value: () -> Parser::AST::Node
32
- def self: () -> Parser::AST::Node
33
- def value: () -> Parser::AST::Node
34
- def variable: () -> Parser::AST::Node
35
- def when_statements: () -> Array[Parser::AST::Node]
36
-
37
- def to_hash: () -> Hash
38
- end