parser_node_ext 0.2.0 → 0.3.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: b640f12cdf44e564f22af212354d86e2e76329cb796f68f843ba1ff151bfe1cc
4
- data.tar.gz: 17cfcb04b5f079946160fffb800cd919654cf1a837d48629b7102e8941c88dfb
3
+ metadata.gz: 0b8a89fd698e65560bb6ec6288cc2739b8073440b2763c6f37d8f2a662e7d629
4
+ data.tar.gz: 36be4ab919e445d38e506f6b91cdbeac541568d328459eb6b4756d21fb1edfc5
5
5
  SHA512:
6
- metadata.gz: 76d8308f1debc8b852d7a6d2bb3f065b6266dbd8a46d844f30135bc0f39ea755afcd7e3685ad25b85fc999bbe497daad1edfa35e26dc9961ba68d92da3d8b1ca
7
- data.tar.gz: 1f1d833611f71a5c1c18e58444d799c5e3d491a50dea28495bf14113c47d11014aa7d955f3164a42d89b5c994dee3e911206d1e193e3e99be0d41140ce91e23c
6
+ metadata.gz: c0e1ac0d615a85c08ac82a793ac3f4c205e44f14360593836ea37dc791c237b7d72691ba35525d60a2dd9e3a2dbc270fddfc036514d2cfa9680755c0850262de
7
+ data.tar.gz: f45b6a7df41303d14829b7416e761ce0b5737a9030b986522c1dd3704ea6b528390ec7137991c7916fa25c3034028e1a60298ff5fd7803e6555bc3e0ab867087
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.3.0 (2022-07-04)
4
+
5
+ * Add `Node#to_hash`
6
+
3
7
  ## 0.2.0 (2022-06-27)
4
8
 
5
9
  * Add `Node#to_value`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- parser_node_ext (0.2.0)
4
+ parser_node_ext (0.3.0)
5
5
  parser
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ParserNodeExt
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -5,8 +5,6 @@ require_relative "parser_node_ext/version"
5
5
  require 'parser'
6
6
 
7
7
  module ParserNodeExt
8
- class Error < StandardError; end
9
-
10
8
  class MethodNotSupported < StandardError; end
11
9
  # Your code goes here...
12
10
 
@@ -272,6 +270,28 @@ module ParserNodeExt
272
270
  loc.expression&.source
273
271
  end
274
272
 
273
+ # Convert node to a hash, so that it can be converted to a json.
274
+ def to_hash
275
+ result = { type: type }
276
+ if TYPE_CHILDREN[type]
277
+ TYPE_CHILDREN[type].each do |key|
278
+ value = send(key)
279
+ result[key] =
280
+ case value
281
+ when Array
282
+ value.map { |v| v.respond_to?(:to_hash) ? v.to_hash : v }
283
+ when Parser::AST::Node
284
+ value.to_hash
285
+ else
286
+ value
287
+ end
288
+ end
289
+ else
290
+ result[:children] = children.map { |c| c.respond_to?(:to_hash) ? c.to_hash : c }
291
+ end
292
+ result
293
+ end
294
+
275
295
  # Respond key value and source for hash node, e.g.
276
296
  # @example
277
297
  # node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
@@ -2,17 +2,19 @@ module ParserNodeExt
2
2
  VERSION: String
3
3
  # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
4
 
5
- def arguments: Array[Parser::AST::Node]
6
- def body: Array[Parser::AST::Node]
7
- def caller: Parser::AST::Node
8
- def key: Parser::AST::Node
9
- def left_value: Parser::AST::Node | Symbol
10
- def message: Symbol
11
- def name: Parser::AST::Node | Symbol
12
- def pairs: Array[Parser::AST::Node]
13
- def parent_class: Parser::AST::Node
14
- def receiver: Parser::AST::Node
15
- def right_value: Parser::AST::Node
16
- def self: Parser::AST::Node
17
- def value: Parser::AST::Node
5
+ def arguments: () -> Array[Parser::AST::Node]
6
+ def body: () -> Array[Parser::AST::Node]
7
+ def caller: () -> Parser::AST::Node
8
+ def key: () -> Parser::AST::Node
9
+ def left_value: () -> Parser::AST::Node | Symbol
10
+ def message: () -> Symbol
11
+ def name: () -> Parser::AST::Node | Symbol
12
+ def pairs: () -> Array[Parser::AST::Node]
13
+ def parent_class: () -> Parser::AST::Node
14
+ def receiver: () -> Parser::AST::Node
15
+ def right_value: () -> Parser::AST::Node
16
+ def self: () -> Parser::AST::Node
17
+ def value: () -> Parser::AST::Node
18
+
19
+ def to_hash: () -> Hash
18
20
  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.2.0
4
+ version: 0.3.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-27 00:00:00.000000000 Z
11
+ date: 2022-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser