syntax_tree_ext 0.7.1 → 0.8.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: 76f93356ad00c2206f575f6aa8117de178eb2710a6534f8df11c10871f68a9b8
4
- data.tar.gz: a846f03d2f62bf8928764a6423df58ded2b1f524cf677c886f069f9e835cc7e9
3
+ metadata.gz: c33e697b9e1cdb6dd491d93f8dc78482ac0a358e1ecb2127a9f377d940df901c
4
+ data.tar.gz: 07f73e482d4740e1bfa13cd9d293accebe5e0fcb52374335c5f1d756bf5c6591
5
5
  SHA512:
6
- metadata.gz: 7e677f857fe5e663a92d2e2905535647ce0c4c6927da6a151a1ab88fd7c3265d465efba409cd4147c7c20486e7aedde93cb7bfa715c00f19d499708bfb5ae17e
7
- data.tar.gz: f9b15130df4786bb1b9548ea3d21eb234508270c38599b80cf01600fa7cd8de8c6150283c6e1e13bb69975b5aea6ad0535410fc952d8013963921ec4926efd7b
6
+ metadata.gz: 5e6dd31f3504dcca0ac4d70d0282ee7ab4d56523c31f2394ef16f5908abb17cccc2933e5d18207833f615d43d56eeebf9e3576e931a9ee63d442d2fdf45a9340
7
+ data.tar.gz: 0bff0422c3e75dd21dec2de6d0d991ec71c18d5a974cc1a72ccc2ba854fd49e4a8ab42994fc7c6290e90f5867ad62c96a566f5a7deddaa84497d88849a73cf69
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.8.0 (2024-04-07)
4
+
5
+ * Abstract `syntax_tree_ext/parent_node_ext`
6
+ * Abstract `syntax_tree_ext/source_ext`
7
+ * Inject hash helper methods only to `HashLiteral` and `BareAssocHash`
8
+
9
+ ## 0.7.2 (2024-02-17)
10
+
11
+ * Use `Hash#filter` instead of `Hash#reject`
12
+
3
13
  ## 0.7.1 (2024-02-11)
4
14
 
5
15
  * Reuse `respond_to_assocs`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree_ext (0.7.1)
4
+ syntax_tree_ext (0.8.0)
5
5
  syntax_tree
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,15 +1,9 @@
1
1
  # SyntaxTreeExt
2
2
 
3
- It adds `parent_node` and `source` methods to the `ParserTree::Node`.
3
+ [![Build Status](https://github.com/synvert-hq/syntax_tree_ext/actions/workflows/main.yml/badge.svg)](https://github.com/synvert-hq/syntax_tree_ext/actions/workflows/main.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/syntax_tree_ext.svg)](https://rubygems.org/gems/syntax_tree_ext)
4
5
 
5
- It also adds some helpers
6
-
7
- ```ruby
8
- # node is a HashLiteral node
9
- node.foo_assoc # get the assoc 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
+ It adds some helpers syntax_tree node.
13
7
 
14
8
  ## Installation
15
9
 
@@ -26,6 +20,17 @@ If bundler is not being used to manage dependencies, install the gem by executin
26
20
  ```ruby
27
21
  require 'syntax_tree'
28
22
  require 'syntax_tree_ext'
23
+
24
+ # node is a HashLiteral or BareAssocHash node
25
+ node.foo_assoc # get the assoc node of hash foo key
26
+ node.foo_value # get the value node of the hash foo key
27
+ node.foo_source # get the source of the value node of the hash foo key
28
+ node.keys # get key nodes of the hash node
29
+ node.values # get value nodes of the hash node
30
+
31
+ # all nodes
32
+ node.to_value # get the value of the node, like `true`, `false`, `nil`, `1`, `"foo"`
33
+ node.to_source # get the source code of the node
29
34
  ```
30
35
 
31
36
  ## Development
@@ -36,4 +41,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
36
41
 
37
42
  ## Contributing
38
43
 
39
- Bug reports and pull requests are welcome on GitHub at https://github.com/xinminlabs/syntax_tree_ext.
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/synvert-hq/syntax_tree_ext.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ class << self
5
+ alias_method :original_parse, :parse
6
+
7
+ def parse(source)
8
+ node = original_parse(source)
9
+ node.set_parent_node(source)
10
+ node
11
+ end
12
+ end
13
+
14
+ class Node
15
+ attr_accessor :parent_node
16
+
17
+ def set_parent_node_and_source(source)
18
+ self.deconstruct_keys([]).filter { |key, _value| ![:location, :comments].include?(key) }.values.each do |child_node|
19
+ if child_node.is_a?(Array)
20
+ child_node.each do |child_child_node|
21
+ next unless child_child_node.is_a?(Node)
22
+
23
+ child_child_node.parent_node = self
24
+ child_child_node.set_parent_node(source)
25
+ end
26
+ end
27
+
28
+ next unless child_node.is_a?(Node)
29
+
30
+ child_node.parent_node = self
31
+ child_node.set_parent_node(source)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ class << self
5
+ alias_method :original_parse, :parse
6
+
7
+ def parse(source)
8
+ node = original_parse(source)
9
+ node.set_source(source)
10
+ node
11
+ end
12
+ end
13
+
14
+ class Node
15
+ attr_accessor :source
16
+
17
+ def set_source(source)
18
+ self.source = source
19
+ self.deconstruct_keys([]).filter { |key, _value| ![:location, :comments].include?(key) }.values.each do |child_node|
20
+ if child_node.is_a?(Array)
21
+ child_node.each do |child_child_node|
22
+ next unless child_child_node.is_a?(Node)
23
+
24
+ child_child_node.set_source(source)
25
+ end
26
+ end
27
+
28
+ next unless child_node.is_a?(Node)
29
+
30
+ child_node.set_source(source)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTreeExt
4
- VERSION = "0.7.1"
4
+ VERSION = "0.8.0"
5
5
  end
@@ -3,14 +3,7 @@
3
3
  require_relative "syntax_tree_ext/version"
4
4
 
5
5
  require 'syntax_tree'
6
-
7
- if RUBY_VERSION.to_i < 3
8
- class Hash
9
- def except(*keys)
10
- self.reject { |k, _| keys.include?(k) }
11
- end
12
- end
13
- end
6
+ require_relative "syntax_tree_ext/source_ext"
14
7
 
15
8
  module SyntaxTreeExt
16
9
  class Error < StandardError; end
@@ -18,101 +11,25 @@ module SyntaxTreeExt
18
11
  end
19
12
 
20
13
  module SyntaxTree
21
- class << self
22
- alias_method :original_parse, :parse
23
-
24
- def parse(source)
25
- node = original_parse(source)
26
- node.set_parent_node_and_source(source)
27
- node
28
- end
29
- end
30
-
31
- class Node
32
- attr_accessor :parent_node, :source
33
-
34
- def set_parent_node_and_source(source)
35
- self.source = source
36
- self.deconstruct_keys([]).except(:location, :comments).values.each do |child_node|
37
- if child_node.is_a?(Array)
38
- child_node.each do |child_child_node|
39
- next unless child_child_node.is_a?(Node)
40
-
41
- child_child_node.parent_node = self
42
- child_child_node.set_parent_node_and_source(source)
43
- end
44
- end
45
-
46
- next unless child_node.is_a?(Node)
47
-
48
- child_node.parent_node = self
49
- child_node.set_parent_node_and_source(source)
50
- end
51
- end
52
-
14
+ module HashNodeExt
53
15
  def keys
54
- if respond_to_assocs?
55
- assocs.map(&:key)
56
- else
57
- raise MethodNotSupported, "keys is not supported for #{self}"
58
- end
16
+ assocs.map(&:key)
59
17
  end
60
18
 
61
19
  def values
62
- if respond_to_assocs?
63
- assocs.map(&:value)
64
- else
65
- raise MethodNotSupported, "values is not supported for #{self}"
66
- end
20
+ assocs.map(&:value)
67
21
  end
68
22
 
69
23
  def hash_assoc(key)
70
- if respond_to_assocs?
71
- assocs.find { |assoc_node| assoc_node.key.to_value == key }
72
- else
73
- raise MethodNotSupported, "hash_assoc is not supported for #{self}"
74
- end
24
+ assocs.find { |assoc_node| assoc_node.key.to_value == key }
75
25
  end
76
26
 
77
27
  def hash_value(key)
78
- if respond_to_assocs?
79
- assocs.find { |assoc_node| assoc_node.key.to_value == key }&.value
80
- else
81
- raise MethodNotSupported, "hash_value is not supported for #{self}"
82
- end
83
- end
84
-
85
- def to_value
86
- case self
87
- when SymbolLiteral
88
- value.value.to_sym
89
- when StringLiteral
90
- parts.map(&:to_value).join
91
- when FloatLiteral
92
- value.to_f
93
- when Int
94
- value.to_i
95
- when Kw
96
- value == 'true'
97
- when VarRef
98
- value.to_value
99
- when Const, Label, TStringContent, Ident
100
- value
101
- when ArrayLiteral
102
- contents ? contents.parts.map { |part| part.to_value } : []
103
- else
104
- self
105
- end
106
- end
107
-
108
- def to_source
109
- source[location.start_char...location.end_char]
28
+ assocs.find { |assoc_node| assoc_node.key.to_value == key }&.value
110
29
  end
111
30
 
112
31
  # Respond key value and source for hash node
113
32
  def method_missing(method_name, *args, &block)
114
- return super unless respond_to_assocs?
115
-
116
33
  if method_name.to_s.end_with?('_assoc')
117
34
  key = method_name.to_s[0..-7]
118
35
  return assocs.find { |assoc| assoc_key_equal?(assoc, key) }
@@ -128,8 +45,6 @@ module SyntaxTree
128
45
  end
129
46
 
130
47
  def respond_to_missing?(method_name, *args)
131
- return super unless respond_to_assocs?
132
-
133
48
  if method_name.to_s.end_with?('_assoc')
134
49
  key = method_name[0..-7]
135
50
  return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
@@ -146,13 +61,46 @@ module SyntaxTree
146
61
 
147
62
  private
148
63
 
149
- def respond_to_assocs?
150
- is_a?(HashLiteral) || is_a?(BareAssocHash)
151
- end
152
-
153
64
  def assoc_key_equal?(assoc, key)
154
65
  assoc_key = assoc.key.to_value.to_s
155
66
  assoc_key.end_with?(':') ? assoc_key == "#{key}:" : assoc_key == key
156
67
  end
157
68
  end
69
+
70
+ class HashLiteral
71
+ include HashNodeExt
72
+ end
73
+
74
+ class BareAssocHash
75
+ include HashNodeExt
76
+ end
77
+
78
+ class Node
79
+ def to_value
80
+ case self
81
+ when SymbolLiteral
82
+ value.value.to_sym
83
+ when StringLiteral
84
+ parts.map(&:to_value).join
85
+ when FloatLiteral
86
+ value.to_f
87
+ when Int
88
+ value.to_i
89
+ when Kw
90
+ value == 'true'
91
+ when VarRef
92
+ value.to_value
93
+ when Const, Label, TStringContent, Ident
94
+ value
95
+ when ArrayLiteral
96
+ contents ? contents.parts.map { |part| part.to_value } : []
97
+ else
98
+ self
99
+ end
100
+ end
101
+
102
+ def to_source
103
+ source[location.start_char...location.end_char]
104
+ end
105
+ end
158
106
  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.7.1
4
+ version: 0.8.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: 2024-02-11 00:00:00.000000000 Z
11
+ date: 2024-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: syntax_tree
@@ -38,14 +38,16 @@ files:
38
38
  - README.md
39
39
  - Rakefile
40
40
  - lib/syntax_tree_ext.rb
41
+ - lib/syntax_tree_ext/parent_node_ext.rb
42
+ - lib/syntax_tree_ext/source_ext.rb
41
43
  - lib/syntax_tree_ext/version.rb
42
44
  - sig/syntax_tree_ext.rbs
43
- homepage: https://github.com/xinminlabs/syntax_tree_ext
45
+ homepage: https://github.com/synvert-hq/syntax_tree_ext
44
46
  licenses: []
45
47
  metadata:
46
- homepage_uri: https://github.com/xinminlabs/syntax_tree_ext
47
- source_code_uri: https://github.com/xinminlabs/syntax_tree_ext
48
- changelog_uri: https://github.com/xinminlabs/syntax_tree_ext/blob/main/CHANGELOG
48
+ homepage_uri: https://github.com/synvert-hq/syntax_tree_ext
49
+ source_code_uri: https://github.com/synvert-hq/syntax_tree_ext
50
+ changelog_uri: https://github.com/synvert-hq/syntax_tree_ext/blob/main/CHANGELOG
49
51
  post_install_message:
50
52
  rdoc_options: []
51
53
  require_paths: