Skip to content

Commit 82dc9b7

Browse files
committed
Introduce plugin/disable_ternary
This will prevent the automatic conversion of `if ... else` to ternary expressions.
1 parent e1d89ee commit 82dc9b7

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ To register plugins, define a file somewhere in your load path named `syntax_tre
658658

659659
* `plugin/single_quotes` - This will change all of your string literals to use single quotes instead of the default double quotes.
660660
* `plugin/trailing_comma` - This will put trailing commas into multiline array literals, hash literals, and method calls that can support trailing commas.
661+
* `plugin/disable_ternary` - This will prevent the automatic conversion of `if ... else` to ternary expressions.
661662

662663
If you're using Syntax Tree as a library, you can require those files directly or manually pass those options to the formatter initializer through the `SyntaxTree::Formatter::Options` class.
663664

lib/syntax_tree/formatter.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ def initialize(version)
2121
# that folks have become entrenched in their ways, we decided to provide a
2222
# small amount of configurability.
2323
class Options
24-
attr_reader :quote, :trailing_comma, :target_ruby_version
24+
attr_reader :quote,
25+
:trailing_comma,
26+
:disable_ternary,
27+
:target_ruby_version
2528

2629
def initialize(
2730
quote: :default,
2831
trailing_comma: :default,
32+
disable_ternary: :default,
2933
target_ruby_version: :default
3034
)
3135
@quote =
@@ -50,6 +54,17 @@ def initialize(
5054
trailing_comma
5155
end
5256

57+
@disable_ternary =
58+
if disable_ternary == :default
59+
# We ship with a disable ternary plugin that will define this
60+
# constant. That constant is responsible for determining the default
61+
# disable ternary value. If it's defined, then we default to true.
62+
# Otherwise we default to false.
63+
defined?(DISABLE_TERNARY)
64+
else
65+
disable_ternary
66+
end
67+
5368
@target_ruby_version =
5469
if target_ruby_version == :default
5570
# The default target Ruby version is the current version of Ruby.
@@ -69,8 +84,9 @@ def initialize(
6984

7085
# These options are overridden in plugins to we need to make sure they are
7186
# available here.
72-
attr_reader :quote, :trailing_comma, :target_ruby_version
87+
attr_reader :quote, :trailing_comma, :disable_ternary, :target_ruby_version
7388
alias trailing_comma? trailing_comma
89+
alias disable_ternary? disable_ternary
7490

7591
def initialize(source, *args, options: Options.new)
7692
super(*args)
@@ -81,6 +97,7 @@ def initialize(source, *args, options: Options.new)
8197
# Memoizing these values to make access faster.
8298
@quote = options.quote
8399
@trailing_comma = options.trailing_comma
100+
@disable_ternary = options.disable_ternary
84101
@target_ruby_version = options.target_ruby_version
85102
end
86103

lib/syntax_tree/node.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6154,6 +6154,7 @@ module Ternaryable
61546154
class << self
61556155
def call(q, node)
61566156
return false if ENV["STREE_FAST_FORMAT"]
6157+
return false if q.disable_ternary?
61576158

61586159
# If this is a conditional inside of a parentheses as the only content,
61596160
# then we don't want to transform it into a ternary. Presumably the user
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module SyntaxTree
4+
class Formatter
5+
DISABLE_TERNARY = true
6+
end
7+
end

test/plugin/disable_ternary_test.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
module SyntaxTree
6+
class DisableTernaryTest < Minitest::Test
7+
def test_short_if_else_unchanged
8+
assert_format(<<~RUBY)
9+
if true
10+
1
11+
else
12+
2
13+
end
14+
RUBY
15+
end
16+
17+
def test_short_ternary_unchanged
18+
assert_format("true ? 1 : 2\n")
19+
end
20+
21+
private
22+
23+
def assert_format(expected, source = expected)
24+
options = Formatter::Options.new(disable_ternary: true)
25+
formatter = Formatter.new(source, [], options: options)
26+
SyntaxTree.parse(source).format(formatter)
27+
28+
formatter.flush
29+
assert_equal(expected, formatter.output.join)
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)