parser_node_ext 1.2.2 → 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 +6 -0
- data/Gemfile.lock +1 -1
- 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 -59
- data/parser_node_ext.gemspec +3 -3
- metadata +7 -6
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
data/Gemfile.lock
CHANGED
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
|
@@ -497,32 +464,6 @@ module ParserNodeExt
|
|
497
464
|
end
|
498
465
|
|
499
466
|
# Extend Parser::AST::Node.
|
500
|
-
# {https://github.com/whitequark/parser/blob/master/lib/parser/ast/node.rb}
|
501
|
-
#
|
502
|
-
# Rules
|
503
|
-
#
|
504
|
-
# Synvert compares ast nodes with key / value pairs, each ast node has
|
505
|
-
# multiple attributes, e.g. +receiver+, +message+ and +arguments+, it
|
506
|
-
# matches only when all of key / value pairs match.
|
507
|
-
#
|
508
|
-
# +type: 'send', message: :include, arguments: ['FactoryGirl::Syntax::Methods']+
|
509
|
-
#
|
510
|
-
# Synvert does comparison based on the value type
|
511
|
-
#
|
512
|
-
# 1. if value is a symbol, then compares ast node value as symbol, e.g. +message: :include+
|
513
|
-
# 2. if value is a string, then compares ast node original source code, e.g. +name: 'Synvert::Application'+
|
514
|
-
# 3. if value is a regexp, then compares ast node original source code, e.g. +message: /find_all_by_/+
|
515
|
-
# 4. if value is an array, then compares each ast node, e.g. +arguments: ['FactoryGirl::Syntax::Methods']+
|
516
|
-
# 5. if value is nil, then check if ast node is nil, e.g. +arguments: [nil]+
|
517
|
-
# 6. if value is true or false, then check if ast node is :true or :false, e.g. +arguments: [false]+
|
518
|
-
# 7. if value is ast, then compare ast node directly, e.g. +to_ast: Parser::CurrentRuby.parse("self.class.serialized_attributes")+
|
519
|
-
#
|
520
|
-
# It can also compare nested key / value pairs, like
|
521
|
-
#
|
522
|
-
# +type: 'send', receiver: { type: 'send', receiver: { type: 'send', message: 'config' }, message: 'active_record' }, message: 'identity_map='+
|
523
|
-
#
|
524
|
-
# Source Code to Ast Node
|
525
|
-
# {https://playground.synvert.net/ruby}
|
526
467
|
class Parser::AST::Node
|
527
468
|
include ParserNodeExt
|
528
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: 2024-
|
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:
|