node_query 1.13.0 → 1.13.2

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: a2e54cf10c098ebfaff54d05d36f482c2766b5308f4b8389b2b6a86c1bfb6363
4
- data.tar.gz: 4d0b3f4ece89c3d4184c9b8159b924a2e321bafde7b598ee1789f17a41b73ee4
3
+ metadata.gz: '085d3a73c288ebe609e98d5ce1e7190afa6b3b9c80751b10c9f411559758f768'
4
+ data.tar.gz: 53d0498ea483d4d199a8116337efffa1885b4cf9b74c95d760898f5aee4d442e
5
5
  SHA512:
6
- metadata.gz: 0a0245239a1a6739dc8a9c51ddc507ddec1fba5e11820678d002af9572e4ed3e810b4ff4835bf6c6c36019e750f252da6a1fbbe08aa2abf00374d7010d6e7e36
7
- data.tar.gz: e5ae500ad6d17776a1c2becc86f451fa7cc222e731a309dcb86be654930e1b3d038ad649319fa7805e03c4ea54320959898413bbde79a07dfeb09a50831a897d
6
+ metadata.gz: 469b271b62d8f3bcd6883a9d6967af1c0a2b465fe4d06aab93efd18d043fd155543905c35751606de4c9d059add2cf27695f155205c8b4874181d8882a4de081
7
+ data.tar.gz: d6cefeec1185bf80aadfd2c2147b8e8d285a4a0950eb6227955d5075747fec74991d71a3c7a60e3633a90f1bf3d8dbb18bc77ee538454fdbf65deb6be2f99c02
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.13.2 (2023-05-18)
4
+
5
+ * Replace `Parser` specific code
6
+
7
+ ## 1.13.1 (2023-05-16)
8
+
9
+ * Require `parser` and `syntax_tree` in adapter
10
+ * `SyntaxTreeParser#get_node_type` returns a symbol
11
+ * Node type can be upcase
12
+
3
13
  ## 1.13.0 (2023-05-15)
4
14
 
5
15
  * Add `SyntaxTreeParser`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_query (1.13.0)
4
+ node_query (1.13.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -67,7 +67,7 @@ GEM
67
67
  shellany (0.0.1)
68
68
  syntax_tree (6.1.1)
69
69
  prettier_print (>= 1.2.0)
70
- syntax_tree_ext (0.3.0)
70
+ syntax_tree_ext (0.3.1)
71
71
  syntax_tree
72
72
  thor (1.2.1)
73
73
 
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'parser'
4
+ require 'parser_node_ext'
5
+
3
6
  class NodeQuery::ParserAdapter
4
7
  def is_node?(node)
5
8
  node.is_a?(Parser::AST::Node)
@@ -1,16 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'syntax_tree'
4
+ require 'syntax_tree_ext'
5
+
3
6
  class NodeQuery::SyntaxTreeAdapter
4
7
  def is_node?(node)
5
8
  node.is_a?(SyntaxTree::Node)
6
9
  end
7
10
 
8
11
  def get_node_type(node)
9
- node.class.name.split('::').last
12
+ node.class.name.split('::').last.to_sym
10
13
  end
11
14
 
12
15
  def get_source(node)
13
- node.source
16
+ node.to_source
14
17
  end
15
18
 
16
19
  def get_children(node)
@@ -90,22 +90,7 @@ module NodeQuery::Compiler
90
90
  # @return the node value, could be integer, float, string, boolean, nil, range, and etc.
91
91
  def actual_value(node)
92
92
  if NodeQuery.adapter.is_node?(node)
93
- case NodeQuery.adapter.get_node_type(node)
94
- when :int, :float, :str, :sym
95
- NodeQuery.adapter.get_children(node).last
96
- when :true
97
- true
98
- when :false
99
- false
100
- when :nil
101
- nil
102
- when :array
103
- NodeQuery.adapter.get_children(node).map { |child_node| actual_value(child_node) }
104
- when :begin
105
- actual_value(NodeQuery.adapter.get_children(node).first)
106
- else
107
- node
108
- end
93
+ node.to_value
109
94
  else
110
95
  node
111
96
  end
@@ -31,8 +31,8 @@ module NodeQuery::Compiler
31
31
  end
32
32
 
33
33
  # Check if node matches the selector.
34
- # @param node [Parser::AST::Node] the node
35
- # @param base_node [Parser::AST::Node] the base node for evaluated node
34
+ # @param node [Node] the node
35
+ # @param base_node [Node] the base node for evaluated node
36
36
  def match?(node, base_node, operator = "==")
37
37
  if node.is_a?(::Array)
38
38
  case operator
@@ -153,9 +153,9 @@ module NodeQuery::Compiler
153
153
  nodes << child_node if @rest.match?(child_node, child_node)
154
154
  end
155
155
  else
156
- node.children.each do |child_node|
156
+ NodeQuery.adapter.get_children(node).each do |child_node|
157
157
  if NodeQuery.adapter.is_node?(child_node) && :begin == NodeQuery.adapter.get_node_type(child_node)
158
- child_node.children.each do |child_child_node|
158
+ NodeQuery.adapter.get_children(child_node).each do |child_child_node|
159
159
  nodes << child_child_node if @rest.match?(child_child_node, child_child_node)
160
160
  end
161
161
  elsif @rest.match?(child_node, child_node)
@@ -30,7 +30,7 @@ class NodeQuery::Helper
30
30
  # Recursively handle child nodes.
31
31
  # @param node [Node] ast node
32
32
  # @yield [child] Gives a child node.
33
- # @yieldparam child [Parser::AST::Node] child node
33
+ # @yieldparam child [Node] child node
34
34
  def handle_recursive_child(node, &block)
35
35
  NodeQuery.adapter.get_children(node).each do |child|
36
36
  if NodeQuery.adapter.is_node?(child)
@@ -95,14 +95,14 @@ class NodeQuery::NodeRules
95
95
 
96
96
  case expected
97
97
  when Symbol
98
- if actual.is_a?(Parser::AST::Node)
98
+ if NodeQuery.adapter.is_node?(actual)
99
99
  actual_source = NodeQuery.adapter.get_source(actual)
100
100
  actual_source == ":#{expected}" || actual_source == expected.to_s
101
101
  else
102
102
  actual.to_sym == expected
103
103
  end
104
104
  when String
105
- if actual.is_a?(Parser::AST::Node)
105
+ if NodeQuery.adapter.is_node?(actual)
106
106
  actual_source = NodeQuery.adapter.get_source(actual)
107
107
  actual_source == expected || actual_source == unwrap_quote(expected) ||
108
108
  unwrap_quote(actual_source) == expected || unwrap_quote(actual_source) == unwrap_quote(expected)
@@ -110,7 +110,7 @@ class NodeQuery::NodeRules
110
110
  actual.to_s == expected || wrap_quote(actual.to_s) == expected
111
111
  end
112
112
  when Regexp
113
- if actual.is_a?(Parser::AST::Node)
113
+ if NodeQuery.adapter.is_node?(actual)
114
114
  actual.to_source =~ Regexp.new(expected.to_s, Regexp::MULTILINE)
115
115
  else
116
116
  actual.to_s =~ Regexp.new(expected.to_s, Regexp::MULTILINE)
@@ -120,13 +120,13 @@ class NodeQuery::NodeRules
120
120
 
121
121
  actual.zip(expected).all? { |a, e| match_value?(a, e) }
122
122
  when NilClass
123
- if actual.is_a?(Parser::AST::Node)
123
+ if NodeQuery.adapter.is_node?(actual)
124
124
  :nil == actual.type
125
125
  else
126
126
  actual.nil?
127
127
  end
128
128
  when Numeric
129
- if actual.is_a?(Parser::AST::Node)
129
+ if NodeQuery.adapter.is_node?(actual)
130
130
  actual.children[0] == expected
131
131
  else
132
132
  actual == expected
@@ -135,10 +135,12 @@ class NodeQuery::NodeRules
135
135
  :true == actual&.type
136
136
  when FalseClass
137
137
  :false == actual&.type
138
- when Parser::AST::Node
139
- actual == expected
140
138
  else
141
- raise NodeQuery::MethodNotSupported, "#{expected} is not supported"
139
+ if NodeQuery.adapter.is_node?(expected)
140
+ actual == expected
141
+ else
142
+ raise NodeQuery::MethodNotSupported, "#{expected} is not supported"
143
+ end
142
144
  end
143
145
  end
144
146
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeQuery
4
- VERSION = "1.13.0"
4
+ VERSION = "1.13.2"
5
5
  end
data/lib/node_query.rb CHANGED
@@ -18,7 +18,7 @@ class NodeQuery
18
18
  # @param [Hash] options options to configure
19
19
  # @option options [NodeQuery::Adapter] :adapter the adpater
20
20
  def self.configure(options)
21
- @adapter = options.adapter
21
+ @adapter = options[:adapter]
22
22
  end
23
23
 
24
24
  # Get the adapter
@@ -7,7 +7,7 @@ macros
7
7
  CLOSE_ARRAY /\)/
8
8
  OPEN_SELECTOR /\(/
9
9
  CLOSE_SELECTOR /\)/
10
- NODE_TYPE /\.[a-z]+/
10
+ NODE_TYPE /\.[a-zA-Z]+/
11
11
  IDENTIFIER /[@\*\-\.\w]*\w/
12
12
  IDENTIFIER_VALUE /[@\.\w!&:\?<>=]+/
13
13
  FALSE /false/
@@ -20,7 +20,7 @@ class NodeQueryLexer
20
20
  CLOSE_ARRAY = /\)/
21
21
  OPEN_SELECTOR = /\(/
22
22
  CLOSE_SELECTOR = /\)/
23
- NODE_TYPE = /\.[a-z]+/
23
+ NODE_TYPE = /\.[a-zA-Z]+/
24
24
  IDENTIFIER = /[@\*\-\.\w]*\w/
25
25
  IDENTIFIER_VALUE = /[@\.\w!&:\?<>=]+/
26
26
  FALSE = /false/
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: node_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.0
4
+ version: 1.13.2
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-05-15 00:00:00.000000000 Z
11
+ date: 2023-05-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ast node query language
14
14
  email: