sexp2ruby 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -1
- data/README.md +20 -3
- data/lib/sexp2ruby/node/base.rb +1 -0
- data/lib/sexp2ruby/node/call.rb +18 -4
- data/lib/sexp2ruby/node/hash.rb +1 -0
- data/lib/sexp2ruby/processor.rb +10 -4
- data/lib/sexp2ruby/version.rb +1 -1
- data/spec/lib/processor_spec.rb +102 -82
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a0f686a975b672835e136dc5ebb67483658f4ed
|
4
|
+
data.tar.gz: b5f1433cb9d3356d37628587b7a81f7c554c7593
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3d6da332f6456aeb084a610e1ec44ed1797a89b85e05fd23812e6a00c57de6f74a82226ae64e91c9cb55eea83d504accd54f62ccc815330734a42d9ac52b9fe
|
7
|
+
data.tar.gz: aadb54bb3269ad6936e99c0cbf3af50b1226ac20714498ccdc72f728208cccd9d1f5a1cb7ac5cf31a2a7229b40ae5789a0184369be08dbe2258e7dce7219faac
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,32 @@
|
|
1
|
-
|
1
|
+
Change Log
|
2
|
+
==========
|
3
|
+
|
4
|
+
This project follows [semver 2.0.0][1] and the recommendations
|
5
|
+
of [keepachangelog.com][2].
|
6
|
+
|
7
|
+
0.0.3
|
8
|
+
-----
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
- Configuration
|
12
|
+
- Changed default of `:hash_syntax` from `:ruby18` to `:ruby19`
|
13
|
+
|
14
|
+
### Added
|
15
|
+
- Do not wrap subhash in parentheses
|
16
|
+
- Configuration
|
17
|
+
- Added `:no_paren_methods` omit argument parentheses
|
18
|
+
|
19
|
+
0.0.2
|
20
|
+
-----
|
21
|
+
|
22
|
+
### Changed
|
23
|
+
- Normalize block arguments. See [ruby_parser PR 189][3] (Ryan Davis)
|
24
|
+
|
25
|
+
0.0.1
|
26
|
+
-----
|
2
27
|
|
3
28
|
Initial version. Just claiming the gem name.
|
29
|
+
|
30
|
+
[1]: http://semver.org/spec/v2.0.0.html
|
31
|
+
[2]: http://keepachangelog.com/
|
32
|
+
[3]: https://github.com/seattlerb/ruby_parser/pull/189
|
data/README.md
CHANGED
@@ -6,10 +6,10 @@ sexp2ruby
|
|
6
6
|
`sexp2ruby` generates ruby from RubyParser S-expressions.
|
7
7
|
It is a fork of [ruby2ruby][1] with slightly different goals.
|
8
8
|
|
9
|
-
-
|
9
|
+
- Follows [ruby-style-guide][3] where possible
|
10
10
|
- Prefers OO design over performance
|
11
11
|
- Drops support for ruby 1.8.7
|
12
|
-
- Depends on [activesupport][4]
|
12
|
+
- Depends on (a small subset of) [activesupport][4]
|
13
13
|
- Uses bundler instead of hoe
|
14
14
|
- Uses rspec instead of minitest
|
15
15
|
|
@@ -29,7 +29,24 @@ Sexp2Ruby::Processor.new.process(sexp.deep_clone)
|
|
29
29
|
```
|
30
30
|
|
31
31
|
As with all `SexpProcessor`s, `Sexp2Ruby#process` destroys its input,
|
32
|
-
so `deep_clone` if you need to preserve it.
|
32
|
+
so `deep_clone` as shown above if you need to preserve it.
|
33
|
+
|
34
|
+
Configuration
|
35
|
+
-------------
|
36
|
+
|
37
|
+
Configure output by passing options to `Sexp2Ruby::Processor.new`:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
hash = s(:hash, s(:lit, :a), s(:lit, 1))
|
41
|
+
Sexp2Ruby::Processor.new(hash_syntax: :ruby18).process
|
42
|
+
# => "{ :a => 1 }"
|
43
|
+
Sexp2Ruby::Processor.new(hash_syntax: :ruby19).process
|
44
|
+
# => "{ a: 1 }"
|
45
|
+
```
|
46
|
+
|
47
|
+
- `:hash_syntax` - either `:ruby18` or `:ruby19`. Default is `:ruby19`.
|
48
|
+
- `:no_paren_methods` - an array of symbols, these methods
|
49
|
+
will omit argument parentheses. Default is `[]`.
|
33
50
|
|
34
51
|
[1]: https://github.com/seattlerb/ruby2ruby
|
35
52
|
[2]: http://docs.seattlerb.org/ruby2ruby
|
data/lib/sexp2ruby/node/base.rb
CHANGED
data/lib/sexp2ruby/node/call.rb
CHANGED
@@ -56,15 +56,29 @@ module Sexp2Ruby
|
|
56
56
|
when :"+@" then
|
57
57
|
"+#{receiver}"
|
58
58
|
else
|
59
|
-
args
|
60
|
-
|
61
|
-
receiver = "#{receiver}." if receiver
|
62
|
-
|
59
|
+
args = arguments(args, name)
|
60
|
+
receiver = "#{receiver}." if receiver
|
63
61
|
"#{receiver}#{name}#{args}"
|
64
62
|
end
|
65
63
|
ensure
|
66
64
|
call_pop
|
67
65
|
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def arguments(args, name)
|
70
|
+
if args.empty?
|
71
|
+
""
|
72
|
+
else
|
73
|
+
fmt = argument_parentheses?(name) ? "(%s)" : " %s"
|
74
|
+
str = "#{args.join(', ')}"
|
75
|
+
fmt % str
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def argument_parentheses?(name)
|
80
|
+
!no_paren_methods.include?(name.to_sym)
|
81
|
+
end
|
68
82
|
end
|
69
83
|
end
|
70
84
|
end
|
data/lib/sexp2ruby/node/hash.rb
CHANGED
data/lib/sexp2ruby/processor.rb
CHANGED
@@ -29,7 +29,10 @@ module Sexp2Ruby
|
|
29
29
|
HASH_SYNTAXES = [:ruby18, :ruby19]
|
30
30
|
RUBY_19_HASH_KEY = /\A[a-z][_a-zA-Z0-9]+\Z/
|
31
31
|
|
32
|
-
CONSTRUCTOR_OPTIONS = [
|
32
|
+
CONSTRUCTOR_OPTIONS = [
|
33
|
+
:hash_syntax,
|
34
|
+
:no_paren_methods
|
35
|
+
]
|
33
36
|
|
34
37
|
NODES = [
|
35
38
|
:alias,
|
@@ -118,16 +121,19 @@ module Sexp2Ruby
|
|
118
121
|
:zsuper
|
119
122
|
]
|
120
123
|
|
121
|
-
attr_reader :hash_syntax, :indent_lvl
|
124
|
+
attr_reader :hash_syntax, :indent_lvl, :no_paren_methods
|
122
125
|
|
123
126
|
# Options:
|
124
127
|
#
|
125
|
-
# - `:hash_syntax` - either `:ruby18` or `:ruby19
|
128
|
+
# - `:hash_syntax` - either `:ruby18` or `:ruby19`. Default is `:ruby19`.
|
129
|
+
# - `:no_paren_methods` - an array of symbols, these methods
|
130
|
+
# will omit argument parentheses. Default is `[]`.
|
126
131
|
|
127
132
|
def initialize(option = {})
|
128
133
|
super()
|
129
134
|
check_option_keys(option)
|
130
|
-
@hash_syntax = extract_option(HASH_SYNTAXES, option[:hash_syntax], :
|
135
|
+
@hash_syntax = extract_option(HASH_SYNTAXES, option[:hash_syntax], :ruby19)
|
136
|
+
@no_paren_methods = option[:no_paren_methods] || []
|
131
137
|
@indent_lvl = " "
|
132
138
|
self.auto_shift_type = true
|
133
139
|
self.strict = true
|
data/lib/sexp2ruby/version.rb
CHANGED
data/spec/lib/processor_spec.rb
CHANGED
@@ -54,6 +54,101 @@ module Sexp2Ruby
|
|
54
54
|
end
|
55
55
|
|
56
56
|
describe "#process" do
|
57
|
+
context "call" do
|
58
|
+
it "kwsplat" do
|
59
|
+
inn = s(:call, nil, :test_splat, s(:hash, s(:kwsplat, s(:call, nil, :testing))))
|
60
|
+
out = "test_splat(**testing)"
|
61
|
+
compare(inn, out, processor)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "arg_assoc_kwsplat" do
|
65
|
+
inn = s(:call, nil, :f,
|
66
|
+
s(:lit, 1),
|
67
|
+
s(:hash, s(:lit, :kw), s(:lit, 2), s(:kwsplat, s(:lit, 3))))
|
68
|
+
out = "f(1, kw: 2, **3)"
|
69
|
+
|
70
|
+
compare(inn, out, processor)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "kwsplat_x" do
|
74
|
+
inn = s(:call, nil, :a, s(:hash, s(:kwsplat, s(:lit, 1))))
|
75
|
+
out = "a(**1)"
|
76
|
+
|
77
|
+
compare(inn, out, processor)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "self_index" do
|
81
|
+
compare(s(:call, nil, :[], s(:lit, 42)), "self[42]", processor)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "self_index_equals" do
|
85
|
+
inp = s(:attrasgn, s(:self), :[]=, s(:lit, 42), s(:lit, 24))
|
86
|
+
compare(inp, "self[42] = 24", processor)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "self_index_equals_array" do
|
90
|
+
inp = s(:attrasgn, s(:self), :[]=, s(:lit, 1), s(:lit, 2), s(:lit, 3))
|
91
|
+
compare(inp, "self[1, 2] = 3", processor)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "arglist_hash_first" do
|
95
|
+
inn = s(:call, nil, :method,
|
96
|
+
s(:hash, s(:lit, :a), s(:lit, 1)),
|
97
|
+
s(:call, nil, :b))
|
98
|
+
out = "method({ :a => 1 }, b)"
|
99
|
+
|
100
|
+
compare(inn, out, processor)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "arglist_hash_first_last" do
|
104
|
+
inn = s(:call, nil, :method,
|
105
|
+
s(:hash, s(:lit, :a), s(:lit, 1)),
|
106
|
+
s(:call, nil, :b),
|
107
|
+
s(:hash, s(:lit, :c), s(:lit, 1)))
|
108
|
+
out = "method({ :a => 1 }, b, :c => 1)"
|
109
|
+
|
110
|
+
compare(inn, out, processor)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "arglist_hash_last" do
|
114
|
+
inn = s(:call, nil, :method,
|
115
|
+
s(:call, nil, :b),
|
116
|
+
s(:hash, s(:lit, :a), s(:lit, 1)))
|
117
|
+
out = "method(b, :a => 1)"
|
118
|
+
|
119
|
+
compare(inn, out, processor)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "arglist_if" do
|
123
|
+
inn = s(:call,
|
124
|
+
s(:call, nil, :a),
|
125
|
+
:+,
|
126
|
+
s(:if,
|
127
|
+
s(:call, nil, :b),
|
128
|
+
s(:call, nil, :c),
|
129
|
+
s(:call, nil, :d)))
|
130
|
+
|
131
|
+
out = "(a + (b ? (c) : (d)))"
|
132
|
+
compare(inn, out, processor)
|
133
|
+
end
|
134
|
+
|
135
|
+
context "when configured to omit parentheses for method x" do
|
136
|
+
let(:processor) { described_class.new(no_paren_methods: [:x]) }
|
137
|
+
|
138
|
+
it "omits parentheses for method x" do
|
139
|
+
inp = s(:call, nil, :x, s(:lit, 1))
|
140
|
+
out = "x 1"
|
141
|
+
compare(inp, out, processor)
|
142
|
+
end
|
143
|
+
|
144
|
+
it "includes parentheses for method y" do
|
145
|
+
inp = s(:call, nil, :y, s(:lit, 1))
|
146
|
+
out = "y(1)"
|
147
|
+
compare(inp, out, processor)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
57
152
|
context "hash" do
|
58
153
|
it "ruby19_one_pair" do
|
59
154
|
inp = s(:hash, s(:lit, :foo), s(:str, "bar"))
|
@@ -112,6 +207,12 @@ module Sexp2Ruby
|
|
112
207
|
compare(inp, out, processor)
|
113
208
|
end
|
114
209
|
|
210
|
+
it "does not wrap hash in parens" do
|
211
|
+
inp = s(:hash, s(:lit, :a), s(:hash, s(:lit, :b), s(:nil)))
|
212
|
+
out = "{ :a => { :b => nil } }"
|
213
|
+
compare(inp, out, processor)
|
214
|
+
end
|
215
|
+
|
115
216
|
it "wraps method call with block (iter) in parens" do
|
116
217
|
iter = s(:iter, s(:call, nil, :foo), 0, s(:str, "bar"))
|
117
218
|
inp = s(:hash, s(:lit, :k), iter)
|
@@ -199,28 +300,6 @@ module Sexp2Ruby
|
|
199
300
|
compare(inp, '/blah\/blah/', processor, false, /blah\/blah/)
|
200
301
|
end
|
201
302
|
|
202
|
-
it "call_kwsplat" do
|
203
|
-
inn = s(:call, nil, :test_splat, s(:hash, s(:kwsplat, s(:call, nil, :testing))))
|
204
|
-
out = "test_splat(**testing)"
|
205
|
-
compare(inn, out, processor)
|
206
|
-
end
|
207
|
-
|
208
|
-
it "call_arg_assoc_kwsplat" do
|
209
|
-
inn = s(:call, nil, :f,
|
210
|
-
s(:lit, 1),
|
211
|
-
s(:hash, s(:lit, :kw), s(:lit, 2), s(:kwsplat, s(:lit, 3))))
|
212
|
-
out = "f(1, :kw => 2, **3)"
|
213
|
-
|
214
|
-
compare(inn, out, processor)
|
215
|
-
end
|
216
|
-
|
217
|
-
it "call_kwsplat_x" do
|
218
|
-
inn = s(:call, nil, :a, s(:hash, s(:kwsplat, s(:lit, 1))))
|
219
|
-
out = "a(**1)"
|
220
|
-
|
221
|
-
compare(inn, out, processor)
|
222
|
-
end
|
223
|
-
|
224
303
|
it "defn_kwargs" do
|
225
304
|
inn = s(:defn, :initialize,
|
226
305
|
s(:args, :arg, s(:kwarg, :keyword, s(:nil)), :"**args"),
|
@@ -242,61 +321,6 @@ module Sexp2Ruby
|
|
242
321
|
compare(inn, out, processor)
|
243
322
|
end
|
244
323
|
|
245
|
-
it "call_self_index" do
|
246
|
-
compare(s(:call, nil, :[], s(:lit, 42)), "self[42]", processor)
|
247
|
-
end
|
248
|
-
|
249
|
-
it "call_self_index_equals" do
|
250
|
-
inp = s(:attrasgn, s(:self), :[]=, s(:lit, 42), s(:lit, 24))
|
251
|
-
compare(inp, "self[42] = 24", processor)
|
252
|
-
end
|
253
|
-
|
254
|
-
it "call_self_index_equals_array" do
|
255
|
-
inp = s(:attrasgn, s(:self), :[]=, s(:lit, 1), s(:lit, 2), s(:lit, 3))
|
256
|
-
compare(inp, "self[1, 2] = 3", processor)
|
257
|
-
end
|
258
|
-
|
259
|
-
it "call_arglist_hash_first" do
|
260
|
-
inn = s(:call, nil, :method,
|
261
|
-
s(:hash, s(:lit, :a), s(:lit, 1)),
|
262
|
-
s(:call, nil, :b))
|
263
|
-
out = "method({ :a => 1 }, b)"
|
264
|
-
|
265
|
-
compare(inn, out, processor)
|
266
|
-
end
|
267
|
-
|
268
|
-
it "call_arglist_hash_first_last" do
|
269
|
-
inn = s(:call, nil, :method,
|
270
|
-
s(:hash, s(:lit, :a), s(:lit, 1)),
|
271
|
-
s(:call, nil, :b),
|
272
|
-
s(:hash, s(:lit, :c), s(:lit, 1)))
|
273
|
-
out = "method({ :a => 1 }, b, :c => 1)"
|
274
|
-
|
275
|
-
compare(inn, out, processor)
|
276
|
-
end
|
277
|
-
|
278
|
-
it "call_arglist_hash_last" do
|
279
|
-
inn = s(:call, nil, :method,
|
280
|
-
s(:call, nil, :b),
|
281
|
-
s(:hash, s(:lit, :a), s(:lit, 1)))
|
282
|
-
out = "method(b, :a => 1)"
|
283
|
-
|
284
|
-
compare(inn, out, processor)
|
285
|
-
end
|
286
|
-
|
287
|
-
it "call_arglist_if" do
|
288
|
-
inn = s(:call,
|
289
|
-
s(:call, nil, :a),
|
290
|
-
:+,
|
291
|
-
s(:if,
|
292
|
-
s(:call, nil, :b),
|
293
|
-
s(:call, nil, :c),
|
294
|
-
s(:call, nil, :d)))
|
295
|
-
|
296
|
-
out = "(a + (b ? (c) : (d)))"
|
297
|
-
compare(inn, out, processor)
|
298
|
-
end
|
299
|
-
|
300
324
|
it "defn_kwsplat" do
|
301
325
|
inn = s(:defn, :test, s(:args, :"**testing"), s(:nil))
|
302
326
|
out = "def test(**testing)\n # do nothing\nend"
|
@@ -446,17 +470,13 @@ module Sexp2Ruby
|
|
446
470
|
end
|
447
471
|
|
448
472
|
it "call_binary_call_with_hash_arg" do
|
449
|
-
# if 42
|
450
|
-
# args << {:key => 24}
|
451
|
-
# end
|
452
|
-
|
453
473
|
inn = s(:if, s(:lit, 42),
|
454
474
|
s(:call, s(:call, nil, :args),
|
455
475
|
:<<,
|
456
476
|
s(:hash, s(:lit, :key), s(:lit, 24))),
|
457
477
|
nil)
|
458
478
|
|
459
|
-
out = "(args << { :
|
479
|
+
out = "(args << { key: 24 }) if 42"
|
460
480
|
|
461
481
|
compare(inn, out, processor)
|
462
482
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sexp2ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Davis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-06-
|
12
|
+
date: 2015-06-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|