1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![allow(unused)]
#![allow(non_upper_case_globals)]
use bitflags::bitflags;
use swc_common::add_bitflags;

bitflags! {
    /// Represents the formatting rule for a list of nodes.
    pub struct ListFormat: u32 {
        /// Default value.
        const None = 0;
    }
}

add_bitflags!(
    ListFormat,
    // Handled by bitflags! macro.
    // Values { None: 0 },
    /// Line separators
    Values {
        /// Prints the list on a single line (default).
        SingleLine: 0,
        /// Prints the list on multiple lines.
        MultiLine: 1 << 0,
        /// Prints the list using line preservation if possible.
        PreserveLines: 1 << 1,
        LinesMask: SingleLine | MultiLine | PreserveLines,
    },
    /// Delimiters
    Values {
        /// There is no delimiter between list items (default).
        NotDelimited: 0,
        /// Each list item is space-and-bar (" |") delimited.
        BarDelimited: 1 << 2,
        /// Each list item is space-and-ampersand (" &") delimited.
        AmpersandDelimited: 1 << 3,
        /// Each list item is comma (",") delimited.
        CommaDelimited: 1 << 4,
        DelimitersMask: BarDelimited | AmpersandDelimited | CommaDelimited,
    },
    Values {
        /// Write a trailing comma (",") if present.
        AllowTrailingComma: 1 << 5,
    },
    /// Whitespace
    Values {
        /// The list should be indented.
        Indented: 1 << 6,
        /// Inserts a space after the opening brace and before the closing
        /// brace.
        SpaceBetweenBraces: 1 << 7,
        /// Inserts a space between each sibling node.
        SpaceBetweenSiblings: 1 << 8,
    },
    /// Brackets/Braces
    Values {
        /// The list is surrounded by "{" and "}".
        Braces: 1 << 9,
        /// The list is surrounded by "(" and ")".
        Parenthesis: 1 << 10,
        /// The list is surrounded by "<" and ">".
        AngleBrackets: 1 << 11,
        /// The list is surrounded by "[" and "]".
        SquareBrackets: 1 << 12,
        BracketsMask: Braces | Parenthesis | AngleBrackets | SquareBrackets,
    },
    Values {
        /// Do not emit brackets if the list is undefined.
        OptionalIfUndefined: 1 << 13,
        /// Do not emit brackets if the list is empty.
        OptionalIfEmpty: 1 << 14,
        Optional: OptionalIfUndefined | OptionalIfEmpty,
    },
    /// Others
    Values {
        /// Prefer adding a LineTerminator between synthesized nodes.
        PreferNewLine: 1 << 15,
        /// Do not emit a trailing NewLine for a MultiLine list.
        NoTrailingNewLine: 1 << 16,
        /// Do not emit comments between each node
        NoInterveningComments: 1 << 17,
        /// If the literal is empty, do not add spaces between braces.
        NoSpaceIfEmpty: 1 << 18,
        SingleElement: 1 << 19,
        ForceTrailingComma: 1 << 20,
    },
    // Optimization.
    Values {
        CanSkipTrailingComma: 1 << 21
    },
    /// Precomputed Formats
    Values {
        Modifiers: SingleLine | SpaceBetweenSiblings | NoInterveningComments,
        HeritageClauses: SingleLine | SpaceBetweenSiblings,
        SingleLineTypeLiteralMembers: SingleLine
            | SpaceBetweenBraces
            | SpaceBetweenSiblings
            | Indented,
        MultiLineTypeLiteralMembers: MultiLine | Indented,
        TupleTypeElements: CommaDelimited | SpaceBetweenSiblings | SingleLine | Indented,
        UnionTypeConstituents: BarDelimited | SpaceBetweenSiblings | SingleLine,
        IntersectionTypeConstituents: AmpersandDelimited | SpaceBetweenSiblings | SingleLine,
        ObjectBindingPatternElements: SingleLine
            | SpaceBetweenBraces
            | CommaDelimited
            | SpaceBetweenSiblings
            | NoSpaceIfEmpty,
        ArrayBindingPatternElements: SingleLine
            | CommaDelimited
            | SpaceBetweenSiblings
            | NoSpaceIfEmpty,
        // ObjectLiteralExpressionProperties: PreserveLines
        //     | CommaDelimited
        //     | SpaceBetweenSiblings
        //     | SpaceBetweenBraces
        //     | Indented
        //     | Braces
        //     | NoSpaceIfEmpty,
        ObjectLiteralExpressionProperties: MultiLine
            | CommaDelimited
            | SpaceBetweenSiblings
            | SpaceBetweenBraces
            | Indented
            | Braces
            | NoSpaceIfEmpty,
        ArrayLiteralExpressionElements: PreserveLines
            | CommaDelimited
            | SpaceBetweenSiblings
            | Indented
            | SquareBrackets,
        CommaListElements: CommaDelimited | SpaceBetweenSiblings | SingleLine,
        CallExpressionArguments: CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis,
        NewExpressionArguments: CommaDelimited
            | SpaceBetweenSiblings
            | SingleLine
            | Parenthesis
            | OptionalIfUndefined,
        TemplateExpressionSpans: SingleLine | NoInterveningComments,
        SingleLineBlockStatements: SpaceBetweenBraces | SpaceBetweenSiblings | SingleLine,
        MultiLineBlockStatements: Indented | MultiLine,
        VariableDeclarationList: CommaDelimited | SpaceBetweenSiblings | SingleLine,
        SingleLineFunctionBodyStatements: SingleLine | SpaceBetweenSiblings | SpaceBetweenBraces,
        MultiLineFunctionBodyStatements: MultiLine,
        ClassHeritageClauses: SingleLine | SpaceBetweenSiblings,
        ClassMembers: Indented | MultiLine,
        InterfaceMembers: Indented | MultiLine,
        EnumMembers: CommaDelimited | Indented | MultiLine,
        CaseBlockClauses: Indented | MultiLine,
        NamedImportsOrExportsElements: CommaDelimited
            | SpaceBetweenSiblings
            | AllowTrailingComma
            | SingleLine
            | SpaceBetweenBraces,
        JsxElementOrFragmentChildren: SingleLine | NoInterveningComments,
        JsxElementAttributes: SingleLine | SpaceBetweenSiblings | NoInterveningComments,
        CaseOrDefaultClauseStatements: Indented | MultiLine | NoTrailingNewLine | OptionalIfEmpty,
        HeritageClauseTypes: CommaDelimited | SpaceBetweenSiblings | SingleLine,
        SourceFileStatements: MultiLine | NoTrailingNewLine,
        Decorators: MultiLine | Optional,
        TypeArguments: CommaDelimited
            | SpaceBetweenSiblings
            | SingleLine
            | AngleBrackets
            | Optional,
        TypeParameters: CommaDelimited
            | SpaceBetweenSiblings
            | SingleLine
            | AngleBrackets
            | Optional,
        Parameters: CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis,
        IndexSignatureParameters: CommaDelimited
            | SpaceBetweenSiblings
            | SingleLine
            | Indented
            | SquareBrackets,
    },
);

impl ListFormat {
    pub fn opening_bracket(self) -> &'static str {
        match self & ListFormat::BracketsMask {
            ListFormat::Braces => "{",
            ListFormat::Parenthesis => "(",
            ListFormat::AngleBrackets => "<",
            ListFormat::SquareBrackets => "[",
            _ => unreachable!(),
        }
    }

    pub fn closing_bracket(self) -> &'static str {
        match self & ListFormat::BracketsMask {
            ListFormat::Braces => "}",
            ListFormat::Parenthesis => ")",
            ListFormat::AngleBrackets => ">",
            ListFormat::SquareBrackets => "]",
            _ => unreachable!(),
        }
    }
}