swc_ecma_transforms_base/
feature.rs

1#![allow(non_upper_case_globals)]
2use bitflags::bitflags;
3use swc_ecma_ast::EsVersion::{self, *};
4
5bitflags! {
6    #[derive(Default, Clone, Copy)]
7    pub struct FeatureFlag: u64 {
8        /// `transform-template-literals`
9        const TemplateLiterals = 1 << 0;
10
11        /// `transform-literals`
12        const Literals = 1 << 1;
13
14        /// `transform-function-name`
15        const FunctionName = 1 << 2;
16
17        /// `transform-arrow-functions`
18        const ArrowFunctions = 1 << 3;
19
20        /// `transform-block-scoped-functions`
21        const BlockScopedFunctions = 1 << 4;
22
23        /// `transform-classes`
24        const Classes = 1 << 5;
25
26        /// `transform-object-super`
27        const ObjectSuper = 1 << 6;
28
29        /// `transform-shorthand-properties`
30        const ShorthandProperties = 1 << 7;
31
32        /// `transform-duplicate-keys`
33        const DuplicateKeys = 1 << 8;
34
35        /// `transform-computed-properties`
36        const ComputedProperties = 1 << 9;
37
38        /// `transform-for-of`
39        const ForOf = 1 << 10;
40
41        /// `transform-sticky-regex`
42        const StickyRegex = 1 << 11;
43
44        /// `transform-dotall-regex`
45        const DotAllRegex = 1 << 12;
46
47        /// `transform-unicode-regex`
48        const UnicodeRegex = 1 << 13;
49
50        /// `transform-spread`
51        const Spread = 1 << 14;
52
53        /// `transform-parameters`
54        const Parameters = 1 << 15;
55
56        /// `transform-destructuring`
57        const Destructuring = 1 << 16;
58
59        /// `transform-block-scoping`
60        const BlockScoping = 1 << 17;
61
62        /// `transform-typeof-symbol`
63        const TypeOfSymbol = 1 << 18;
64
65        /// `transform-new-target`
66        const NewTarget = 1 << 19;
67
68        /// `transform-regenerator`
69        const Regenerator = 1 << 20;
70
71        /// `transform-exponentiation-operator`
72        const ExponentiationOperator = 1 << 21;
73
74        /// `transform-async-to-generator`
75        const AsyncToGenerator = 1 << 22;
76
77        /// `proposal-async-generator-functions`
78        const AsyncGeneratorFunctions = 1 << 23;
79
80        /// `proposal-object-rest-spread`
81        const ObjectRestSpread = 1 << 24;
82
83        /// `proposal-unicode-property-regex`
84        const UnicodePropertyRegex = 1 << 25;
85
86        /// `proposal-json-strings`
87        const JsonStrings = 1 << 26;
88
89        /// `proposal-optional-catch-binding`
90        const OptionalCatchBinding = 1 << 27;
91
92        /// `transform-named-capturing-groups-regex`
93        const NamedCapturingGroupsRegex = 1 << 28;
94
95        /// `transform-member-expression-literals`
96        const MemberExpressionLiterals = 1 << 29;
97
98        /// `transform-property-literals`
99        const PropertyLiterals = 1 << 30;
100
101        /// `transform-reserved-words`
102        const ReservedWords = 1 << 31;
103
104        /// `proposal-export-namespace-from`
105        const ExportNamespaceFrom = 1 << 32;
106
107        /// `proposal-nullish-coalescing-operator`
108        const NullishCoalescing = 1 << 33;
109
110        /// `proposal-logical-assignment-operators`
111        const LogicalAssignmentOperators = 1 << 34;
112
113        /// `proposal-optional-chaining`
114        const OptionalChaining = 1 << 35;
115
116        /// `proposal-class-properties`
117        const ClassProperties = 1 << 36;
118
119        /// `proposal-numeric-separator`
120        const NumericSeparator = 1 << 37;
121
122        /// `proposal-private-methods`
123        const PrivateMethods = 1 << 38;
124
125        /// `proposal-class-static-block`
126        const ClassStaticBlock = 1 << 39;
127
128        /// `proposal-private-property-in-object`
129        const PrivatePropertyInObject = 1 << 40;
130
131        /// `transform-unicode-escapes`
132        const UnicodeEscapes = 1 << 41;
133
134        /// `bugfix/transform-async-arrows-in-class`
135        const BugfixAsyncArrowsInClass = 1 << 42;
136
137        /// `bugfix/transform-edge-default-parameters`
138        const BugfixEdgeDefaultParam = 1 << 43;
139
140        /// `bugfix/transform-tagged-template-caching`
141        const BugfixTaggedTemplateCaching = 1 << 44;
142
143        /// `bugfix/transform-safari-id-destructuring-collision-in-function-expression`
144        const BugfixSafariIdDestructuringCollisionInFunctionExpression = 1 << 45;
145
146        /// `bugfix/transform-edge-function-name`
147        const BugfixTransformEdgeFunctionName = 1 << 46; // TODO
148
149        /// `bugfix/transform-safari-block-shadowing`
150        const BugfixTransformSafariBlockShadowing = 1 << 47; // TODO
151
152        /// `bugfix/transform-safari-for-shadowing`
153        const BugfixTransformSafariForShadowing = 1 << 48; // TODO
154
155        /// `bugfix/transform-v8-spread-parameters-in-optional-chaining`
156        const BugfixTransformV8SpreadParametersInOptionalChaining = 1 << 49; // TODO
157    }
158}
159
160pub fn enable_available_feature_from_es_version(version: EsVersion) -> FeatureFlag {
161    let mut feature = FeatureFlag::empty();
162
163    if version < Es5 {
164        return feature;
165    }
166
167    feature |= FeatureFlag::PropertyLiterals
168        | FeatureFlag::MemberExpressionLiterals
169        | FeatureFlag::ReservedWords;
170
171    if version < Es2015 {
172        return feature;
173    }
174
175    feature |= FeatureFlag::ArrowFunctions
176        | FeatureFlag::BlockScopedFunctions
177        | FeatureFlag::BlockScoping
178        | FeatureFlag::Classes
179        | FeatureFlag::ComputedProperties
180        | FeatureFlag::Destructuring
181        | FeatureFlag::DuplicateKeys
182        | FeatureFlag::ForOf
183        | FeatureFlag::FunctionName
184        | FeatureFlag::NewTarget
185        | FeatureFlag::ObjectSuper
186        | FeatureFlag::Parameters
187        | FeatureFlag::Regenerator
188        | FeatureFlag::ShorthandProperties
189        | FeatureFlag::Spread
190        | FeatureFlag::StickyRegex
191        | FeatureFlag::TemplateLiterals
192        | FeatureFlag::TypeOfSymbol
193        | FeatureFlag::UnicodeRegex;
194
195    if version < Es2016 {
196        return feature;
197    }
198
199    feature |= FeatureFlag::ExponentiationOperator;
200
201    if version < Es2017 {
202        return feature;
203    }
204
205    // support `async`
206    feature |= FeatureFlag::AsyncToGenerator;
207
208    if version < Es2018 {
209        return feature;
210    }
211
212    feature |= FeatureFlag::ObjectRestSpread
213        | FeatureFlag::DotAllRegex
214        | FeatureFlag::NamedCapturingGroupsRegex
215        | FeatureFlag::UnicodePropertyRegex;
216
217    if version < Es2019 {
218        return feature;
219    }
220
221    feature |= FeatureFlag::OptionalCatchBinding;
222
223    if version < Es2020 {
224        return feature;
225    }
226
227    feature |= FeatureFlag::ExportNamespaceFrom
228        | FeatureFlag::NullishCoalescing
229        | FeatureFlag::OptionalChaining;
230
231    if version < Es2021 {
232        return feature;
233    }
234
235    feature |= FeatureFlag::LogicalAssignmentOperators;
236
237    if version < Es2022 {
238        return feature;
239    }
240
241    feature |= FeatureFlag::ClassProperties
242        | FeatureFlag::ClassStaticBlock
243        | FeatureFlag::PrivatePropertyInObject;
244
245    feature
246}