parser_node_ext 0.5.1 → 0.6.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: fc6fc830978807e48ef5cc142cf9dfa32829e835020e31d09252f29d96d13e42
4
- data.tar.gz: 3e44eee2fb5e4dafdf6c2070c49d934cee5d0dc5e1c9d56e7146d37628bed239
3
+ metadata.gz: caf4a4308e04940f7b74d41db9bdf1ae06a15f9b4a63f565f4b4731c89d11693
4
+ data.tar.gz: edcb3bc6b1d55dfec2b17b5e97e1fe5bd86b83f09e5cb4a41e2ea9c0f0c5b4cb
5
5
  SHA512:
6
- metadata.gz: 8ae876214f7a2d73764d2a34c04eba1205917120d5133bee4c32ccd1219ba3de1b5e5be69d9deb11d6d53d1aa0e7e8015d4e47b74441c8926ee779d5bcb40db5
7
- data.tar.gz: 7c588aa80f8a762b8402f173adc902aded19e37821bf91ad0fb7ac5ab7ebf392c7b47f4f12ab31aba09a9b307a629ad3a9e5859f5bcf84515db2e674a899d511
6
+ metadata.gz: 92a28bab2a901ee32a7e19007e9eb0cd8ac01997acd033b080f9608e6bd9e333294d4b683a124af72cecd2aaaf523571bb57714082ccb8e5cf6b0e0627f7078e
7
+ data.tar.gz: b4bff787710ace35fbbce28815f95d6e4f5487ae7c21eb6601e4890ed6f8253831b6a38a9b50598301d77b8080518e48d00c5ba7b4a2384208d7155717d92c7b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.6.0 (2023-01-06)
4
+
5
+ * Support `value` for `float`, `int`, `str` and `sym` nodes
6
+ * Support `if` node
7
+ * Support `case`/`when` node
8
+ * Truly dynamically define methods based on const `TYPE_CHILDREN`
9
+
10
+ ## 0.5.1 (2022-12-26)
11
+
12
+ * hash node pairs should return an array of pair nodes
13
+
14
+ ## 0.5.0 (2022-12-25)
15
+
16
+ * Add primitive types
17
+
3
18
  ## 0.4.1 (2022-10-21)
4
19
 
5
20
  * Update error message
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- parser_node_ext (0.5.1)
4
+ parser_node_ext (0.6.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.1.3.0)
12
+ parser (3.2.0.0)
13
13
  ast (~> 2.4.1)
14
14
  rake (13.0.6)
15
15
  rspec (3.11.0)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ParserNodeExt
4
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -14,6 +14,7 @@ module ParserNodeExt
14
14
  begin: %i[body],
15
15
  block: %i[caller arguments body],
16
16
  blockarg: %i[name],
17
+ case: %i[expression when_statements else_statement],
17
18
  const: %i[parent_const name],
18
19
  class: %i[name parent_class body],
19
20
  csend: %i[receiver message arguments],
@@ -23,9 +24,10 @@ module ParserNodeExt
23
24
  definded?: %i[arguments],
24
25
  defs: %i[self name arguments body],
25
26
  false: [],
26
- float: [],
27
+ float: %i[value],
27
28
  hash: %i[pairs],
28
- int: [],
29
+ if: %i[expression if_statement else_statement],
30
+ int: %i[value],
29
31
  ivasgn: %i[left_value right_value],
30
32
  ivar: %i[name],
31
33
  lvar: %i[name],
@@ -38,10 +40,11 @@ module ParserNodeExt
38
40
  pair: %i[key value],
39
41
  restarg: %i[name],
40
42
  send: %i[receiver message arguments],
41
- str: [],
43
+ str: %i[value],
42
44
  super: %i[arguments],
43
- sym: [],
45
+ sym: %i[value],
44
46
  true: [],
47
+ when: %i[expression body],
45
48
  zsuper: []
46
49
  }
47
50
 
@@ -80,22 +83,8 @@ module ParserNodeExt
80
83
  parent.children[index + 1..]
81
84
  end
82
85
 
83
- # Dyamically defined method
84
- # caller, key, left_value, message, name, pairs, parent_class, parent_const, receivr, rgith_value and value.
85
- # based on const TYPE_CHILDREN.
86
- %i[
87
- caller
88
- key
89
- left_value
90
- message
91
- name
92
- pairs
93
- parent_class
94
- parent_const
95
- receiver
96
- right_value
97
- value
98
- ].each do |method_name|
86
+ # Dyamically defined method based on const TYPE_CHILDREN.
87
+ TYPE_CHILDREN.values.flatten.uniq.each do |method_name|
99
88
  define_method(method_name) do
100
89
  index = TYPE_CHILDREN[type]&.index(method_name)
101
90
  return children[index] if index
@@ -153,6 +142,10 @@ module ParserNodeExt
153
142
  case type
154
143
  when :begin
155
144
  children
145
+ when :when
146
+ return [] if children[1].nil?
147
+
148
+ :begin == children[1].type ? children[1].body : children[1..-1]
156
149
  when :def, :block, :class, :module
157
150
  return [] if children[2].nil?
158
151
 
@@ -166,21 +159,24 @@ module ParserNodeExt
166
159
  end
167
160
  end
168
161
 
169
- # Get condition of node.
170
- # It supports :if node.
171
- # @example
172
- # node # s(:if, s(:defined?, s(:const, nil, :Bundler)), nil, nil)
173
- # node.condition # s(:defined?, s(:const, nil, :Bundler))
174
- # @return [Parser::AST::Node] condition of node.
175
- # @raise [MethodNotSupported] if calls on other node.
176
- def condition
177
- if :if == type
178
- children[0]
162
+ def when_statements
163
+ if :case == type
164
+ children[1...-1]
179
165
  else
180
- raise MethodNotSupported, "condition is not supported for #{self}"
166
+ raise MethodNotSupported, "when_statements is not supported for #{self}"
181
167
  end
182
168
  end
183
169
 
170
+ def else_statement
171
+ children[-1]
172
+ end
173
+
174
+ # Get pairs of :hash node.
175
+ # @example
176
+ # node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)), s(:pair, s(:str, "foo"), s(:str, "bar")))
177
+ # node.pairs # [s(:pair, s(:sym, :foo), s(:sym, :bar)), s(:pair, s(:str, "foo"), s(:str, "bar"))]
178
+ # @return [Array<Parser::AST::Node>] pairs of node.
179
+ # @raise [MethodNotSupported] if calls on other node.
184
180
  def pairs
185
181
  if :hash == type
186
182
  children
@@ -5,6 +5,9 @@ module ParserNodeExt
5
5
  def arguments: () -> Array[Parser::AST::Node]
6
6
  def body: () -> Array[Parser::AST::Node]
7
7
  def caller: () -> Parser::AST::Node
8
+ def else_statement: () -> Parser::AST::Node
9
+ def expression: () -> Parser::AST::Node
10
+ def if_statement: () -> Parser::AST::Node
8
11
  def key: () -> Parser::AST::Node
9
12
  def left_value: () -> Parser::AST::Node | Symbol
10
13
  def message: () -> Symbol
@@ -15,6 +18,7 @@ module ParserNodeExt
15
18
  def right_value: () -> Parser::AST::Node
16
19
  def self: () -> Parser::AST::Node
17
20
  def value: () -> Parser::AST::Node
21
+ def when_statements: () -> Array[Parser::AST::Node]
18
22
 
19
23
  def to_hash: () -> Hash
20
24
  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.5.1
4
+ version: 0.6.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-12-26 00:00:00.000000000 Z
11
+ date: 2023-01-06 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.3.26
66
+ rubygems_version: 3.4.1
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: extend parser node