syntax_tree_ext 0.1.0 → 0.3.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: d51e4d0fd788f5234eded21764f3e8e7a85dd27038c9c40f0a7f211a1a84dd60
4
- data.tar.gz: fb064aee9423515356daf3e935bbfcac7320e56883d25fad0cc179380f78bcc1
3
+ metadata.gz: b6521c0c1d5ead0d51f0dce8810f7dcb8e080f4ebceb14a1a0c82a1ea9de5a44
4
+ data.tar.gz: 384efd9248ed1002836b9b481d1ca727d058120f6cb082eecd9b4190cac609a5
5
5
  SHA512:
6
- metadata.gz: d68490aa8fa813bd72e892acf2db74627142c60dd2c6f6d5f163593ba999b026fda774c3fa0c6d37c0eaa293ebe7baeab4f18aeb0126d5fefc50df3d42cf7edb
7
- data.tar.gz: 69554ffc9196a4c0c758c91771a60b7e39b0cb65dfb6535e4f918ce81e93c15e7d5764188c40d9e6b51d7591067cb262e8d2b6f0102d6d43a50ffe6f5933dfe4
6
+ metadata.gz: 9fea364be6aa7bb2303d5932b050efdae247dd8cb8f6e84d1906759eb6aef3dd0acd245e7f16c2e50e897d32a90a9fef6f79d29a03482a73c79bdfc2b1e40adb
7
+ data.tar.gz: 49e7e190eaa3772b5bfb29e053bb366ebb00581ed28de823a8bb2aa1500c960d455519cb12ac64d13c395f31086e840c18341e348f5c07b896fac8f329bcb550
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.3.0 (2023-05-15)
4
+
5
+ * Support `xxx_assoc` to get assoc node of the hash node
6
+
7
+ ## 0.2.0 (2023-05-14)
8
+
9
+ * Rename `Node#parent` to `Node#parent_node`
10
+ * Add `Node#to_value` and `Node#to_source`
11
+ * Add `Node#method_missing` for hash `xxx_value` and `xxx_pair`
12
+
3
13
  ## 0.1.0 (2023-04-20)
4
14
 
5
15
  * Add `SyntaxTree::Node#siblings`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree_ext (0.1.0)
4
+ syntax_tree_ext (0.3.0)
5
5
  syntax_tree
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,24 +1,32 @@
1
1
  # SyntaxTreeExt
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ It adds `parent_node` and `source` methods to the `ParserTree::Node`.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/syntax_tree_ext`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ It also adds some helpers
6
6
 
7
- ## Installation
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
+ ```
8
13
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
14
+ ## Installation
10
15
 
11
16
  Install the gem and add to the application's Gemfile by executing:
12
17
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+ $ bundle add syntax_tree_ext
14
19
 
15
20
  If bundler is not being used to manage dependencies, install the gem by executing:
16
21
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
22
+ $ gem install syntax_tree_ext
18
23
 
19
24
  ## Usage
20
25
 
21
- TODO: Write usage instructions here
26
+ ```ruby
27
+ require 'syntax_tree'
28
+ require 'syntax_tree_ext'
29
+ ```
22
30
 
23
31
  ## Development
24
32
 
@@ -28,4 +36,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
36
 
29
37
  ## Contributing
30
38
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/syntax_tree_ext.
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xinminlabs/syntax_tree_ext.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTreeExt
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -15,31 +15,98 @@ module SyntaxTree
15
15
 
16
16
  def parse
17
17
  node = original_parse
18
- node.set_parent_and_source(source)
18
+ node.set_parent_node_and_source(source)
19
19
  node
20
20
  end
21
21
  end
22
22
 
23
23
  class Node
24
- attr_accessor :parent, :source
24
+ attr_accessor :parent_node, :source
25
25
 
26
- def set_parent_and_source(source)
26
+ def set_parent_node_and_source(source)
27
27
  self.source = source
28
28
  child_nodes.each do |child_node|
29
29
  next unless child_node.is_a?(Node)
30
30
 
31
- child_node.parent = self
32
- child_node.set_parent_and_source(source)
31
+ child_node.parent_node = self
32
+ child_node.set_parent_node_and_source(source)
33
33
  end
34
34
  end
35
35
 
36
36
  def siblings
37
- index = parent.child_nodes.index(self)
38
- parent.child_nodes[index + 1...]
37
+ index = parent_node.child_nodes.index(self)
38
+ parent_node.child_nodes[index + 1...]
39
39
  end
40
40
 
41
- def source
42
- @source[location.start_char..location.end_char]
41
+ def to_value
42
+ case self
43
+ when SymbolLiteral
44
+ value.value.to_sym
45
+ when StringLiteral
46
+ parts.map(&:to_value).join
47
+ when FloatLiteral
48
+ value.to_f
49
+ when Int
50
+ value.to_i
51
+ when Kw
52
+ value == 'true'
53
+ when VarRef
54
+ value.to_value
55
+ when Label, TStringContent
56
+ value
57
+ else
58
+ self
59
+ end
60
+ end
61
+
62
+ def to_source
63
+ source[location.start_char...location.end_char]
64
+ end
65
+
66
+ # Respond key value and source for hash node
67
+ def method_missing(method_name, *args, &block)
68
+ return super unless respond_to_assocs?
69
+
70
+ if method_name.to_s.end_with?('_assoc')
71
+ key = method_name.to_s[0..-7]
72
+ return assocs.find { |assoc| assoc_key_equal?(assoc, key) }
73
+ elsif method_name.to_s.end_with?('_value')
74
+ key = method_name.to_s[0..-7]
75
+ return assocs.find { |assoc| assoc_key_equal?(assoc, key) }&.value
76
+ elsif method_name.to_s.end_with?('_source')
77
+ key = method_name.to_s[0..-8]
78
+ return assocs.find { |assoc| assoc_key_equal?(assoc, key) }&.value&.to_source || ''
79
+ end
80
+
81
+ super
82
+ end
83
+
84
+ def respond_to_missing?(method_name, *args)
85
+ return super unless respond_to_assocs?
86
+
87
+ if method_name.to_s.end_with?('_assoc')
88
+ key = method_name[0..-7]
89
+ return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
90
+ elsif method_name.to_s.end_with?('_value')
91
+ key = method_name[0..-7]
92
+ return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
93
+ elsif method_name.to_s.end_with?('_source')
94
+ key = method_name.to_s[0..-8]
95
+ return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
96
+ end
97
+
98
+ super
99
+ end
100
+
101
+ private
102
+
103
+ def respond_to_assocs?
104
+ is_a?(HashLiteral) || is_a?(BareAssocHash)
105
+ end
106
+
107
+ def assoc_key_equal?(assoc, key)
108
+ assoc_key = assoc.key.to_value.to_s
109
+ assoc_key.end_with?(':') ? assoc_key == "#{key}:" : assoc_key == key
43
110
  end
44
111
  end
45
112
  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.1.0
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: 2023-04-20 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: syntax_tree