node_query 1.12.0 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +10 -4
- data/README.md +14 -13
- data/lib/node_query/adapter/syntax_tree.rb +23 -0
- data/lib/node_query/adapter.rb +1 -1
- data/lib/node_query/compiler/basic_selector.rb +4 -1
- data/lib/node_query/compiler/comparable.rb +7 -3
- data/lib/node_query/compiler/selector.rb +13 -2
- data/lib/node_query/helper.rb +3 -3
- data/lib/node_query/node_rules.rb +7 -6
- data/lib/node_query/version.rb +1 -1
- data/lib/node_query.rb +2 -1
- data/lib/node_query_parser.racc.rb +1 -1
- metadata +5 -4
- /data/lib/node_query/{parser_adapter.rb → adapter/parser.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2e54cf10c098ebfaff54d05d36f482c2766b5308f4b8389b2b6a86c1bfb6363
|
4
|
+
data.tar.gz: 4d0b3f4ece89c3d4184c9b8159b924a2e321bafde7b598ee1789f17a41b73ee4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a0245239a1a6739dc8a9c51ddc507ddec1fba5e11820678d002af9572e4ed3e810b4ff4835bf6c6c36019e750f252da6a1fbbe08aa2abf00374d7010d6e7e36
|
7
|
+
data.tar.gz: e5ae500ad6d17776a1c2becc86f451fa7cc222e731a309dcb86be654930e1b3d038ad649319fa7805e03c4ea54320959898413bbde79a07dfeb09a50831a897d
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
node_query (1.
|
4
|
+
node_query (1.13.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -38,14 +38,15 @@ GEM
|
|
38
38
|
nenv (~> 0.1)
|
39
39
|
shellany (~> 0.0)
|
40
40
|
oedipus_lex (2.6.0)
|
41
|
-
parser (3.
|
41
|
+
parser (3.2.2.1)
|
42
42
|
ast (~> 2.4.1)
|
43
|
-
parser_node_ext (
|
43
|
+
parser_node_ext (1.1.0)
|
44
44
|
parser
|
45
|
+
prettier_print (1.2.1)
|
45
46
|
pry (0.14.1)
|
46
47
|
coderay (~> 1.1)
|
47
48
|
method_source (~> 1.0)
|
48
|
-
racc (1.6.
|
49
|
+
racc (1.6.2)
|
49
50
|
rake (13.0.6)
|
50
51
|
rb-fsevent (0.11.1)
|
51
52
|
rb-inotify (0.10.1)
|
@@ -64,6 +65,10 @@ GEM
|
|
64
65
|
rspec-support (~> 3.11.0)
|
65
66
|
rspec-support (3.11.0)
|
66
67
|
shellany (0.0.1)
|
68
|
+
syntax_tree (6.1.1)
|
69
|
+
prettier_print (>= 1.2.0)
|
70
|
+
syntax_tree_ext (0.3.0)
|
71
|
+
syntax_tree
|
67
72
|
thor (1.2.1)
|
68
73
|
|
69
74
|
PLATFORMS
|
@@ -82,6 +87,7 @@ DEPENDENCIES
|
|
82
87
|
racc
|
83
88
|
rake (~> 13.0)
|
84
89
|
rspec (~> 3.0)
|
90
|
+
syntax_tree_ext
|
85
91
|
|
86
92
|
BUNDLED WITH
|
87
93
|
2.3.7
|
data/README.md
CHANGED
@@ -17,14 +17,14 @@ NodeQuery defines a NQL (node query language) and node rules to query AST nodes.
|
|
17
17
|
- [nql matches method result](#nql-matches-method-result)
|
18
18
|
- [nql matches operators](#nql-matches-operators)
|
19
19
|
- [nql matches array node attribute](#nql-matches-array-node-attribute)
|
20
|
-
- [nql matches
|
20
|
+
- [nql matches \* in attribute key](#nql-matches--in-attribute-key)
|
21
21
|
- [nql matches multiple selectors](#nql-matches-multiple-selectors)
|
22
22
|
- [Descendant combinator](#descendant-combinator)
|
23
23
|
- [Child combinator](#child-combinator)
|
24
24
|
- [Adjacent sibling combinator](#adjacent-sibling-combinator)
|
25
25
|
- [General sibling combinator](#general-sibling-combinator)
|
26
26
|
- [nql matches goto scope](#nql-matches-goto-scope)
|
27
|
-
- [nql matches :has and :
|
27
|
+
- [nql matches :has and :not\_has pseudo selector](#nql-matches-has-and-not_has-pseudo-selector)
|
28
28
|
- [nql matches :first-child and :last-child pseudo selector](#nql-matches-first-child-and-last-child-pseudo-selector)
|
29
29
|
- [nql matches multiple expressions](#nql-matches-multiple-expressions)
|
30
30
|
- [Node Rules](#node-rules)
|
@@ -115,7 +115,7 @@ It matches class node whose parent class name is Base
|
|
115
115
|
### nql matches evaluated value
|
116
116
|
|
117
117
|
```
|
118
|
-
.ivasgn[
|
118
|
+
.ivasgn[variable="@{{value}}"]
|
119
119
|
```
|
120
120
|
|
121
121
|
It matches ivasgn node whose left value equals '@' plus the evaluated value of right value.
|
@@ -271,7 +271,7 @@ It matches send node whose parent is def node.
|
|
271
271
|
#### Adjacent sibling combinator
|
272
272
|
|
273
273
|
```
|
274
|
-
.send[
|
274
|
+
.send[variable=@id] + .send
|
275
275
|
```
|
276
276
|
|
277
277
|
It matches send node only if it is immediately follows the send node whose left value is @id.
|
@@ -279,7 +279,7 @@ It matches send node only if it is immediately follows the send node whose left
|
|
279
279
|
#### General sibling combinator
|
280
280
|
|
281
281
|
```
|
282
|
-
.send[
|
282
|
+
.send[variable=@id] ~ .send
|
283
283
|
```
|
284
284
|
|
285
285
|
It matches send node only if it is follows the send node whose left value is @id.
|
@@ -323,7 +323,7 @@ It matches the last def node.
|
|
323
323
|
### nql matches multiple expressions
|
324
324
|
|
325
325
|
```
|
326
|
-
.ivasgn[
|
326
|
+
.ivasgn[variable=@id], .ivasgn[variable=@name]
|
327
327
|
```
|
328
328
|
|
329
329
|
It matches ivasgn node whose left value is either @id or @name.
|
@@ -363,7 +363,7 @@ It matches class node whose parent class name is Base
|
|
363
363
|
### rules matches evaluated value
|
364
364
|
|
365
365
|
```
|
366
|
-
{ node_type: 'ivasgn',
|
366
|
+
{ node_type: 'ivasgn', variable: '@{{value}}' }
|
367
367
|
```
|
368
368
|
|
369
369
|
It matches ivasgn node whose left value equals '@' plus the evaluated value of right value.
|
@@ -474,17 +474,18 @@ It matches def node whose arguments are id and name.
|
|
474
474
|
|
475
475
|
## Write Adapter
|
476
476
|
|
477
|
-
Different parser, like parser, will generate different AST nodes, to make NodeQuery work for them all,
|
477
|
+
Different parser, like parser, syntax_tree, will generate different AST nodes, to make NodeQuery work for them all,
|
478
478
|
we define an [Adapter](https://github.com/xinminlabs/node-query-ruby/blob/main/lib/node_query/adapter.rb) interface,
|
479
479
|
if you implement the Adapter interface, you can set it as NodeQuery's adapter.
|
480
480
|
|
481
|
-
|
482
|
-
NodeQuery.configure(adapter: ParserAdapter.new)
|
483
|
-
```
|
481
|
+
It provides 2 adapters
|
484
482
|
|
485
|
-
|
483
|
+
1. `ParserAdapter`
|
484
|
+
2. `SyntaxTreeAdapter`
|
486
485
|
|
487
|
-
|
486
|
+
```ruby
|
487
|
+
NodeQuery.configure(adapter: SyntaxTreeAdapter.new) # default is ParserAdapter
|
488
|
+
```
|
488
489
|
|
489
490
|
## Development
|
490
491
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class NodeQuery::SyntaxTreeAdapter
|
4
|
+
def is_node?(node)
|
5
|
+
node.is_a?(SyntaxTree::Node)
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_node_type(node)
|
9
|
+
node.class.name.split('::').last
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_source(node)
|
13
|
+
node.source
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_children(node)
|
17
|
+
node.child_nodes
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_siblings(node)
|
21
|
+
node.siblings
|
22
|
+
end
|
23
|
+
end
|
data/lib/node_query/adapter.rb
CHANGED
@@ -18,7 +18,10 @@ module NodeQuery::Compiler
|
|
18
18
|
def match?(node, base_node, _operator = '==')
|
19
19
|
return false unless node
|
20
20
|
|
21
|
-
@node_type.to_sym == NodeQuery.adapter.get_node_type(node) && (!@attribute_list || @attribute_list.match?(
|
21
|
+
@node_type.to_sym == NodeQuery.adapter.get_node_type(node) && (!@attribute_list || @attribute_list.match?(
|
22
|
+
node,
|
23
|
+
base_node
|
24
|
+
))
|
22
25
|
end
|
23
26
|
|
24
27
|
def to_s
|
@@ -25,7 +25,9 @@ module NodeQuery::Compiler
|
|
25
25
|
when '!='
|
26
26
|
if expected.is_a?(::Array)
|
27
27
|
!actual.is_a?(::Array) || actual.size != expected.size ||
|
28
|
-
actual.zip(expected).any? { |actual_child, expected_child|
|
28
|
+
actual.zip(expected).any? { |actual_child, expected_child|
|
29
|
+
expected_child.match?(actual_child, base_node, '!=')
|
30
|
+
}
|
29
31
|
else
|
30
32
|
!is_equal?(actual, expected)
|
31
33
|
end
|
@@ -66,7 +68,9 @@ module NodeQuery::Compiler
|
|
66
68
|
else
|
67
69
|
if expected.is_a?(::Array)
|
68
70
|
actual.is_a?(::Array) && actual.size == expected.size &&
|
69
|
-
actual.zip(expected).all? { |actual_child, expected_child|
|
71
|
+
actual.zip(expected).all? { |actual_child, expected_child|
|
72
|
+
expected_child.match?(actual_child, base_node, '==')
|
73
|
+
}
|
70
74
|
else
|
71
75
|
is_equal?(actual, expected)
|
72
76
|
end
|
@@ -110,7 +114,7 @@ module NodeQuery::Compiler
|
|
110
114
|
# Get the expected value
|
111
115
|
# @param base_node [Node] the base node for evaluated value
|
112
116
|
# @return expected value, could be integer, float, string, boolean, nil, range, and etc.
|
113
|
-
def expected_value(
|
117
|
+
def expected_value(_base_node)
|
114
118
|
@value
|
115
119
|
end
|
116
120
|
|
@@ -12,7 +12,15 @@ module NodeQuery::Compiler
|
|
12
12
|
# @param attribute_list [NodeQuery::Compiler::AttributeList] the attribute list
|
13
13
|
# @param pseudo_class [String] the pseudo class, can be <code>has</code> or <code>not_has</code>
|
14
14
|
# @param pseudo_selector [NodeQuery::Compiler::Expression] the pseudo selector
|
15
|
-
def initialize(
|
15
|
+
def initialize(
|
16
|
+
goto_scope: nil,
|
17
|
+
relationship: nil,
|
18
|
+
rest: nil,
|
19
|
+
basic_selector: nil,
|
20
|
+
position: nil,
|
21
|
+
pseudo_class: nil,
|
22
|
+
pseudo_selector: nil
|
23
|
+
)
|
16
24
|
@goto_scope = goto_scope
|
17
25
|
@relationship = relationship
|
18
26
|
@rest = rest
|
@@ -36,7 +44,10 @@ module NodeQuery::Compiler
|
|
36
44
|
return false
|
37
45
|
end
|
38
46
|
end
|
39
|
-
NodeQuery.adapter.is_node?(node) && (!@basic_selector || (operator == "!=" ? !@basic_selector.match?(
|
47
|
+
NodeQuery.adapter.is_node?(node) && (!@basic_selector || (operator == "!=" ? !@basic_selector.match?(
|
48
|
+
node,
|
49
|
+
base_node
|
50
|
+
) : @basic_selector.match?(node, base_node))) && match_pseudo_class?(node)
|
40
51
|
end
|
41
52
|
|
42
53
|
# Query nodes by the selector.
|
data/lib/node_query/helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class NodeQuery::Helper
|
4
|
-
class <<self
|
4
|
+
class << self
|
5
5
|
# Get target node by the keys.
|
6
6
|
# @param node [Node] ast node
|
7
7
|
# @param keys [String|Array] keys of child node.
|
@@ -43,7 +43,7 @@ class NodeQuery::Helper
|
|
43
43
|
# Evaluate node value.
|
44
44
|
# @example
|
45
45
|
# source code of the node is @id = id
|
46
|
-
# evaluated_node_value(node, "@{{
|
46
|
+
# evaluated_node_value(node, "@{{value}}") # => @id
|
47
47
|
# @param node [Node] ast node
|
48
48
|
# @param str [String] string to be evaluated
|
49
49
|
# @return [String] evaluated string
|
@@ -63,4 +63,4 @@ class NodeQuery::Helper
|
|
63
63
|
node.to_s
|
64
64
|
end
|
65
65
|
end
|
66
|
-
end
|
66
|
+
end
|
@@ -22,7 +22,7 @@ class NodeQuery::NodeRules
|
|
22
22
|
return match_node?(node) ? [node] : []
|
23
23
|
end
|
24
24
|
|
25
|
-
matching_nodes
|
25
|
+
matching_nodes = []
|
26
26
|
if options[:including_self] && match_node?(node)
|
27
27
|
matching_nodes.push(node)
|
28
28
|
return matching_nodes if options[:stop_at_first_match]
|
@@ -51,9 +51,10 @@ class NodeQuery::NodeRules
|
|
51
51
|
def match_node?(node)
|
52
52
|
flat_hash(@rules).keys.all? do |multi_keys|
|
53
53
|
last_key = multi_keys.last
|
54
|
-
actual =
|
55
|
-
|
56
|
-
|
54
|
+
actual =
|
55
|
+
KEYWORDS.include?(last_key) ?
|
56
|
+
NodeQuery::Helper.get_target_node(node, multi_keys[0...-1].join('.')) :
|
57
|
+
NodeQuery::Helper.get_target_node(node, multi_keys.join('.'))
|
57
58
|
expected = expected_value(@rules, multi_keys)
|
58
59
|
expected = NodeQuery::Helper.evaluate_node_value(node, expected) if expected.is_a?(String)
|
59
60
|
case last_key
|
@@ -131,9 +132,9 @@ class NodeQuery::NodeRules
|
|
131
132
|
actual == expected
|
132
133
|
end
|
133
134
|
when TrueClass
|
134
|
-
:true == actual
|
135
|
+
:true == actual&.type
|
135
136
|
when FalseClass
|
136
|
-
:false == actual
|
137
|
+
:false == actual&.type
|
137
138
|
when Parser::AST::Node
|
138
139
|
actual == expected
|
139
140
|
else
|
data/lib/node_query/version.rb
CHANGED
data/lib/node_query.rb
CHANGED
@@ -8,7 +8,8 @@ class NodeQuery
|
|
8
8
|
class MethodNotSupported < StandardError; end
|
9
9
|
|
10
10
|
autoload :Adapter, "node_query/adapter"
|
11
|
-
autoload :ParserAdapter, "node_query/
|
11
|
+
autoload :ParserAdapter, "node_query/adapter/parser"
|
12
|
+
autoload :SyntaxTreeAdapter, "node_query/adapter/syntax_tree"
|
12
13
|
autoload :Compiler, "node_query/compiler"
|
13
14
|
autoload :Helper, "node_query/helper"
|
14
15
|
autoload :NodeRules, "node_query/node_rules"
|
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.
|
4
|
+
version: 1.13.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-
|
11
|
+
date: 2023-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ast node query language
|
14
14
|
email:
|
@@ -26,6 +26,8 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- lib/node_query.rb
|
28
28
|
- lib/node_query/adapter.rb
|
29
|
+
- lib/node_query/adapter/parser.rb
|
30
|
+
- lib/node_query/adapter/syntax_tree.rb
|
29
31
|
- lib/node_query/compiler.rb
|
30
32
|
- lib/node_query/compiler/array_value.rb
|
31
33
|
- lib/node_query/compiler/attribute.rb
|
@@ -47,7 +49,6 @@ files:
|
|
47
49
|
- lib/node_query/compiler/symbol.rb
|
48
50
|
- lib/node_query/helper.rb
|
49
51
|
- lib/node_query/node_rules.rb
|
50
|
-
- lib/node_query/parser_adapter.rb
|
51
52
|
- lib/node_query/version.rb
|
52
53
|
- lib/node_query_lexer.rex
|
53
54
|
- lib/node_query_lexer.rex.rb
|
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
78
|
- !ruby/object:Gem::Version
|
78
79
|
version: '0'
|
79
80
|
requirements: []
|
80
|
-
rubygems_version: 3.4.
|
81
|
+
rubygems_version: 3.4.10
|
81
82
|
signing_key:
|
82
83
|
specification_version: 4
|
83
84
|
summary: ast node query language
|
File without changes
|