parser_node_ext 1.2.2 → 1.3.1
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 +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 +9 -83
- 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: 50a45e7affbc066c77b4b1ced9b3b9e95f44dda27d545406a81835c3272eada5
|
4
|
+
data.tar.gz: 20b65501f3e92ea387a1a3f28ec67627cd3c6ea48228a7988c309730a68d380d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69a851d7c5e73797708dd354c5b9f145d6d6c525b234b5be9c2d7c9e79ef690117a1c8c9bdb068a428a0796403d67a91104dd327551c004f42c6168d7e41d92a
|
7
|
+
data.tar.gz: 660151c08259100c51194e374c8db31ff66fb2acaf76a9f8c41c594029de8ef775f2a4bdc352718860224c31531fca0e63b6cefdb7e85bc1511e1ce26e86a462
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.3.1 (2024-04-18)
|
4
|
+
|
5
|
+
* Remove `.key?` method
|
6
|
+
|
7
|
+
## 1.3.0 (2024-04-07)
|
8
|
+
|
9
|
+
* Add github actions
|
10
|
+
* Remove `siblings` method
|
11
|
+
* Abstract `parser_node_ext/parent_node_ext`
|
12
|
+
|
3
13
|
## 1.2.2 (2024-02-10)
|
4
14
|
|
5
15
|
* Remove `to_hash` extend
|
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
|
@@ -352,21 +319,6 @@ module ParserNodeExt
|
|
352
319
|
end
|
353
320
|
end
|
354
321
|
|
355
|
-
# Check if :hash node contains specified key.
|
356
|
-
# @example
|
357
|
-
# node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
|
358
|
-
# node.key?(:foo) # true
|
359
|
-
# @param [Symbol, String] key value.
|
360
|
-
# @return [Boolean] true if specified key exists.
|
361
|
-
# @raise [MethodNotSupported] if calls on other node.
|
362
|
-
def key?(key)
|
363
|
-
if %i[hash hash_pattern].include?(type)
|
364
|
-
pairs.any? { |pair_node| pair_node.key.to_value == key }
|
365
|
-
else
|
366
|
-
raise MethodNotSupported, "key? is not supported for #{self}"
|
367
|
-
end
|
368
|
-
end
|
369
|
-
|
370
322
|
# Get :hash pair node according to specified key.
|
371
323
|
# @example
|
372
324
|
# node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
|
@@ -455,20 +407,20 @@ module ParserNodeExt
|
|
455
407
|
return children.send(method_name, *args, &block)
|
456
408
|
elsif :hash == type && method_name.to_s.end_with?('_pair')
|
457
409
|
key = method_name.to_s[0..-6]
|
458
|
-
return hash_pair(key.to_sym) if
|
459
|
-
return hash_pair(key.to_s) if
|
410
|
+
return hash_pair(key.to_sym) if keys.map(&:to_value).include?(key.to_sym)
|
411
|
+
return hash_pair(key.to_s) if keys.map(&:to_value).include?(key.to_s)
|
460
412
|
|
461
413
|
return nil
|
462
414
|
elsif :hash == type && method_name.to_s.end_with?('_value')
|
463
415
|
key = method_name.to_s[0..-7]
|
464
|
-
return hash_value(key.to_sym) if
|
465
|
-
return hash_value(key.to_s) if
|
416
|
+
return hash_value(key.to_sym) if keys.map(&:to_value).include?(key.to_sym)
|
417
|
+
return hash_value(key.to_s) if keys.map(&:to_value).include?(key.to_s)
|
466
418
|
|
467
419
|
return nil
|
468
420
|
elsif :hash == type && method_name.to_s.end_with?('_source')
|
469
421
|
key = method_name.to_s[0..-8]
|
470
|
-
return hash_value(key.to_sym)&.to_source if
|
471
|
-
return hash_value(key.to_s)&.to_source if
|
422
|
+
return hash_value(key.to_sym)&.to_source if keys.map(&:to_value).include?(key.to_sym)
|
423
|
+
return hash_value(key.to_s)&.to_source if keys.map(&:to_value).include?(key.to_s)
|
472
424
|
|
473
425
|
return ''
|
474
426
|
end
|
@@ -481,13 +433,13 @@ module ParserNodeExt
|
|
481
433
|
return true
|
482
434
|
elsif :hash == type && method_name.to_s.end_with?('_pair')
|
483
435
|
key = method_name.to_s[0..-6]
|
484
|
-
return
|
436
|
+
return keys.map(&:to_value).include?(key.to_sym) || keys.map(&:to_value).include?(key.to_s)
|
485
437
|
elsif :hash == type && method_name.to_s.end_with?('_value')
|
486
438
|
key = method_name.to_s[0..-7]
|
487
|
-
return
|
439
|
+
return keys.map(&:to_value).include?(key.to_sym) || keys.map(&:to_value).include?(key.to_s)
|
488
440
|
elsif :hash == type && method_name.to_s.end_with?('_source')
|
489
441
|
key = method_name.to_s[0..-8]
|
490
|
-
return
|
442
|
+
return keys.map(&:to_value).include?(key.to_sym) || keys.map(&:to_value).include?(key.to_s)
|
491
443
|
end
|
492
444
|
|
493
445
|
super
|
@@ -497,32 +449,6 @@ module ParserNodeExt
|
|
497
449
|
end
|
498
450
|
|
499
451
|
# 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
452
|
class Parser::AST::Node
|
527
453
|
include ParserNodeExt
|
528
454
|
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.1
|
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-18 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:
|