ast 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e3398752b5d18c8a0d8d92566ddb560f35391bc
4
- data.tar.gz: 1d08b307064106e701044d3f640c5cac033148c6
3
+ metadata.gz: 6fd328f8be0444eea7ebadfba831f783f87defe1
4
+ data.tar.gz: ee6204555aec2a37f32aa1db514a7a43dd959d77
5
5
  SHA512:
6
- metadata.gz: 2dfe3fe6af1cbfc6c4ce5dde12c8c285c10a4249779e8442ecbe1a9a73301dcb7e5b6028ccf4e7a4c7686b353762475103f85c1216f7ee4a69a55d8d8b6a3169
7
- data.tar.gz: 752a64d60bf8b65c20782343a653958458f224fb4b124d258e1edef86e2c7acbd95fc9c5eebabbd7ebe4ced860e6fd0280412d180a896aabe8320ec3dfdc7a30
6
+ metadata.gz: 32abdd8a6d4ba69e9433c307d9048358d39423c621a8af819649f9cc712ada40cdfd21289549a5478d1db68c695eab51b4edb7bae5966381284735ebc9cd65bc
7
+ data.tar.gz: a680513585f7b67ef99b25e621cf5bbea81183d2412e3c237d756cc82faa21a32775ea050251bf893aeeba813699a27104705f6032fb94c7e1fbb38f212c983d
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # AST
2
2
 
3
- [![Build Status](https://travis-ci.org/whitequark/ast.png?branch=master)](https://travis-ci.org/whitequark/ast)
4
- [![Code Climate](https://codeclimate.com/github/whitequark/ast.png)](https://codeclimate.com/github/whitequark/ast)
5
- [![Coverage Status](https://coveralls.io/repos/whitequark/ast/badge.png?branch=master)](https://coveralls.io/r/whitequark/ast)
3
+ [![Build Status](https://travis-ci.org/whitequark/ast.svg?branch=master)](https://travis-ci.org/whitequark/ast)
4
+ [![Code Climate](https://codeclimate.com/github/whitequark/ast.svg)](https://codeclimate.com/github/whitequark/ast)
5
+ [![Coverage Status](https://coveralls.io/repos/whitequark/ast/badge.svg?branch=master)](https://coveralls.io/r/whitequark/ast)
6
6
 
7
7
  AST is a small library for working with immutable abstract syntax trees.
8
8
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ast'
3
- s.version = '2.1.0'
3
+ s.version = '2.2.0'
4
4
  s.license = 'MIT'
5
5
  s.authors = ["whitequark"]
6
6
  s.email = ["whitequark@whitequark.org"]
@@ -80,7 +80,7 @@ module AST
80
80
  # By default, each entry in the `properties` hash is assigned to
81
81
  # an instance variable in this instance of Node. A subclass should define
82
82
  # attribute readers for such variables. The values passed in the hash
83
- # are not frozen or whitelisted; such behavior can also be implemented\
83
+ # are not frozen or whitelisted; such behavior can also be implemented
84
84
  # by subclassing Node and overriding this method.
85
85
  #
86
86
  # @return [nil]
@@ -104,6 +104,7 @@ module AST
104
104
  def dup
105
105
  self
106
106
  end
107
+ alias :clone :dup
107
108
 
108
109
  # Returns a new instance of Node where non-nil arguments replace the
109
110
  # corresponding fields of `self`.
@@ -166,13 +167,6 @@ module AST
166
167
 
167
168
  alias << append
168
169
 
169
- # Converts `self` to a concise s-expression, omitting any children.
170
- #
171
- # @return [String]
172
- def to_s
173
- "(#{fancy_type} ...)"
174
- end
175
-
176
170
  # Returns {#children}. This is very useful in order to decompose nodes
177
171
  # concisely. For example:
178
172
  #
@@ -211,7 +205,34 @@ module AST
211
205
 
212
206
  sexp
213
207
  end
214
- alias :inspect :to_sexp
208
+
209
+ alias to_s to_sexp
210
+
211
+ # Converts `self` to a s-expression ruby string.
212
+ # The code return will recreate the node, using the sexp module s()
213
+ #
214
+ # @param [Integer] indent Base indentation level.
215
+ # @return [String]
216
+ def inspect(indent=0)
217
+ indented = " " * indent
218
+ sexp = "#{indented}s(:#{@type}"
219
+
220
+ first_node_child = children.index do |child|
221
+ child.is_a?(Node) || child.is_a?(Array)
222
+ end || children.count
223
+
224
+ children.each_with_index do |child, idx|
225
+ if child.is_a?(Node) && idx >= first_node_child
226
+ sexp << ",\n#{child.inspect(indent + 1)}"
227
+ else
228
+ sexp << ", #{child.inspect}"
229
+ end
230
+ end
231
+
232
+ sexp << ")"
233
+
234
+ sexp
235
+ end
215
236
 
216
237
  # @return [AST::Node] self
217
238
  def to_ast
@@ -30,6 +30,10 @@ describe AST::Node do
30
30
  @node.dup.should.equal? @node
31
31
  end
32
32
 
33
+ it 'should return self when cloning' do
34
+ @node.clone.should.equal? @node
35
+ end
36
+
33
37
  it 'should return an updated node, but only if needed' do
34
38
  @node.updated().should.be.identical_to @node
35
39
  @node.updated(:node).should.be.identical_to @node
@@ -51,11 +55,6 @@ describe AST::Node do
51
55
  updated.meta.should.equal 'other_value'
52
56
  end
53
57
 
54
- it 'should use fancy type in to_s' do
55
- node = AST::Node.new(:ast_node)
56
- node.to_s.should.equal '(ast-node ...)'
57
- end
58
-
59
58
  it 'should format to_sexp correctly' do
60
59
  AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).to_sexp.should.equal '(a :sym [1, 2])'
61
60
  AST::Node.new(:a, [ :sym, @node ]).to_sexp.should.equal "(a :sym\n (node 0 1))"
@@ -64,6 +63,28 @@ describe AST::Node do
64
63
  ]).to_sexp.should.equal "(a :sym\n (b\n (node 0 1)\n (node 0 1)))"
65
64
  end
66
65
 
66
+ it 'should format to_s correctly' do
67
+ AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).to_s.should.equal '(a :sym [1, 2])'
68
+ AST::Node.new(:a, [ :sym, @node ]).to_s.should.equal "(a :sym\n (node 0 1))"
69
+ AST::Node.new(:a, [ :sym,
70
+ AST::Node.new(:b, [ @node, @node ])
71
+ ]).to_s.should.equal "(a :sym\n (b\n (node 0 1)\n (node 0 1)))"
72
+ end
73
+
74
+ it 'should format inspect correctly' do
75
+ AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).inspect.should.equal "s(:a, :sym, [1, 2])"
76
+ AST::Node.new(:a, [ :sym,
77
+ AST::Node.new(:b, [ @node, @node ])
78
+ ]).inspect.should.equal "s(:a, :sym,\n s(:b,\n s(:node, 0, 1),\n s(:node, 0, 1)))"
79
+ end
80
+
81
+ it 'should recreate inspect output' do
82
+ simple_node = AST::Node.new(:a, [ :sym, [ 1, 2 ] ])
83
+ eval(simple_node.inspect).should.equal simple_node
84
+ complex_node = s(:a , :sym, s(:b, s(:node, 0, 1), s(:node, 0, 1)))
85
+ eval(complex_node.inspect).should.equal complex_node
86
+ end
87
+
67
88
  it 'should return self in to_ast' do
68
89
  @node.to_ast.should.be.identical_to @node
69
90
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ast
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - whitequark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-03 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake