parser_node_ext 0.5.0 → 0.6.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: b9bee187df49c963339ad5b6ed34721fb558e93b066593ac8fdf34ae453932f4
4
- data.tar.gz: 960b04b95fb1ae5414d4ca820f22e1b2ae117c47e2d4502c9503848fc5048c19
3
+ metadata.gz: caf4a4308e04940f7b74d41db9bdf1ae06a15f9b4a63f565f4b4731c89d11693
4
+ data.tar.gz: edcb3bc6b1d55dfec2b17b5e97e1fe5bd86b83f09e5cb4a41e2ea9c0f0c5b4cb
5
5
  SHA512:
6
- metadata.gz: 6a1432adf0618a77279d580ae9ad8de92a5e601c15256bedbafd870d3f46348c07143c11e25c4e3717dd6de19a99f22f1ce0306247ef5ad0f9529799d04fe0e1
7
- data.tar.gz: 84e0d2ed48ba5650714e0bf994b43b7d9ef8f1191cf24941bf8a376963128145cbb970adb05f3e22f60ba6184d31f598f3b38b1a333b3d8a541149f3298cf9b5
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.0)
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.0"
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,18 +159,29 @@ module ParserNodeExt
166
159
  end
167
160
  end
168
161
 
169
- # Get condition of node.
170
- # It supports :if node.
162
+ def when_statements
163
+ if :case == type
164
+ children[1...-1]
165
+ else
166
+ raise MethodNotSupported, "when_statements is not supported for #{self}"
167
+ end
168
+ end
169
+
170
+ def else_statement
171
+ children[-1]
172
+ end
173
+
174
+ # Get pairs of :hash node.
171
175
  # @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.
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.
175
179
  # @raise [MethodNotSupported] if calls on other node.
176
- def condition
177
- if :if == type
178
- children[0]
180
+ def pairs
181
+ if :hash == type
182
+ children
179
183
  else
180
- raise MethodNotSupported, "condition is not supported for #{self}"
184
+ raise MethodNotSupported, "pairs is not supported for #{self}"
181
185
  end
182
186
  end
183
187
 
@@ -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.0
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-25 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