sexp2ruby 0.0.3 → 0.0.4

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: 0a0f686a975b672835e136dc5ebb67483658f4ed
4
- data.tar.gz: b5f1433cb9d3356d37628587b7a81f7c554c7593
3
+ metadata.gz: e85bdfc910bdea5229b1808b0751e8d75277199d
4
+ data.tar.gz: 8d280d6b355022806feba17979c97a3d273ca996
5
5
  SHA512:
6
- metadata.gz: a3d6da332f6456aeb084a610e1ec44ed1797a89b85e05fd23812e6a00c57de6f74a82226ae64e91c9cb55eea83d504accd54f62ccc815330734a42d9ac52b9fe
7
- data.tar.gz: aadb54bb3269ad6936e99c0cbf3af50b1226ac20714498ccdc72f728208cccd9d1f5a1cb7ac5cf31a2a7229b40ae5789a0184369be08dbe2258e7dce7219faac
6
+ metadata.gz: 701652fb216fd8b7bf4df826d177675b932ecfd270c65a5d982e28f0c94fa33ab14380ec352c6aeaa5c9b072a84172e87b78fb3c9cc97e094582109dc6d862c0
7
+ data.tar.gz: ef682dec8338256d2552fc14164442384c76a07c47cf52249dbe785c6cd977c3db618b34e21ffa8f80f6177f3ec2afc7c8a3655bb5ca227b18c44766a308a455
@@ -1,9 +1,11 @@
1
1
  language: ruby
2
+ cache: bundler
2
3
  rvm:
3
4
  - 1.9.3
4
5
  - 2.0.0
5
6
  - 2.1.6
6
7
  - 2.2.2
8
+ - 2.3.1
7
9
  script: bundle exec rspec --require spec_helper
8
10
  notifications:
9
11
  email: false
@@ -4,10 +4,25 @@ Change Log
4
4
  This project follows [semver 2.0.0][1] and the recommendations
5
5
  of [keepachangelog.com][2].
6
6
 
7
- 0.0.3
8
- -----
7
+ ## 0.0.4
9
8
 
10
- ### Changed
9
+ ### Breaking Changes
10
+
11
+ None
12
+
13
+ ### Added
14
+
15
+ None
16
+
17
+ ### Fixed
18
+
19
+ - Iter whitespace: an empty body should render as `{}`, not `{ }`.
20
+ - A bug where a one-line iter whose call matches no_paren_methods produced
21
+ invalid output. Example: `a B {}` is now `a B do\nend`
22
+
23
+ ## 0.0.3
24
+
25
+ ### Breaking Changes
11
26
  - Configuration
12
27
  - Changed default of `:hash_syntax` from `:ruby18` to `:ruby19`
13
28
 
@@ -16,14 +31,12 @@ of [keepachangelog.com][2].
16
31
  - Configuration
17
32
  - Added `:no_paren_methods` omit argument parentheses
18
33
 
19
- 0.0.2
20
- -----
34
+ ## 0.0.2
21
35
 
22
36
  ### Changed
23
37
  - Normalize block arguments. See [ruby_parser PR 189][3] (Ryan Davis)
24
38
 
25
- 0.0.1
26
- -----
39
+ ## 0.0.1
27
40
 
28
41
  Initial version. Just claiming the gem name.
29
42
 
@@ -1,10 +1,44 @@
1
1
  module Sexp2Ruby
2
2
  module Node
3
+ # A method call.
4
+ #
5
+ # Examples:
6
+ #
7
+ # ```
8
+ # a
9
+ # s(:call, nil, :a)
10
+ #
11
+ # A.b
12
+ # s(:call, s(:const, :A), :b)
13
+ #
14
+ # a(b)
15
+ # s(:call, nil, :a, s(:call, nil, :b))
16
+ #
17
+ # a(b, c: d, &e)
18
+ # s(
19
+ # :call,
20
+ # nil,
21
+ # :a,
22
+ # s(:call, nil, :b),
23
+ # s(:hash, s(:lit, :c), s(:call, nil, :d)),
24
+ # s(:block_pass, s(:call, nil, :e))
25
+ # )
26
+ #
27
+ # a = :a; a.b
28
+ # s(
29
+ # :block,
30
+ # s(:lasgn, :a, s(:lit, :a)),
31
+ # s(:call, s(:lvar, :a), :b)
32
+ # )
33
+ # ```
3
34
  class Call < Base
4
-
5
35
  # binary operation messages
6
36
  BINARY = [:<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :**, :'!=']
7
37
 
38
+ def arguments?(exp)
39
+ exp.length > 2 # 1. receiver, 2. method name, 3+ arguments
40
+ end
41
+
8
42
  def to_s(exp)
9
43
  receiver_node_type = exp.first.nil? ? nil : exp.first.first
10
44
  receiver = process exp.shift
@@ -1,9 +1,35 @@
1
1
  module Sexp2Ruby
2
2
  module Node
3
+ # An `Iter`, AFAICT, is a `Call` with a block.
4
+ #
5
+ # Example:
6
+ #
7
+ # ```
8
+ # # ruby
9
+ # derp(foo) { |bar| herp }
10
+ #
11
+ # # sexp
12
+ # s(
13
+ # :iter,
14
+ # s(:call, nil, :a, s(:call, nil, :b)),
15
+ # s(:args, :c),
16
+ # s(:call, nil, :d)
17
+ # )
18
+ # ```
19
+ #
3
20
  class Iter < Base
4
21
  def to_s(exp)
5
- iter = process exp.shift
22
+ call_sexp = exp.shift
23
+
24
+ # Process the `Call`. The Sexp is not consumed here (it is cloned)
25
+ # because we will need to refer to it later, when determining which
26
+ # block delimiters to use (brackets vs. do/end).
27
+ iter = process(call_sexp.deep_clone)
28
+
29
+ # The block arguments (as opposed to the `Call` arguments)
6
30
  args = exp.shift
31
+
32
+ # The body of the block.
7
33
  body = exp.empty? ? nil : process(exp.shift)
8
34
 
9
35
  args = case args
@@ -25,15 +51,16 @@ module Sexp2Ruby
25
51
  result = []
26
52
  result << "#{iter} {"
27
53
  result << args
28
- if body
29
- result << " #{body.strip} "
30
- else
31
- result << ' '
32
- end
54
+ result << (body ? " #{body.strip} " : "")
33
55
  result << "}"
34
56
  result = result.join
35
- return result if result !~ /\n/ and result.size < LINE_LENGTH
36
57
 
58
+ # Can we squeeze the block onto the same line as the call?
59
+ if same_line_bracket_block?(result, iter, call_sexp.deep_clone)
60
+ return result
61
+ end
62
+
63
+ # We will not try to squeeze the block onto one line.
37
64
  result = []
38
65
  result << "#{iter} #{b}"
39
66
  result << args
@@ -45,6 +72,22 @@ module Sexp2Ruby
45
72
  result << e
46
73
  result.join
47
74
  end
75
+
76
+ private
77
+
78
+ # Given `iter` (a rendered `Call`), and `result`, should we try to
79
+ # squeeze the body (`result`) onto the same line as `iter`? There are two
80
+ # considerations.
81
+ #
82
+ # - Syntactic - did the `Call` parenthesize its arguments?
83
+ # - Stylistic - would it exceed the line length
84
+ #
85
+ def same_line_bracket_block?(result, iter, call_sexp)
86
+ call_sexp.shift # discard the sexp_type, as the processor would
87
+ syntactic = !Call.new(processor).arguments?(call_sexp) || iter.end_with?(")")
88
+ stylistic = result !~ /\n/ && result.size < LINE_LENGTH
89
+ syntactic && stylistic
90
+ end
48
91
  end
49
92
  end
50
93
  end
@@ -1,3 +1,3 @@
1
1
  module Sexp2Ruby
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -26,8 +26,8 @@ It is a fork of ruby2ruby with slightly different goals.
26
26
  spec.add_runtime_dependency "activesupport", "~> 4.2"
27
27
  spec.add_runtime_dependency "sexp_processor", "~> 4.6"
28
28
 
29
- spec.add_development_dependency "rspec-core", "~> 3.2"
30
- spec.add_development_dependency "rspec-expectations", "~> 3.2"
31
- spec.add_development_dependency "rspec-mocks", "~> 3.2"
29
+ spec.add_development_dependency "rspec-core", "~> 3.4"
30
+ spec.add_development_dependency "rspec-expectations", "~> 3.4"
31
+ spec.add_development_dependency "rspec-mocks", "~> 3.4"
32
32
  spec.add_development_dependency "ruby_parser", "~> 3.7"
33
33
  end
@@ -149,6 +149,25 @@ module Sexp2Ruby
149
149
  end
150
150
  end
151
151
 
152
+ context "iter" do
153
+ context "empty body" do
154
+ it "does not put spaces inside brackets" do
155
+ inp = s(:iter, s(:call, nil, :x, s(:lit, 1)), 0)
156
+ out = "x(1) {}"
157
+ compare(inp, out, processor)
158
+ end
159
+ end
160
+
161
+ context "when configured to omit parentheses for method x" do
162
+ it "uses do/end because brackets would be invalid" do
163
+ pro = described_class.new(no_paren_methods: [:x])
164
+ inp = s(:iter, s(:call, nil, :x, s(:lit, 1)), 0)
165
+ out = "x 1 do\nend"
166
+ compare(inp, out, pro)
167
+ end
168
+ end
169
+ end
170
+
152
171
  context "hash" do
153
172
  it "ruby19_one_pair" do
154
173
  inp = s(:hash, s(:lit, :foo), s(:str, "bar"))
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.3
4
+ version: 0.0.4
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-11 00:00:00.000000000 Z
12
+ date: 2016-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -45,42 +45,42 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '3.2'
48
+ version: '3.4'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '3.2'
55
+ version: '3.4'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rspec-expectations
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '3.2'
62
+ version: '3.4'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '3.2'
69
+ version: '3.4'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rspec-mocks
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: '3.2'
76
+ version: '3.4'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: '3.2'
83
+ version: '3.4'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: ruby_parser
86
86
  requirement: !ruby/object:Gem::Requirement
@@ -224,7 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
224
  version: '0'
225
225
  requirements: []
226
226
  rubyforge_project:
227
- rubygems_version: 2.4.5
227
+ rubygems_version: 2.5.1
228
228
  signing_key:
229
229
  specification_version: 4
230
230
  summary: Generates ruby from RubyParser S-expressions