swc_ecma_ast/
list.rs

1#![allow(unused)]
2#![allow(non_upper_case_globals)]
3use bitflags::bitflags;
4
5bitflags! {
6    /// Represents the formatting rule for a list of nodes.
7    #[derive(PartialEq, Eq, Copy, Clone)]
8    pub struct ListFormat: u32 {
9        /// Default value.
10        const None = 0;
11
12        // Line separators
13        /// Prints the list on a single line (default).
14        const SingleLine = 0;
15        /// Prints the list on multiple lines.
16        const MultiLine = 1 << 0;
17        /// Prints the list using line preservation if possible.
18        const PreserveLines = 1 << 1;
19        const LinesMask = Self::MultiLine.bits() | Self::PreserveLines.bits();
20
21        // Delimiters
22        /// There is no delimiter between list items (default).
23        const NotDelimited = 0;
24        /// Each list item is space-and-bar (" |") delimited.
25        const BarDelimited = 1 << 2;
26        /// Each list item is space-and-ampersand (" &") delimited.
27        const AmpersandDelimited = 1 << 3;
28        /// Each list item is comma (";") delimited.
29        const CommaDelimited = 1 << 4;
30        const DelimitersMask = Self::BarDelimited.bits()
31            | Self::AmpersandDelimited.bits()
32            | Self::CommaDelimited.bits();
33
34        // Write a trailing comma (";") if present.
35        const AllowTrailingComma = 1 << 5;
36
37        // Whitespace
38        /// The list should be indented.
39        const Indented = 1 << 6;
40        /// Inserts a space after the opening brace and before the closing
41        /// brace.
42        const SpaceBetweenBraces = 1 << 7;
43        /// Inserts a space between each sibling node.
44        const SpaceBetweenSiblings = 1 << 8;
45
46        // Brackets/Braces
47        /// The list is surrounded by "{" and "}".
48        const Braces = 1 << 9;
49        /// The list is surrounded by "(" and ")".
50        const Parenthesis = 1 << 10;
51        /// The list is surrounded by "<" and ">".
52        const AngleBrackets = 1 << 11;
53        /// The list is surrounded by "[" and "]".
54        const SquareBrackets = 1 << 12;
55        const BracketsMask = Self::Braces.bits()
56            | Self::Parenthesis.bits()
57            | Self::AngleBrackets.bits()
58            | Self::SquareBrackets.bits();
59
60        /// Do not emit brackets if the list is undefined.
61        const OptionalIfUndefined = 1 << 13;
62        /// Do not emit brackets if the list is empty.
63        const OptionalIfEmpty = 1 << 14;
64        const Optional = Self::OptionalIfUndefined.bits() | Self::OptionalIfEmpty.bits();
65
66        // Others
67        /// Prefer adding a LineTerminator between synthesized nodes.
68        const PreferNewLine = 1 << 15;
69        /// Do not emit a trailing NewLine for a MultiLine list.
70        const NoTrailingNewLine = 1 << 16;
71        /// Do not emit comments between each node
72        const NoInterveningComments = 1 << 17;
73        /// If the literal is empty; do not add spaces between braces.
74        const NoSpaceIfEmpty = 1 << 18;
75        const SingleElement = 1 << 19;
76        const ForceTrailingComma = 1 << 20;
77
78        // Optimization.
79        const CanSkipTrailingComma = 1 << 21;
80
81        // Precomputed Formats
82        const Modifiers = Self::SingleLine.bits()
83            | Self::SpaceBetweenSiblings.bits()
84            | Self::NoInterveningComments.bits();
85        const HeritageClauses = Self::SingleLine.bits() | Self::SpaceBetweenSiblings.bits();
86        const SingleLineTypeLiteralMembers = Self::SingleLine.bits()
87            | Self::SpaceBetweenBraces.bits()
88            | Self::SpaceBetweenSiblings.bits()
89            | Self::Indented.bits();
90        const MultiLineTypeLiteralMembers = Self::MultiLine.bits() | Self::Indented.bits();
91        const TupleTypeElements = Self::CommaDelimited.bits()
92            | Self::SpaceBetweenSiblings.bits()
93            | Self::SingleLine.bits()
94            | Self::Indented.bits();
95        const UnionTypeConstituents = Self::BarDelimited.bits()
96            | Self::SpaceBetweenSiblings.bits()
97            | Self::SingleLine.bits();
98        const IntersectionTypeConstituents = Self::AmpersandDelimited.bits()
99            | Self::SpaceBetweenSiblings.bits()
100            | Self::SingleLine.bits();
101        const ObjectBindingPatternElements = Self::SingleLine.bits()
102            | Self::SpaceBetweenBraces.bits()
103            | Self::CommaDelimited.bits()
104            | Self::SpaceBetweenSiblings.bits()
105            | Self::NoSpaceIfEmpty.bits();
106        const ArrayBindingPatternElements = Self::SingleLine.bits()
107            | Self::CommaDelimited.bits()
108            | Self::SpaceBetweenSiblings.bits()
109            | Self::NoSpaceIfEmpty.bits();
110        const ObjectLiteralExpressionProperties = Self::MultiLine.bits()
111            | Self::CommaDelimited.bits()
112            | Self::SpaceBetweenSiblings.bits()
113            | Self::SpaceBetweenBraces.bits()
114            | Self::Indented.bits()
115            | Self::Braces.bits()
116            | Self::NoSpaceIfEmpty.bits();
117        const ArrayLiteralExpressionElements = Self::PreserveLines.bits()
118            | Self::CommaDelimited.bits()
119            | Self::SpaceBetweenSiblings.bits()
120            | Self::Indented.bits()
121            | Self::SquareBrackets.bits();
122        const CommaListElements = Self::CommaDelimited.bits()
123            | Self::SpaceBetweenSiblings.bits()
124            | Self::SingleLine.bits();
125        const CallExpressionArguments = Self::CommaDelimited.bits()
126            | Self::SpaceBetweenSiblings.bits()
127            | Self::SingleLine.bits()
128            | Self::Parenthesis.bits();
129        const NewExpressionArguments = Self::CommaDelimited.bits()
130            | Self::SpaceBetweenSiblings.bits()
131            | Self::SingleLine.bits()
132            | Self::Parenthesis.bits()
133            | Self::OptionalIfUndefined.bits();
134        const TemplateExpressionSpans = Self::SingleLine.bits() | Self::NoInterveningComments.bits();
135        const SingleLineBlockStatements = Self::SpaceBetweenBraces.bits()
136            | Self::SpaceBetweenSiblings.bits()
137            | Self::SingleLine.bits();
138        const MultiLineBlockStatements = Self::Indented.bits() | Self::MultiLine.bits();
139        const VariableDeclarationList = Self::CommaDelimited.bits()
140            | Self::SpaceBetweenSiblings.bits()
141            | Self::SingleLine.bits();
142        const SingleLineFunctionBodyStatements = Self::SingleLine.bits()
143            | Self::SpaceBetweenSiblings.bits()
144            | Self::SpaceBetweenBraces.bits();
145        const MultiLineFunctionBodyStatements = Self::MultiLine.bits();
146        const ClassHeritageClauses = Self::CommaDelimited.bits() | Self::SingleLine.bits() | Self::SpaceBetweenSiblings.bits();
147        const ClassMembers = Self::Indented.bits() | Self::MultiLine.bits();
148        const InterfaceMembers = Self::Indented.bits() | Self::MultiLine.bits();
149        const EnumMembers = Self::CommaDelimited.bits() | Self::Indented.bits() | Self::MultiLine.bits();
150        const CaseBlockClauses = Self::Indented.bits() | Self::MultiLine.bits();
151        const NamedImportsOrExportsElements = Self::CommaDelimited.bits()
152            | Self::SpaceBetweenSiblings.bits()
153            | Self::AllowTrailingComma.bits()
154            | Self::SingleLine.bits()
155            | Self::SpaceBetweenBraces.bits();
156        const JsxElementOrFragmentChildren = Self::SingleLine.bits() | Self::NoInterveningComments.bits();
157        const JsxElementAttributes = Self::SingleLine.bits()
158            | Self::SpaceBetweenSiblings.bits()
159            | Self::NoInterveningComments.bits();
160        const CaseOrDefaultClauseStatements = Self::Indented.bits()
161            | Self::MultiLine.bits()
162            | Self::NoTrailingNewLine.bits()
163            | Self::OptionalIfEmpty.bits();
164        const HeritageClauseTypes = Self::CommaDelimited.bits()
165            | Self::SpaceBetweenSiblings.bits()
166            | Self::SingleLine.bits();
167        const SourceFileStatements = Self::MultiLine.bits() | Self::NoTrailingNewLine.bits();
168        const Decorators = Self::MultiLine.bits() | Self::Optional.bits();
169        const TypeArguments = Self::CommaDelimited.bits()
170            | Self::SpaceBetweenSiblings.bits()
171            | Self::SingleLine.bits()
172            | Self::AngleBrackets.bits()
173            | Self::Optional.bits();
174        const TypeParameters = Self::CommaDelimited.bits()
175            | Self::SpaceBetweenSiblings.bits()
176            | Self::SingleLine.bits()
177            | Self::AngleBrackets.bits()
178            | Self::Optional.bits();
179        const Parameters = Self::CommaDelimited.bits()
180            | Self::SpaceBetweenSiblings.bits()
181            | Self::SingleLine.bits()
182            | Self::Parenthesis.bits();
183        const IndexSignatureParameters = Self::CommaDelimited.bits()
184            | Self::SpaceBetweenSiblings.bits()
185            | Self::SingleLine.bits()
186            | Self::Indented.bits()
187            | Self::SquareBrackets.bits();
188    }
189}
190
191impl ListFormat {
192    pub fn opening_bracket(self) -> &'static str {
193        match self & ListFormat::BracketsMask {
194            ListFormat::Braces => "{",
195            ListFormat::Parenthesis => "(",
196            ListFormat::AngleBrackets => "<",
197            ListFormat::SquareBrackets => "[",
198            _ => unreachable!(),
199        }
200    }
201
202    pub fn closing_bracket(self) -> &'static str {
203        match self & ListFormat::BracketsMask {
204            ListFormat::Braces => "}",
205            ListFormat::Parenthesis => ")",
206            ListFormat::AngleBrackets => ">",
207            ListFormat::SquareBrackets => "]",
208            _ => unreachable!(),
209        }
210    }
211}