syntax_tree_ext 0.7.2 → 0.8.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 +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +15 -10
- data/lib/syntax_tree_ext/parent_node_ext.rb +35 -0
- data/lib/syntax_tree_ext/source_ext.rb +34 -0
- data/lib/syntax_tree_ext/version.rb +1 -1
- data/lib/syntax_tree_ext.rb +43 -87
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c33e697b9e1cdb6dd491d93f8dc78482ac0a358e1ecb2127a9f377d940df901c
|
4
|
+
data.tar.gz: 07f73e482d4740e1bfa13cd9d293accebe5e0fcb52374335c5f1d756bf5c6591
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e6dd31f3504dcca0ac4d70d0282ee7ab4d56523c31f2394ef16f5908abb17cccc2933e5d18207833f615d43d56eeebf9e3576e931a9ee63d442d2fdf45a9340
|
7
|
+
data.tar.gz: 0bff0422c3e75dd21dec2de6d0d991ec71c18d5a974cc1a72ccc2ba854fd49e4a8ab42994fc7c6290e90f5867ad62c96a566f5a7deddaa84497d88849a73cf69
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.8.0 (2024-04-07)
|
4
|
+
|
5
|
+
* Abstract `syntax_tree_ext/parent_node_ext`
|
6
|
+
* Abstract `syntax_tree_ext/source_ext`
|
7
|
+
* Inject hash helper methods only to `HashLiteral` and `BareAssocHash`
|
8
|
+
|
3
9
|
## 0.7.2 (2024-02-17)
|
4
10
|
|
5
11
|
* Use `Hash#filter` instead of `Hash#reject`
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,9 @@
|
|
1
1
|
# SyntaxTreeExt
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://github.com/synvert-hq/syntax_tree_ext/actions/workflows/main.yml/badge.svg)](https://github.com/synvert-hq/syntax_tree_ext/actions/workflows/main.yml)
|
4
|
+
[![Gem Version](https://img.shields.io/gem/v/syntax_tree_ext.svg)](https://rubygems.org/gems/syntax_tree_ext)
|
4
5
|
|
5
|
-
It
|
6
|
-
|
7
|
-
```ruby
|
8
|
-
# node is a HashLiteral node
|
9
|
-
node.foo_assoc # get the assoc node of hash foo key
|
10
|
-
node.foo_value # get the value node of the hash foo key
|
11
|
-
node.foo_source # get the source of the value node of the hash foo key
|
12
|
-
```
|
6
|
+
It adds some helpers syntax_tree node.
|
13
7
|
|
14
8
|
## Installation
|
15
9
|
|
@@ -26,6 +20,17 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
26
20
|
```ruby
|
27
21
|
require 'syntax_tree'
|
28
22
|
require 'syntax_tree_ext'
|
23
|
+
|
24
|
+
# node is a HashLiteral or BareAssocHash node
|
25
|
+
node.foo_assoc # get the assoc node of hash foo key
|
26
|
+
node.foo_value # get the value node of the hash foo key
|
27
|
+
node.foo_source # get the source of the value node of the hash foo key
|
28
|
+
node.keys # get key nodes of the hash node
|
29
|
+
node.values # get value nodes of the hash node
|
30
|
+
|
31
|
+
# all nodes
|
32
|
+
node.to_value # get the value of the node, like `true`, `false`, `nil`, `1`, `"foo"`
|
33
|
+
node.to_source # get the source code of the node
|
29
34
|
```
|
30
35
|
|
31
36
|
## Development
|
@@ -36,4 +41,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
36
41
|
|
37
42
|
## Contributing
|
38
43
|
|
39
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
44
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/synvert-hq/syntax_tree_ext.
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SyntaxTree
|
4
|
+
class << self
|
5
|
+
alias_method :original_parse, :parse
|
6
|
+
|
7
|
+
def parse(source)
|
8
|
+
node = original_parse(source)
|
9
|
+
node.set_parent_node(source)
|
10
|
+
node
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Node
|
15
|
+
attr_accessor :parent_node
|
16
|
+
|
17
|
+
def set_parent_node_and_source(source)
|
18
|
+
self.deconstruct_keys([]).filter { |key, _value| ![:location, :comments].include?(key) }.values.each do |child_node|
|
19
|
+
if child_node.is_a?(Array)
|
20
|
+
child_node.each do |child_child_node|
|
21
|
+
next unless child_child_node.is_a?(Node)
|
22
|
+
|
23
|
+
child_child_node.parent_node = self
|
24
|
+
child_child_node.set_parent_node(source)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
next unless child_node.is_a?(Node)
|
29
|
+
|
30
|
+
child_node.parent_node = self
|
31
|
+
child_node.set_parent_node(source)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SyntaxTree
|
4
|
+
class << self
|
5
|
+
alias_method :original_parse, :parse
|
6
|
+
|
7
|
+
def parse(source)
|
8
|
+
node = original_parse(source)
|
9
|
+
node.set_source(source)
|
10
|
+
node
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Node
|
15
|
+
attr_accessor :source
|
16
|
+
|
17
|
+
def set_source(source)
|
18
|
+
self.source = source
|
19
|
+
self.deconstruct_keys([]).filter { |key, _value| ![:location, :comments].include?(key) }.values.each do |child_node|
|
20
|
+
if child_node.is_a?(Array)
|
21
|
+
child_node.each do |child_child_node|
|
22
|
+
next unless child_child_node.is_a?(Node)
|
23
|
+
|
24
|
+
child_child_node.set_source(source)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
next unless child_node.is_a?(Node)
|
29
|
+
|
30
|
+
child_node.set_source(source)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/syntax_tree_ext.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require_relative "syntax_tree_ext/version"
|
4
4
|
|
5
5
|
require 'syntax_tree'
|
6
|
+
require_relative "syntax_tree_ext/source_ext"
|
6
7
|
|
7
8
|
module SyntaxTreeExt
|
8
9
|
class Error < StandardError; end
|
@@ -10,101 +11,25 @@ module SyntaxTreeExt
|
|
10
11
|
end
|
11
12
|
|
12
13
|
module SyntaxTree
|
13
|
-
|
14
|
-
alias_method :original_parse, :parse
|
15
|
-
|
16
|
-
def parse(source)
|
17
|
-
node = original_parse(source)
|
18
|
-
node.set_parent_node_and_source(source)
|
19
|
-
node
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class Node
|
24
|
-
attr_accessor :parent_node, :source
|
25
|
-
|
26
|
-
def set_parent_node_and_source(source)
|
27
|
-
self.source = source
|
28
|
-
self.deconstruct_keys([]).filter { |key, _value| ![:location, :comments].include?(key) }.values.each do |child_node|
|
29
|
-
if child_node.is_a?(Array)
|
30
|
-
child_node.each do |child_child_node|
|
31
|
-
next unless child_child_node.is_a?(Node)
|
32
|
-
|
33
|
-
child_child_node.parent_node = self
|
34
|
-
child_child_node.set_parent_node_and_source(source)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
next unless child_node.is_a?(Node)
|
39
|
-
|
40
|
-
child_node.parent_node = self
|
41
|
-
child_node.set_parent_node_and_source(source)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
14
|
+
module HashNodeExt
|
45
15
|
def keys
|
46
|
-
|
47
|
-
assocs.map(&:key)
|
48
|
-
else
|
49
|
-
raise MethodNotSupported, "keys is not supported for #{self}"
|
50
|
-
end
|
16
|
+
assocs.map(&:key)
|
51
17
|
end
|
52
18
|
|
53
19
|
def values
|
54
|
-
|
55
|
-
assocs.map(&:value)
|
56
|
-
else
|
57
|
-
raise MethodNotSupported, "values is not supported for #{self}"
|
58
|
-
end
|
20
|
+
assocs.map(&:value)
|
59
21
|
end
|
60
22
|
|
61
23
|
def hash_assoc(key)
|
62
|
-
|
63
|
-
assocs.find { |assoc_node| assoc_node.key.to_value == key }
|
64
|
-
else
|
65
|
-
raise MethodNotSupported, "hash_assoc is not supported for #{self}"
|
66
|
-
end
|
24
|
+
assocs.find { |assoc_node| assoc_node.key.to_value == key }
|
67
25
|
end
|
68
26
|
|
69
27
|
def hash_value(key)
|
70
|
-
|
71
|
-
assocs.find { |assoc_node| assoc_node.key.to_value == key }&.value
|
72
|
-
else
|
73
|
-
raise MethodNotSupported, "hash_value is not supported for #{self}"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def to_value
|
78
|
-
case self
|
79
|
-
when SymbolLiteral
|
80
|
-
value.value.to_sym
|
81
|
-
when StringLiteral
|
82
|
-
parts.map(&:to_value).join
|
83
|
-
when FloatLiteral
|
84
|
-
value.to_f
|
85
|
-
when Int
|
86
|
-
value.to_i
|
87
|
-
when Kw
|
88
|
-
value == 'true'
|
89
|
-
when VarRef
|
90
|
-
value.to_value
|
91
|
-
when Const, Label, TStringContent, Ident
|
92
|
-
value
|
93
|
-
when ArrayLiteral
|
94
|
-
contents ? contents.parts.map { |part| part.to_value } : []
|
95
|
-
else
|
96
|
-
self
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def to_source
|
101
|
-
source[location.start_char...location.end_char]
|
28
|
+
assocs.find { |assoc_node| assoc_node.key.to_value == key }&.value
|
102
29
|
end
|
103
30
|
|
104
31
|
# Respond key value and source for hash node
|
105
32
|
def method_missing(method_name, *args, &block)
|
106
|
-
return super unless respond_to_assocs?
|
107
|
-
|
108
33
|
if method_name.to_s.end_with?('_assoc')
|
109
34
|
key = method_name.to_s[0..-7]
|
110
35
|
return assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
@@ -120,8 +45,6 @@ module SyntaxTree
|
|
120
45
|
end
|
121
46
|
|
122
47
|
def respond_to_missing?(method_name, *args)
|
123
|
-
return super unless respond_to_assocs?
|
124
|
-
|
125
48
|
if method_name.to_s.end_with?('_assoc')
|
126
49
|
key = method_name[0..-7]
|
127
50
|
return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
@@ -138,13 +61,46 @@ module SyntaxTree
|
|
138
61
|
|
139
62
|
private
|
140
63
|
|
141
|
-
def respond_to_assocs?
|
142
|
-
is_a?(HashLiteral) || is_a?(BareAssocHash)
|
143
|
-
end
|
144
|
-
|
145
64
|
def assoc_key_equal?(assoc, key)
|
146
65
|
assoc_key = assoc.key.to_value.to_s
|
147
66
|
assoc_key.end_with?(':') ? assoc_key == "#{key}:" : assoc_key == key
|
148
67
|
end
|
149
68
|
end
|
69
|
+
|
70
|
+
class HashLiteral
|
71
|
+
include HashNodeExt
|
72
|
+
end
|
73
|
+
|
74
|
+
class BareAssocHash
|
75
|
+
include HashNodeExt
|
76
|
+
end
|
77
|
+
|
78
|
+
class Node
|
79
|
+
def to_value
|
80
|
+
case self
|
81
|
+
when SymbolLiteral
|
82
|
+
value.value.to_sym
|
83
|
+
when StringLiteral
|
84
|
+
parts.map(&:to_value).join
|
85
|
+
when FloatLiteral
|
86
|
+
value.to_f
|
87
|
+
when Int
|
88
|
+
value.to_i
|
89
|
+
when Kw
|
90
|
+
value == 'true'
|
91
|
+
when VarRef
|
92
|
+
value.to_value
|
93
|
+
when Const, Label, TStringContent, Ident
|
94
|
+
value
|
95
|
+
when ArrayLiteral
|
96
|
+
contents ? contents.parts.map { |part| part.to_value } : []
|
97
|
+
else
|
98
|
+
self
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def to_source
|
103
|
+
source[location.start_char...location.end_char]
|
104
|
+
end
|
105
|
+
end
|
150
106
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syntax_tree_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.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: syntax_tree
|
@@ -38,14 +38,16 @@ files:
|
|
38
38
|
- README.md
|
39
39
|
- Rakefile
|
40
40
|
- lib/syntax_tree_ext.rb
|
41
|
+
- lib/syntax_tree_ext/parent_node_ext.rb
|
42
|
+
- lib/syntax_tree_ext/source_ext.rb
|
41
43
|
- lib/syntax_tree_ext/version.rb
|
42
44
|
- sig/syntax_tree_ext.rbs
|
43
|
-
homepage: https://github.com/
|
45
|
+
homepage: https://github.com/synvert-hq/syntax_tree_ext
|
44
46
|
licenses: []
|
45
47
|
metadata:
|
46
|
-
homepage_uri: https://github.com/
|
47
|
-
source_code_uri: https://github.com/
|
48
|
-
changelog_uri: https://github.com/
|
48
|
+
homepage_uri: https://github.com/synvert-hq/syntax_tree_ext
|
49
|
+
source_code_uri: https://github.com/synvert-hq/syntax_tree_ext
|
50
|
+
changelog_uri: https://github.com/synvert-hq/syntax_tree_ext/blob/main/CHANGELOG
|
49
51
|
post_install_message:
|
50
52
|
rdoc_options: []
|
51
53
|
require_paths:
|