prism_ext 0.2.3 → 0.3.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: 796c4e8f489be737f684de406b557fd5e558686ce3e8e3ed6570537851ccf4fd
4
- data.tar.gz: d91ee9f891a1653763b4b03721233eb3c643174f72e486505534d7e93706bddc
3
+ metadata.gz: 9dc2a569a5d46086c7979bf6dd2b6af03b766ba78a7d3909873e568bcf22baa9
4
+ data.tar.gz: 40a740a9bf6b7f3bd0920a5fc1af08cd27ff0ccaf5e88e172ef2757a690d5f19
5
5
  SHA512:
6
- metadata.gz: 8b5c512f633da3c49c51c0364d7dd2bfeb7ee76cd87863dc54ce46f8ce9b9728b4fda7b2ae6287eea118ba926a7fe69a396a7bcb2d8e08644006bfd3a412e5c5
7
- data.tar.gz: 0230e47fe757e7bf5d7cec6770d53131145b0b3ac14869e01cab1f55bafa6428d0a6bcb80b29172b4bd3516a3d39435da230ccbf25ee9bbeb61c4727fc2ce17f
6
+ metadata.gz: 3a8aa09f730f28d6faf50be52288d3c85f16ab3e724befc09221fd5ff89185c6b086c069324643a46b93e9650a503411433e994c336a33885e59df5e54bd9396
7
+ data.tar.gz: 27920f2028fa75a70c74a2240d9794c9346d960e22af211e83b76c1cb7ee1ee840c2011e6d0852dbb4af620a9015b9c239218fc2a9f611873ca2a41e7398c7e7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.3.0 (2024-04-07)
4
+
5
+ * Abstract `prism_ext/parent_node_ext`
6
+ * Inject hash helper methods only to `HashNode` and `KeywordHashNode`
7
+
3
8
  ## 0.2.3 (2024-02-17)
4
9
 
5
10
  * Remove `Prism::Node#source` hack
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prism_ext (0.2.3)
4
+ prism_ext (0.3.0)
5
5
  prism
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,15 +1,9 @@
1
1
  # PrismExt
2
2
 
3
- It adds `parent_node` to the `Prism::Node`.
3
+ [![Build Status](https://github.com/synvert-hq/prism_ext/actions/workflows/main.yml/badge.svg)](https://github.com/synvert-hq/prism_ext/actions/workflows/main.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/prism_ext.svg)](https://rubygems.org/gems/prism_ext)
4
5
 
5
- It also adds some helpers to prism node.
6
-
7
- ```ruby
8
- # node is a HashNode
9
- node.foo_element # get the element node of hash foo key
10
- node.foo_value # get the value of 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 to prism 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 'prism'
28
22
  require 'prism_ext'
23
+
24
+ # node is a HashNode or KeywordHashNode
25
+ node.foo_element # get the element node of hash foo key
26
+ node.foo_value # get the value of 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/prism_ext.
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/synvert-hq/prism_ext.
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prism
4
+ class << self
5
+ alias_method :original_parse, :parse
6
+
7
+ def parse(source)
8
+ result = original_parse(source)
9
+ result.value.set_parent_node
10
+ result
11
+ end
12
+ end
13
+
14
+ class Node
15
+ attr_accessor :parent_node
16
+
17
+ def set_parent_node
18
+ self.compact_child_nodes.each do |child_node|
19
+ if child_node.is_a?(Array)
20
+ child_node.each do |child_child_node|
21
+ child_child_node.parent_node = self
22
+ child_child_node.set_parent_node
23
+ end
24
+ else
25
+ child_node.parent_node = self
26
+ child_node.set_parent_node
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PrismExt
4
- VERSION = "0.2.3"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/prism_ext.rb CHANGED
@@ -10,94 +10,25 @@ module PrismExt
10
10
  end
11
11
 
12
12
  module Prism
13
- class << self
14
- alias_method :original_parse, :parse
15
-
16
- def parse(source)
17
- result = original_parse(source)
18
- result.value.set_parent_node
19
- result
20
- end
21
- end
22
-
23
- class Node
24
- attr_accessor :parent_node
25
-
26
- def set_parent_node
27
- self.compact_child_nodes.each do |child_node|
28
- if child_node.is_a?(Array)
29
- child_node.each do |child_child_node|
30
- child_child_node.parent_node = self
31
- child_child_node.set_parent_node
32
- end
33
- else
34
- child_node.parent_node = self
35
- child_node.set_parent_node
36
- end
37
- end
38
- end
39
-
13
+ module HashNodeExt
40
14
  def keys
41
- if respond_to_elements?
42
- elements.map(&:key)
43
- else
44
- raise MethodNotSupported, "keys is not supported for #{self}"
45
- end
15
+ elements.map(&:key)
46
16
  end
47
17
 
48
18
  def values
49
- if respond_to_elements?
50
- elements.map(&:value)
51
- else
52
- raise MethodNotSupported, "values is not supported for #{self}"
53
- end
19
+ elements.map(&:value)
54
20
  end
55
21
 
56
22
  def hash_element(key)
57
- if respond_to_elements?
58
- elements.find { |element_node| element_node.key.to_value == key }
59
- else
60
- raise MethodNotSupported, "hash_pair is not supported for #{self}"
61
- end
23
+ elements.find { |element_node| element_node.key.to_value == key }
62
24
  end
63
25
 
64
26
  def hash_value(key)
65
- if respond_to_elements?
66
- elements.find { |element_node| element_node.key.to_value == key }&.value
67
- else
68
- raise MethodNotSupported, "hash_value is not supported for #{self}"
69
- end
70
- end
71
-
72
- def to_value
73
- case self
74
- when SymbolNode
75
- value.to_sym
76
- when StringNode
77
- content
78
- when FloatNode
79
- value
80
- when IntegerNode
81
- value
82
- when TrueNode
83
- true
84
- when FalseNode
85
- false
86
- when NilNode
87
- nil
88
- when ArrayNode
89
- elements.map { |element| element.to_value }
90
- else
91
- self
92
- end
27
+ elements.find { |element_node| element_node.key.to_value == key }&.value
93
28
  end
94
29
 
95
- alias :to_source :slice
96
-
97
30
  # Respond key value and source for hash node
98
31
  def method_missing(method_name, *args, &block)
99
- return super unless respond_to_elements?
100
-
101
32
  if method_name.to_s.end_with?('_element')
102
33
  key = method_name.to_s[0..-9]
103
34
  return elements.find { |element| element.key.to_value.to_s == key }
@@ -113,8 +44,6 @@ module Prism
113
44
  end
114
45
 
115
46
  def respond_to_missing?(method_name, *args)
116
- return super unless respond_to_elements?
117
-
118
47
  if method_name.to_s.end_with?('_element')
119
48
  key = method_name.to_s[0..-9]
120
49
  return !!elements.find { |element| element.key.to_value.to_s == key }
@@ -128,11 +57,40 @@ module Prism
128
57
 
129
58
  super
130
59
  end
60
+ end
131
61
 
132
- private
62
+ class HashNode
63
+ include HashNodeExt
64
+ end
65
+
66
+ class KeywordHashNode
67
+ include HashNodeExt
68
+ end
133
69
 
134
- def respond_to_elements?
135
- is_a?(HashNode) || is_a?(KeywordHashNode)
70
+ class Node
71
+ def to_value
72
+ case self
73
+ when SymbolNode
74
+ value.to_sym
75
+ when StringNode
76
+ content
77
+ when FloatNode
78
+ value
79
+ when IntegerNode
80
+ value
81
+ when TrueNode
82
+ true
83
+ when FalseNode
84
+ false
85
+ when NilNode
86
+ nil
87
+ when ArrayNode
88
+ elements.map { |element| element.to_value }
89
+ else
90
+ self
91
+ end
136
92
  end
93
+
94
+ alias :to_source :slice
137
95
  end
138
96
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prism_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
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: 2024-02-17 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: prism
@@ -38,14 +38,15 @@ files:
38
38
  - README.md
39
39
  - Rakefile
40
40
  - lib/prism_ext.rb
41
+ - lib/prism_ext/parent_node_ext.rb
41
42
  - lib/prism_ext/version.rb
42
43
  - sig/prism_ext.rbs
43
- homepage: https://github.com/xinminlabs/prism_ext
44
+ homepage: https://github.com/synvert-hq/prism_ext
44
45
  licenses: []
45
46
  metadata:
46
- homepage_uri: https://github.com/xinminlabs/prism_ext
47
- source_code_uri: https://github.com/xinminlabs/prism_ext
48
- changelog_uri: https://github.com/xinminlabs/prism_ext/CHANGELOG
47
+ homepage_uri: https://github.com/synvert-hq/prism_ext
48
+ source_code_uri: https://github.com/synvert-hq/prism_ext
49
+ changelog_uri: https://github.com/synvert-hq/prism_ext/CHANGELOG
49
50
  post_install_message:
50
51
  rdoc_options: []
51
52
  require_paths: