parser_node_ext 1.2.1 → 1.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 +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +3 -3
- data/README.md +9 -0
- data/lib/parser_node_ext/parent_node_ext.rb +29 -0
- data/lib/parser_node_ext/version.rb +1 -1
- data/lib/parser_node_ext.rb +0 -81
- data/parser_node_ext.gemspec +3 -3
- metadata +8 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3f58dd28166500b9ae0882d5a629d272f4ad7713538e2f7aeb2a0218c77a359a
|
|
4
|
+
data.tar.gz: 3947994cf6a5669d2d37971af8dae83836a429c9715aa2c2e29a7efd3c74863a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3e2462943a0c6db7a0e2d95a9ce9e967deb19c5d2a7ce71d44b68c7db5cb74bbf398d2ae487ea0db8145371d4c7ec17910c5889d675d9201b19e81ae7ab041d4
|
|
7
|
+
data.tar.gz: f8b0f07ff32914d11bb1a3cc70e1a6b2a19501bdb08d795334c738780fa4cfa016a18e86e3014f80b86953eba7e06e44dcfca4d67680e7bb0e35e52f9402f9e0
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 1.3.0 (2024-04-07)
|
|
4
|
+
|
|
5
|
+
* Add github actions
|
|
6
|
+
* Remove `siblings` method
|
|
7
|
+
* Abstract `parser_node_ext/parent_node_ext`
|
|
8
|
+
|
|
9
|
+
## 1.2.2 (2024-02-10)
|
|
10
|
+
|
|
11
|
+
* Remove `to_hash` extend
|
|
12
|
+
|
|
3
13
|
## 1.2.1 (2023-10-01)
|
|
4
14
|
|
|
5
15
|
* Do not handle `irange` and `erange` in `to_value`
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
parser_node_ext (1.
|
|
4
|
+
parser_node_ext (1.3.0)
|
|
5
5
|
parser
|
|
6
6
|
|
|
7
7
|
GEM
|
|
@@ -9,10 +9,10 @@ GEM
|
|
|
9
9
|
specs:
|
|
10
10
|
ast (2.4.2)
|
|
11
11
|
diff-lcs (1.5.0)
|
|
12
|
-
parser (3.
|
|
12
|
+
parser (3.3.0.5)
|
|
13
13
|
ast (~> 2.4.1)
|
|
14
14
|
racc
|
|
15
|
-
racc (1.7.
|
|
15
|
+
racc (1.7.3)
|
|
16
16
|
rake (13.0.6)
|
|
17
17
|
rspec (3.11.0)
|
|
18
18
|
rspec-core (~> 3.11.0)
|
data/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# ParserNodeExt
|
|
2
2
|
|
|
3
|
+
[](https://github.com/synvert-hq/parser_node_ext/actions/workflows/main.yml)
|
|
4
|
+
[](https://rubygems.org/gems/parser_node_ext)
|
|
5
|
+
|
|
3
6
|
It assigns names to the child nodes of the [parser](https://rubygems.org/gems/parser).
|
|
4
7
|
|
|
5
8
|
```ruby
|
|
@@ -16,6 +19,12 @@ It also adds some helpers
|
|
|
16
19
|
node.foo_pair # get the pair node of hash foo key
|
|
17
20
|
node.foo_value # get the value node of the hash foo key
|
|
18
21
|
node.foo_source # get the source of the value node of the hash foo key
|
|
22
|
+
node.keys # get key nodes of the hash node
|
|
23
|
+
node.values # get value nodes of the hash node
|
|
24
|
+
|
|
25
|
+
# all nodes
|
|
26
|
+
node.to_value # get the value of the node, like `true`, `false`, `nil`, `1`, `"foo"`
|
|
27
|
+
node.to_source # get the source code of the node
|
|
19
28
|
```
|
|
20
29
|
|
|
21
30
|
## Installation
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Parser::AST::Node
|
|
4
|
+
# Initialize a Node.
|
|
5
|
+
#
|
|
6
|
+
# It extends {Parser::AST::Node} and set parent for its child nodes.
|
|
7
|
+
def initialize(type, children = [], properties = {})
|
|
8
|
+
@mutable_attributes = {}
|
|
9
|
+
super
|
|
10
|
+
# children could be nil for s(:array)
|
|
11
|
+
Array(children).each do |child_node|
|
|
12
|
+
if child_node.is_a?(Parser::AST::Node)
|
|
13
|
+
child_node.parent = self
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Get the parent node.
|
|
19
|
+
# @return [Parser::AST::Node] parent node.
|
|
20
|
+
def parent
|
|
21
|
+
@mutable_attributes[:parent]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Set the parent node.
|
|
25
|
+
# @param node [Parser::AST::Node] parent node.
|
|
26
|
+
def parent=(node)
|
|
27
|
+
@mutable_attributes[:parent] = node
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/parser_node_ext.rb
CHANGED
|
@@ -119,39 +119,6 @@ module ParserNodeExt
|
|
|
119
119
|
|
|
120
120
|
def self.included(base)
|
|
121
121
|
base.class_eval do
|
|
122
|
-
# Initialize a Node.
|
|
123
|
-
#
|
|
124
|
-
# It extends {Parser::AST::Node} and set parent for its child nodes.
|
|
125
|
-
def initialize(type, children = [], properties = {})
|
|
126
|
-
@mutable_attributes = {}
|
|
127
|
-
super
|
|
128
|
-
# children could be nil for s(:array)
|
|
129
|
-
Array(children).each do |child_node|
|
|
130
|
-
if child_node.is_a?(Parser::AST::Node)
|
|
131
|
-
child_node.parent = self
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
# Get the parent node.
|
|
137
|
-
# @return [Parser::AST::Node] parent node.
|
|
138
|
-
def parent
|
|
139
|
-
@mutable_attributes[:parent]
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
# Set the parent node.
|
|
143
|
-
# @param node [Parser::AST::Node] parent node.
|
|
144
|
-
def parent=(node)
|
|
145
|
-
@mutable_attributes[:parent] = node
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
# Get the sibling nodes.
|
|
149
|
-
# @return [Array<Parser::AST::Node>] sibling nodes.
|
|
150
|
-
def siblings
|
|
151
|
-
index = parent.children.index(self)
|
|
152
|
-
parent.children[index + 1..]
|
|
153
|
-
end
|
|
154
|
-
|
|
155
122
|
# Dyamically defined method based on const TYPE_CHILDREN.
|
|
156
123
|
TYPE_CHILDREN.values.flatten.uniq.each do |method_name|
|
|
157
124
|
define_method(method_name) do
|
|
@@ -445,28 +412,6 @@ module ParserNodeExt
|
|
|
445
412
|
loc.expression&.source
|
|
446
413
|
end
|
|
447
414
|
|
|
448
|
-
# Convert node to a hash, so that it can be converted to a json.
|
|
449
|
-
def to_hash
|
|
450
|
-
result = { node_type: type }
|
|
451
|
-
if TYPE_CHILDREN[type]
|
|
452
|
-
TYPE_CHILDREN[type].each do |key|
|
|
453
|
-
value = send(key)
|
|
454
|
-
result[key] =
|
|
455
|
-
case value
|
|
456
|
-
when Array
|
|
457
|
-
value.map { |v| v.respond_to?(:to_hash) ? v.to_hash : v }
|
|
458
|
-
when Parser::AST::Node
|
|
459
|
-
value.to_hash
|
|
460
|
-
else
|
|
461
|
-
value
|
|
462
|
-
end
|
|
463
|
-
end
|
|
464
|
-
else
|
|
465
|
-
result[:children] = children.map { |c| c.respond_to?(:to_hash) ? c.to_hash : c }
|
|
466
|
-
end
|
|
467
|
-
result
|
|
468
|
-
end
|
|
469
|
-
|
|
470
415
|
# Respond key value and source for hash node, e.g.
|
|
471
416
|
# @example
|
|
472
417
|
# node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
|
|
@@ -519,32 +464,6 @@ module ParserNodeExt
|
|
|
519
464
|
end
|
|
520
465
|
|
|
521
466
|
# Extend Parser::AST::Node.
|
|
522
|
-
# {https://github.com/whitequark/parser/blob/master/lib/parser/ast/node.rb}
|
|
523
|
-
#
|
|
524
|
-
# Rules
|
|
525
|
-
#
|
|
526
|
-
# Synvert compares ast nodes with key / value pairs, each ast node has
|
|
527
|
-
# multiple attributes, e.g. +receiver+, +message+ and +arguments+, it
|
|
528
|
-
# matches only when all of key / value pairs match.
|
|
529
|
-
#
|
|
530
|
-
# +type: 'send', message: :include, arguments: ['FactoryGirl::Syntax::Methods']+
|
|
531
|
-
#
|
|
532
|
-
# Synvert does comparison based on the value type
|
|
533
|
-
#
|
|
534
|
-
# 1. if value is a symbol, then compares ast node value as symbol, e.g. +message: :include+
|
|
535
|
-
# 2. if value is a string, then compares ast node original source code, e.g. +name: 'Synvert::Application'+
|
|
536
|
-
# 3. if value is a regexp, then compares ast node original source code, e.g. +message: /find_all_by_/+
|
|
537
|
-
# 4. if value is an array, then compares each ast node, e.g. +arguments: ['FactoryGirl::Syntax::Methods']+
|
|
538
|
-
# 5. if value is nil, then check if ast node is nil, e.g. +arguments: [nil]+
|
|
539
|
-
# 6. if value is true or false, then check if ast node is :true or :false, e.g. +arguments: [false]+
|
|
540
|
-
# 7. if value is ast, then compare ast node directly, e.g. +to_ast: Parser::CurrentRuby.parse("self.class.serialized_attributes")+
|
|
541
|
-
#
|
|
542
|
-
# It can also compare nested key / value pairs, like
|
|
543
|
-
#
|
|
544
|
-
# +type: 'send', receiver: { type: 'send', receiver: { type: 'send', message: 'config' }, message: 'active_record' }, message: 'identity_map='+
|
|
545
|
-
#
|
|
546
|
-
# Source Code to Ast Node
|
|
547
|
-
# {https://playground.synvert.net/ruby}
|
|
548
467
|
class Parser::AST::Node
|
|
549
468
|
include ParserNodeExt
|
|
550
469
|
end
|
data/parser_node_ext.gemspec
CHANGED
|
@@ -10,12 +10,12 @@ Gem::Specification.new do |spec|
|
|
|
10
10
|
|
|
11
11
|
spec.summary = "extend parser node"
|
|
12
12
|
spec.description = "extend parser node, add parent and sibling, use meaning properties to get child node"
|
|
13
|
-
spec.homepage = "https://github.com/
|
|
13
|
+
spec.homepage = "https://github.com/synvert-hq/parser_node_ext"
|
|
14
14
|
spec.required_ruby_version = ">= 2.6.0"
|
|
15
15
|
|
|
16
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
|
-
spec.metadata["source_code_uri"] = "https://github.com/
|
|
18
|
-
spec.metadata["changelog_uri"] = "https://github.com/
|
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/synvert-hq/parser_node_ext"
|
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/synvert-hq/parser_node_ext/blob/main/CHANGELOG.md"
|
|
19
19
|
|
|
20
20
|
# Specify which files should be added to the gem when it is released.
|
|
21
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: parser_node_ext
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.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:
|
|
11
|
+
date: 2024-04-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: parser
|
|
@@ -39,14 +39,15 @@ files:
|
|
|
39
39
|
- README.md
|
|
40
40
|
- Rakefile
|
|
41
41
|
- lib/parser_node_ext.rb
|
|
42
|
+
- lib/parser_node_ext/parent_node_ext.rb
|
|
42
43
|
- lib/parser_node_ext/version.rb
|
|
43
44
|
- parser_node_ext.gemspec
|
|
44
|
-
homepage: https://github.com/
|
|
45
|
+
homepage: https://github.com/synvert-hq/parser_node_ext
|
|
45
46
|
licenses: []
|
|
46
47
|
metadata:
|
|
47
|
-
homepage_uri: https://github.com/
|
|
48
|
-
source_code_uri: https://github.com/
|
|
49
|
-
changelog_uri: https://github.com/
|
|
48
|
+
homepage_uri: https://github.com/synvert-hq/parser_node_ext
|
|
49
|
+
source_code_uri: https://github.com/synvert-hq/parser_node_ext
|
|
50
|
+
changelog_uri: https://github.com/synvert-hq/parser_node_ext/blob/main/CHANGELOG.md
|
|
50
51
|
post_install_message:
|
|
51
52
|
rdoc_options: []
|
|
52
53
|
require_paths:
|
|
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
62
63
|
- !ruby/object:Gem::Version
|
|
63
64
|
version: '0'
|
|
64
65
|
requirements: []
|
|
65
|
-
rubygems_version: 3.
|
|
66
|
+
rubygems_version: 3.5.3
|
|
66
67
|
signing_key:
|
|
67
68
|
specification_version: 4
|
|
68
69
|
summary: extend parser node
|