Class: SyntaxTree::Formatter::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/formatter.rb

Overview

We want to minimize as much as possible the number of options that are available in syntax tree. For the most part, if users want non-default formatting, they should override the format methods on the specific nodes themselves. However, because of some history with prettier and the fact that folks have become entrenched in their ways, we decided to provide a small amount of configurability.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quote: :default, trailing_comma: :default, disable_auto_ternary: :default, target_ruby_version: :default) ⇒ Options

Returns a new instance of Options.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/syntax_tree/formatter.rb', line 29

def initialize(
  quote: :default,
  trailing_comma: :default,
  disable_auto_ternary: :default,
  target_ruby_version: :default
)
  @quote =
    if quote == :default
      # We ship with a single quotes plugin that will define this
      # constant. That constant is responsible for determining the default
      # quote style. If it's defined, we default to single quotes,
      # otherwise we default to double quotes.
      defined?(SINGLE_QUOTES) ? "'" : "\""
    else
      quote
    end

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

  @disable_auto_ternary =
    if disable_auto_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_auto_ternary
    end

  @target_ruby_version =
    if target_ruby_version == :default
      # The default target Ruby version is the current version of Ruby.
      # This is really only used for very niche cases, and it shouldn't be
      # used by most users.
      SemanticVersion.new(RUBY_VERSION)
    else
      target_ruby_version
    end
end

Instance Attribute Details

#disable_auto_ternaryObject (readonly)

Returns the value of attribute disable_auto_ternary.



24
25
26
# File 'lib/syntax_tree/formatter.rb', line 24

def disable_auto_ternary
  @disable_auto_ternary
end

#quoteObject (readonly)

Returns the value of attribute quote.



24
25
26
# File 'lib/syntax_tree/formatter.rb', line 24

def quote
  @quote
end

#target_ruby_versionObject (readonly)

Returns the value of attribute target_ruby_version.



24
25
26
# File 'lib/syntax_tree/formatter.rb', line 24

def target_ruby_version
  @target_ruby_version
end

#trailing_commaObject (readonly)

Returns the value of attribute trailing_comma.



24
25
26
# File 'lib/syntax_tree/formatter.rb', line 24

def trailing_comma
  @trailing_comma
end