@@ -4,6 +4,18 @@ module SyntaxTree
44 # A slightly enhanced PP that knows how to format recursively including
55 # comments.
66 class Formatter < PrettierPrint
7+ # We want to minimize as much as possible the number of options that are
8+ # available in syntax tree. For the most part, if users want non-default
9+ # formatting, they should override the format methods on the specific nodes
10+ # themselves. However, because of some history with prettier and the fact
11+ # that folks have become entrenched in their ways, we decided to provide a
12+ # small amount of configurability.
13+ #
14+ # Note that we're keeping this in a global-ish hash instead of just
15+ # overriding methods on classes so that other plugins can reference this if
16+ # necessary. For example, the RBS plugin references the quote style.
17+ OPTIONS = { quote : "\" " , trailing_comma : false }
18+
719 COMMENT_PRIORITY = 1
820 HEREDOC_PRIORITY = 2
921
@@ -14,13 +26,20 @@ class Formatter < PrettierPrint
1426 attr_reader :quote , :trailing_comma
1527 alias trailing_comma? trailing_comma
1628
17- def initialize ( source , ...)
18- super ( ...)
29+ def initialize (
30+ source ,
31+ *args ,
32+ quote : OPTIONS [ :quote ] ,
33+ trailing_comma : OPTIONS [ :trailing_comma ]
34+ )
35+ super ( *args )
1936
2037 @source = source
2138 @stack = [ ]
22- @quote = "\" "
23- @trailing_comma = false
39+
40+ # Memoizing these values per formatter to make access faster.
41+ @quote = quote
42+ @trailing_comma = trailing_comma
2443 end
2544
2645 def self . format ( source , node )
0 commit comments