prism_ext 0.1.0 → 0.2.1

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: d09f437a427a12d11ba67c28be50da7372493ad3a177938815c5b5abf065b5cf
4
- data.tar.gz: c2f18817e9862e578b3bbda235a7040d7b8526654888ae8f200a90fac00db0c3
3
+ metadata.gz: 5579e7f3f46ecbe569deead151875c5a0b4c55ca6b1694c281c206b63c02a806
4
+ data.tar.gz: c8ea4497448ce416a62261bd0fa217a8080fbdfe07ca4cfd6cf6524d288e1789
5
5
  SHA512:
6
- metadata.gz: 6174bbad0ec343872a8f15d0953c09f8ab642476a8ed6c68161969df4aed8c786cebd6559ba6ae2b2e9fbf0bd897c0ee2a89e55e4f7bda6c952e85b5c85c9e8b
7
- data.tar.gz: f49dde3337f979b258633a07fa1825d286e029e474d9fd560203d2e74efd77258a76bcfd90f6fdbc1add4588c1f3e1bcd8430b8e7949bbbd23547d752a9da38e
6
+ metadata.gz: c6ec0f9615e24af0e70423988e26b2c5dd92028e4b6efeb7199ca4566c5ec6109c4fccaf7e23c2c0b4e329305dc7ad8a8be3a97be2d2e3a456a9036dc9a03e51
7
+ data.tar.gz: 9365bc64d9f4dcb77e09e6a6cab9199b332268c3357117fad1305e69521f5468745aecde543e8d73453f721b32c9539614cbbf8755c6cc0be392ff88c0c6c051
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.2.1 (2024-02-11)
4
+
5
+ * Check both `HashNode` and `KeywordHashNode`.
6
+
7
+ ## 0.2.0 (2024-02-10)
8
+
9
+ * Set `parent_node` to `Prism::Node`.
10
+
3
11
  ## 0.1.0 (2024-02-10)
4
12
 
5
13
  * Add some helper methods to `Prism::Node`.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prism_ext (0.1.0)
4
+ prism_ext (0.2.1)
5
5
  prism
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # PrismExt
2
2
 
3
- It adds some helpers to prism node.
3
+ It adds `parent_node` to the `Prism::Node`.
4
+
5
+ It also adds some helpers to prism node.
4
6
 
5
7
  ```ruby
6
8
  # node is a HashNode
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PrismExt
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/prism_ext.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "prism_ext/version"
4
+
4
5
  require 'prism'
5
6
 
6
7
  module PrismExt
@@ -9,7 +10,37 @@ module PrismExt
9
10
  end
10
11
 
11
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
+
12
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
+
13
44
  def source
14
45
  location.instance_variable_get(:@source).source
15
46
  end
@@ -73,7 +104,7 @@ module Prism
73
104
 
74
105
  # Respond key value and source for hash node
75
106
  def method_missing(method_name, *args, &block)
76
- return super unless is_a?(HashNode)
107
+ return super unless respond_to_elements?
77
108
 
78
109
  if method_name.to_s.end_with?('_element')
79
110
  key = method_name.to_s[0..-9]
@@ -90,7 +121,7 @@ module Prism
90
121
  end
91
122
 
92
123
  def respond_to_missing?(method_name, *args)
93
- return super unless is_a?(HashNode)
124
+ return super unless respond_to_elements?
94
125
 
95
126
  if method_name.to_s.end_with?('_element')
96
127
  key = method_name.to_s[0..-9]
@@ -105,5 +136,11 @@ module Prism
105
136
 
106
137
  super
107
138
  end
139
+
140
+ private
141
+
142
+ def respond_to_elements?
143
+ is_a?(HashNode) || is_a?(KeywordHashNode)
144
+ end
108
145
  end
109
146
  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.1.0
4
+ version: 0.2.1
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-10 00:00:00.000000000 Z
11
+ date: 2024-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism