Skip to content

Introduce plugin/disable_ternary #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ To register plugins, define a file somewhere in your load path named `syntax_tre

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

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.

Expand Down
21 changes: 19 additions & 2 deletions lib/syntax_tree/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ def initialize(version)
# that folks have become entrenched in their ways, we decided to provide a
# small amount of configurability.
class Options
attr_reader :quote, :trailing_comma, :target_ruby_version
attr_reader :quote,
:trailing_comma,
:disable_ternary,
:target_ruby_version

def initialize(
quote: :default,
trailing_comma: :default,
disable_ternary: :default,
target_ruby_version: :default
)
@quote =
Expand All @@ -50,6 +54,17 @@ def initialize(
trailing_comma
end

@disable_ternary =
if disable_ternary == :default
# We ship with a disable ternary plugin that will define this
# constant. That constant is responsible for determining the default
# disable ternary value. If it's defined, then we default to true.
# Otherwise we default to false.
defined?(DISABLE_TERNARY)
else
disable_ternary
end

@target_ruby_version =
if target_ruby_version == :default
# The default target Ruby version is the current version of Ruby.
Expand All @@ -69,8 +84,9 @@ def initialize(

# These options are overridden in plugins to we need to make sure they are
# available here.
attr_reader :quote, :trailing_comma, :target_ruby_version
attr_reader :quote, :trailing_comma, :disable_ternary, :target_ruby_version
alias trailing_comma? trailing_comma
alias disable_ternary? disable_ternary

def initialize(source, *args, options: Options.new)
super(*args)
Expand All @@ -81,6 +97,7 @@ def initialize(source, *args, options: Options.new)
# Memoizing these values to make access faster.
@quote = options.quote
@trailing_comma = options.trailing_comma
@disable_ternary = options.disable_ternary
@target_ruby_version = options.target_ruby_version
end

Expand Down
1 change: 1 addition & 0 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6154,6 +6154,7 @@ module Ternaryable
class << self
def call(q, node)
return false if ENV["STREE_FAST_FORMAT"]
return false if q.disable_ternary?

# If this is a conditional inside of a parentheses as the only content,
# then we don't want to transform it into a ternary. Presumably the user
Expand Down
7 changes: 7 additions & 0 deletions lib/syntax_tree/plugin/disable_ternary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module SyntaxTree
class Formatter
DISABLE_TERNARY = true
end
end
32 changes: 32 additions & 0 deletions test/plugin/disable_ternary_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require_relative "../test_helper"

module SyntaxTree
class DisableTernaryTest < Minitest::Test
def test_short_if_else_unchanged
assert_format(<<~RUBY)
if true
1
else
2
end
RUBY
end

def test_short_ternary_unchanged
assert_format("true ? 1 : 2\n")
end

private

def assert_format(expected, source = expected)
options = Formatter::Options.new(disable_ternary: true)
formatter = Formatter.new(source, [], options: options)
SyntaxTree.parse(source).format(formatter)

formatter.flush
assert_equal(expected, formatter.output.join)
end
end
end