prism_ext 0.2.2 → 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: c5fbb378a9c09790397fcdda6460841dda3ee4a743f3fae4d19e6309b2239539
4
- data.tar.gz: d52081d91e7b7f133f3fd5241e14168765d0fbbe8cfeafb686dc79adbc285a4a
3
+ metadata.gz: 9dc2a569a5d46086c7979bf6dd2b6af03b766ba78a7d3909873e568bcf22baa9
4
+ data.tar.gz: 40a740a9bf6b7f3bd0920a5fc1af08cd27ff0ccaf5e88e172ef2757a690d5f19
5
5
  SHA512:
6
- metadata.gz: 6c9d6e3142e5ffbabe9c6ef8432e1ce58d1ae779ff8af732ed734cbd46e367b4816e7c5f816dc03cbc504e41b3299d9bad977f8a1127728bc7b34086cc4bcb21
7
- data.tar.gz: cc2f194f4e933b1110ddd6f6795c02cb5b50515ec106cc61b77d0051afa077f6a49f7794c3d827a9078e2af50892742a701a9910313ade8ae20a0701cd29df7e
6
+ metadata.gz: 3a8aa09f730f28d6faf50be52288d3c85f16ab3e724befc09221fd5ff89185c6b086c069324643a46b93e9650a503411433e994c336a33885e59df5e54bd9396
7
+ data.tar.gz: 27920f2028fa75a70c74a2240d9794c9346d960e22af211e83b76c1cb7ee1ee840c2011e6d0852dbb4af620a9015b9c239218fc2a9f611873ca2a41e7398c7e7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
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
+
8
+ ## 0.2.3 (2024-02-17)
9
+
10
+ * Remove `Prism::Node#source` hack
11
+
3
12
  ## 0.2.2 (2024-02-11)
4
13
 
5
14
  * Reuse `respond_to_elements?`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prism_ext (0.2.2)
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.2"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/prism_ext.rb CHANGED
@@ -10,102 +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.child_nodes.each do |child_node|
28
- if child_node.is_a?(Array)
29
- child_node.each do |child_child_node|
30
- next unless child_child_node.is_a?(Node)
31
-
32
- child_child_node.parent_node = self
33
- child_child_node.set_parent_node
34
- end
35
- end
36
-
37
- next unless child_node.is_a?(Node)
38
-
39
- child_node.parent_node = self
40
- child_node.set_parent_node
41
- end
42
- end
43
-
44
- def source
45
- location.instance_variable_get(:@source).source
46
- end
47
-
13
+ module HashNodeExt
48
14
  def keys
49
- if respond_to_elements?
50
- elements.map(&:key)
51
- else
52
- raise MethodNotSupported, "keys is not supported for #{self}"
53
- end
15
+ elements.map(&:key)
54
16
  end
55
17
 
56
18
  def values
57
- if respond_to_elements?
58
- elements.map(&:value)
59
- else
60
- raise MethodNotSupported, "values is not supported for #{self}"
61
- end
19
+ elements.map(&:value)
62
20
  end
63
21
 
64
22
  def hash_element(key)
65
- if respond_to_elements?
66
- elements.find { |element_node| element_node.key.to_value == key }
67
- else
68
- raise MethodNotSupported, "hash_pair is not supported for #{self}"
69
- end
23
+ elements.find { |element_node| element_node.key.to_value == key }
70
24
  end
71
25
 
72
26
  def hash_value(key)
73
- if respond_to_elements?
74
- elements.find { |element_node| element_node.key.to_value == key }&.value
75
- else
76
- raise MethodNotSupported, "hash_value is not supported for #{self}"
77
- end
27
+ elements.find { |element_node| element_node.key.to_value == key }&.value
78
28
  end
79
29
 
80
- def to_value
81
- case self
82
- when SymbolNode
83
- value.to_sym
84
- when StringNode
85
- content
86
- when FloatNode
87
- value
88
- when IntegerNode
89
- value
90
- when TrueNode
91
- true
92
- when FalseNode
93
- false
94
- when NilNode
95
- nil
96
- when ArrayNode
97
- elements.map { |element| element.to_value }
98
- else
99
- self
100
- end
101
- end
102
-
103
- alias :to_source :slice
104
-
105
30
  # Respond key value and source for hash node
106
31
  def method_missing(method_name, *args, &block)
107
- return super unless respond_to_elements?
108
-
109
32
  if method_name.to_s.end_with?('_element')
110
33
  key = method_name.to_s[0..-9]
111
34
  return elements.find { |element| element.key.to_value.to_s == key }
@@ -121,8 +44,6 @@ module Prism
121
44
  end
122
45
 
123
46
  def respond_to_missing?(method_name, *args)
124
- return super unless respond_to_elements?
125
-
126
47
  if method_name.to_s.end_with?('_element')
127
48
  key = method_name.to_s[0..-9]
128
49
  return !!elements.find { |element| element.key.to_value.to_s == key }
@@ -136,11 +57,40 @@ module Prism
136
57
 
137
58
  super
138
59
  end
60
+ end
139
61
 
140
- private
62
+ class HashNode
63
+ include HashNodeExt
64
+ end
141
65
 
142
- def respond_to_elements?
143
- is_a?(HashNode) || is_a?(KeywordHashNode)
66
+ class KeywordHashNode
67
+ include HashNodeExt
68
+ end
69
+
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
144
92
  end
93
+
94
+ alias :to_source :slice
145
95
  end
146
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.2
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-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: 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: