syntax_tree_ext 0.3.0 → 0.4.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: b6521c0c1d5ead0d51f0dce8810f7dcb8e080f4ebceb14a1a0c82a1ea9de5a44
4
- data.tar.gz: 384efd9248ed1002836b9b481d1ca727d058120f6cb082eecd9b4190cac609a5
3
+ metadata.gz: 252bb9039575140c0f6880c558a9dc3b19b08e400f673184672a4064584473aa
4
+ data.tar.gz: cfe4011f7e28f90f1ce9a30573fde27529f0f92eabc1af79719cd2d5a4405033
5
5
  SHA512:
6
- metadata.gz: 9fea364be6aa7bb2303d5932b050efdae247dd8cb8f6e84d1906759eb6aef3dd0acd245e7f16c2e50e897d32a90a9fef6f79d29a03482a73c79bdfc2b1e40adb
7
- data.tar.gz: 49e7e190eaa3772b5bfb29e053bb366ebb00581ed28de823a8bb2aa1500c960d455519cb12ac64d13c395f31086e840c18341e348f5c07b896fac8f329bcb550
6
+ metadata.gz: f729e476eb65c5587c4a2bc3fd6026ef9debd27af3b951b4a9a269480fad087c819a337084b401df9cc15e8ea82349879245f427bcbb0d1026c4d55b707d6c59
7
+ data.tar.gz: c329ff9f956c7ba8e718ffc8df2f5f05c6bd7c6e556c28828e3da5378db4fcd91f44e7a71a242391e2ff7b53750b57b3133247285e0a78bac10e53747d285876
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.0 (2023-06-08)
4
+
5
+ * Add `Syntax::Node#to_hash`
6
+
7
+ ## 0.3.1 (2023-05-18)
8
+
9
+ * Support `array` in `Node#to_value`
10
+
3
11
  ## 0.3.0 (2023-05-15)
4
12
 
5
13
  * Support `xxx_assoc` to get assoc node of the hash node
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree_ext (0.3.0)
4
+ syntax_tree_ext (0.4.0)
5
5
  syntax_tree
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTreeExt
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -52,8 +52,10 @@ module SyntaxTree
52
52
  value == 'true'
53
53
  when VarRef
54
54
  value.to_value
55
- when Label, TStringContent
55
+ when Label, TStringContent, Ident
56
56
  value
57
+ when ArrayLiteral
58
+ contents.parts.map { |part| part.to_value }
57
59
  else
58
60
  self
59
61
  end
@@ -63,6 +65,27 @@ module SyntaxTree
63
65
  source[location.start_char...location.end_char]
64
66
  end
65
67
 
68
+ # Convert node to a hash, so that it can be converted to a json.
69
+ def to_hash
70
+ result = { node_type: self.class.name.split('::').last }
71
+ unless deconstruct_keys([]).empty?
72
+ deconstruct_keys([]).each do |key, value|
73
+ result[key] =
74
+ case value
75
+ when Array
76
+ value.map { |v| v.respond_to?(:to_hash) ? v.to_hash : v }
77
+ when SyntaxTree::Node, SyntaxTree::Location
78
+ value.to_hash
79
+ else
80
+ value
81
+ end
82
+ end
83
+ else
84
+ result[:children] = children.map { |c| c.respond_to?(:to_hash) ? c.to_hash : c }
85
+ end
86
+ result
87
+ end
88
+
66
89
  # Respond key value and source for hash node
67
90
  def method_missing(method_name, *args, &block)
68
91
  return super unless respond_to_assocs?
@@ -109,4 +132,17 @@ module SyntaxTree
109
132
  assoc_key.end_with?(':') ? assoc_key == "#{key}:" : assoc_key == key
110
133
  end
111
134
  end
135
+
136
+ class Location
137
+ def to_hash
138
+ {
139
+ start_line: start_line,
140
+ start_char: start_char,
141
+ start_column: start_column,
142
+ end_line: end_line,
143
+ end_char: end_char,
144
+ end_column: end_column,
145
+ }
146
+ end
147
+ end
112
148
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_tree_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.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-05-15 00:00:00.000000000 Z
11
+ date: 2023-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: syntax_tree
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
- rubygems_version: 3.4.10
64
+ rubygems_version: 3.4.13
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: extend syntax_tree