Skip to content

Commit 8d0a517

Browse files
committed
avoid unnecessary parens around ternary operators
1 parent 2a86d32 commit 8d0a517

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

lib/ruby2js/converter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Converter < Serializer
2828
:=== => :'!=='
2929
}
3030

31-
GROUP_OPERATORS = [:begin, :dstr, :dsym, :and, :or, :casgn]
31+
GROUP_OPERATORS = [:begin, :dstr, :dsym, :and, :or, :casgn, :if]
3232

3333
VASGN = [:cvasgn, :ivasgn, :gvasgn, :lvasgn]
3434

lib/ruby2js/converter/if.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class Converter
5555
end
5656
else
5757
else_block ||= s(:nil)
58-
put '('; parse condition; put ' ? '; parse then_block
59-
put ' : '; parse else_block; put ')'
58+
parse condition; put ' ? '; parse then_block
59+
put ' : '; parse else_block
6060
end
6161
end
6262
end

spec/transliteration_spec.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ def to_js( string, opts={} )
114114

115115
it "should parse ternary operator" do
116116
to_js( 'x = true ? true : false').
117-
must_equal "var x = (true ? true : false)"
117+
must_equal "var x = true ? true : false"
118118
to_js( 'x = (true if y)').
119-
must_equal "var x = (y ? true : null)"
119+
must_equal "var x = y ? true : null"
120120
to_js( 'x = (true unless y)').
121-
must_equal "var x = (!y ? true : null)"
121+
must_equal "var x = !y ? true : null"
122+
to_js( 'x = a + (b ? 1 : 0)').
123+
must_equal "var x = a + (b ? 1 : 0)"
122124
end
123125
end
124126

spec/vue_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def to_js_fn(string)
389389

390390
it "should handle mixed strings and a conditional value" do
391391
to_js( 'class Foo<Vue; def render; _a.b class: ("c" if d); end; end' ).
392-
must_include '$h("a", {class: ["b", (d ? "c" : null)]})'
392+
must_include '$h("a", {class: ["b", d ? "c" : null]})'
393393
end
394394

395395
it "should handle only a value" do

0 commit comments

Comments
 (0)