swc_ecma_ast/
list.rs

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