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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#![allow(non_upper_case_globals)]

use bitflags::bitflags;
use swc_common::add_bitflags;

impl Default for TypeFacts {
    fn default() -> Self {
        Self::None
    }
}

bitflags! {
    pub struct TypeFacts: u32 {
        const None = 0;
        /// typeof x === "string"
        const TypeofEQString = 1 << 0;
        /// typeof x === "number"
        const TypeofEQNumber = 1 << 1;
        /// typeof x === "bigint"
        const TypeofEQBigInt = 1 << 2;
        /// typeof x === "boolean"
        const TypeofEQBoolean = 1 << 3;
        /// typeof x === "symbol"
        const TypeofEQSymbol = 1 << 4;
        /// typeof x === "object"
        const TypeofEQObject = 1 << 5;
        /// typeof x === "function"
        const TypeofEQFunction = 1 << 6;
        /// typeof x === "xxx"
        const TypeofEQHostObject = 1 << 7;
        /// typeof x !== "string"
        const TypeofNEString = 1 << 8;
        /// typeof x !== "number"
        const TypeofNENumber = 1 << 9;
        /// typeof x !== "bigint"
        const TypeofNEBigInt = 1 << 10;
        /// typeof x !== "boolean"
        const TypeofNEBoolean = 1 << 11;
        /// typeof x !== "symbol"
        const TypeofNESymbol = 1 << 12;
        /// typeof x !== "object"
        const TypeofNEObject = 1 << 13;
        /// typeof x !== "function"
        const TypeofNEFunction = 1 << 14;
        /// typeof x !== "xxx"
        const TypeofNEHostObject = 1 << 15;
        /// x === undefined
        const EQUndefined = 1 << 16;
        /// x === null
        const EQNull = 1 << 17;
        /// x === undefined / x === null
        const EQUndefinedOrNull = 1 << 18;
        /// x !== undefined
        const NEUndefined = 1 << 19;
        /// x !== null
        const NENull = 1 << 20;
        /// x != undefined / x != null
        const NEUndefinedOrNull = 1 << 21;
        /// x
        const Truthy = 1 << 22;
        /// !x
        const Falsy = 1 << 23;
        const All = (1 << 24) - 1;
    }
}

add_bitflags!(
    TypeFacts,
    // Handled by bitflags! macro.
    // Values { None: 0 },
    /// Line separators
    Values {},
    /// Delimiters
    Values {
        BaseStringStrictFacts: TypeofEQString
            | TypeofNENumber
            | TypeofNEBigInt
            | TypeofNEBoolean
            | TypeofNESymbol
            | TypeofNEObject
            | TypeofNEFunction
            | TypeofNEHostObject
            | NEUndefined
            | NENull
            | NEUndefinedOrNull,
        BaseStringFacts: BaseStringStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
        StringStrictFacts: BaseStringStrictFacts | Truthy | Falsy,
        StringFacts: BaseStringFacts | Truthy,
        EmptyStringStrictFacts: BaseStringStrictFacts | Falsy,
        EmptyStringFacts: BaseStringFacts,
        NonEmptyStringStrictFacts: BaseStringStrictFacts | Truthy,
        NonEmptyStringFacts: BaseStringFacts | Truthy,
        BaseNumberStrictFacts: TypeofEQNumber
            | TypeofNEString
            | TypeofNEBigInt
            | TypeofNEBoolean
            | TypeofNESymbol
            | TypeofNEObject
            | TypeofNEFunction
            | TypeofNEHostObject
            | NEUndefined
            | NENull
            | NEUndefinedOrNull,
        BaseNumberFacts: BaseNumberStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
        NumberStrictFacts: BaseNumberStrictFacts | Truthy | Falsy,
        NumberFacts: BaseNumberFacts | Truthy,
        ZeroNumberStrictFacts: BaseNumberStrictFacts | Falsy,
        ZeroNumberFacts: BaseNumberFacts,
        NonZeroNumberStrictFacts: BaseNumberStrictFacts | Truthy,
        NonZeroNumberFacts: BaseNumberFacts | Truthy,
        BaseBigIntStrictFacts: TypeofEQBigInt
            | TypeofNEString
            | TypeofNENumber
            | TypeofNEBoolean
            | TypeofNESymbol
            | TypeofNEObject
            | TypeofNEFunction
            | TypeofNEHostObject
            | NEUndefined
            | NENull
            | NEUndefinedOrNull,
        BaseBigIntFacts: BaseBigIntStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
        BigIntStrictFacts: BaseBigIntStrictFacts | Truthy | Falsy,
        BigIntFacts: BaseBigIntFacts | Truthy,
        ZeroBigIntStrictFacts: BaseBigIntStrictFacts | Falsy,
        ZeroBigIntFacts: BaseBigIntFacts,
        NonZeroBigIntStrictFacts: BaseBigIntStrictFacts | Truthy,
        NonZeroBigIntFacts: BaseBigIntFacts | Truthy,
        BaseBooleanStrictFacts: TypeofEQBoolean
            | TypeofNEString
            | TypeofNENumber
            | TypeofNEBigInt
            | TypeofNESymbol
            | TypeofNEObject
            | TypeofNEFunction
            | TypeofNEHostObject
            | NEUndefined
            | NENull
            | NEUndefinedOrNull,
        BaseBooleanFacts: BaseBooleanStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
        BooleanStrictFacts: BaseBooleanStrictFacts | Truthy | Falsy,
        BooleanFacts: BaseBooleanFacts | Truthy,
        FalseStrictFacts: BaseBooleanStrictFacts | Falsy,
        FalseFacts: BaseBooleanFacts,
        TrueStrictFacts: BaseBooleanStrictFacts | Truthy,
        TrueFacts: BaseBooleanFacts | Truthy,
        SymbolStrictFacts: TypeofEQSymbol
            | TypeofNEString
            | TypeofNENumber
            | TypeofNEBigInt
            | TypeofNEBoolean
            | TypeofNEObject
            | TypeofNEFunction
            | TypeofNEHostObject
            | NEUndefined
            | NENull
            | NEUndefinedOrNull
            | Truthy,
        SymbolFacts: SymbolStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
        ObjectStrictFacts: TypeofEQObject
            | TypeofEQHostObject
            | TypeofNEString
            | TypeofNENumber
            | TypeofNEBigInt
            | TypeofNEBoolean
            | TypeofNESymbol
            | TypeofNEFunction
            | NEUndefined
            | NENull
            | NEUndefinedOrNull
            | Truthy,
        ObjectFacts: ObjectStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
        FunctionStrictFacts: TypeofEQFunction
            | TypeofEQHostObject
            | TypeofNEString
            | TypeofNENumber
            | TypeofNEBigInt
            | TypeofNEBoolean
            | TypeofNESymbol
            | TypeofNEObject
            | NEUndefined
            | NENull
            | NEUndefinedOrNull
            | Truthy,
        FunctionFacts: FunctionStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
        UndefinedFacts: TypeofNEString
            | TypeofNENumber
            | TypeofNEBigInt
            | TypeofNEBoolean
            | TypeofNESymbol
            | TypeofNEObject
            | TypeofNEFunction
            | TypeofNEHostObject
            | EQUndefined
            | EQUndefinedOrNull
            | NENull
            | Falsy,
        NullFacts: TypeofEQObject
            | TypeofNEString
            | TypeofNENumber
            | TypeofNEBigInt
            | TypeofNEBoolean
            | TypeofNESymbol
            | TypeofNEFunction
            | TypeofNEHostObject
            | EQNull
            | EQUndefinedOrNull
            | NEUndefined
            | Falsy,
        EmptyObjectStrictFacts: TypeFacts::All.bits
            & !(TypeFacts::EQUndefined.bits | TypeFacts::EQNull.bits | TypeFacts::EQUndefinedOrNull.bits),
        EmptyObjectFacts: All,
    },
);

impl TypeFacts {
    pub fn typeof_eq(s: &str) -> Option<Self> {
        Some(match s {
            "string" => TypeFacts::BaseStringStrictFacts,
            "number" => TypeFacts::BaseNumberStrictFacts,
            "bigint" => TypeFacts::BaseBigIntStrictFacts,
            "boolean" => TypeFacts::BaseBooleanStrictFacts,
            "symbol" => TypeFacts::SymbolStrictFacts,
            "undefined" => TypeFacts::EQUndefined,
            "object" => {
                TypeFacts::TypeofEQObject
                    | TypeFacts::TypeofNEString
                    | TypeFacts::TypeofNENumber
                    | TypeFacts::TypeofNEBigInt
                    | TypeFacts::TypeofNEBoolean
                    | TypeFacts::TypeofNESymbol
                    | TypeFacts::TypeofNEFunction
                    | TypeFacts::NEUndefined
            }
            "function" => {
                TypeFacts::TypeofEQFunction
                    | TypeFacts::TypeofNEString
                    | TypeFacts::TypeofNENumber
                    | TypeFacts::TypeofNEBigInt
                    | TypeFacts::TypeofNEBoolean
                    | TypeFacts::TypeofNESymbol
                    | TypeFacts::TypeofNEObject
                    | TypeFacts::NEUndefined
                    | TypeFacts::NENull
                    | TypeFacts::NEUndefinedOrNull
                    | TypeFacts::Truthy
            }
            _ => return None,
        })
    }

    pub fn typeof_neq(s: &str) -> Option<Self> {
        Some(match s {
            "string" => TypeFacts::TypeofNEString,
            "number" => TypeFacts::TypeofNENumber,
            "bigint" => TypeFacts::TypeofNEBigInt,
            "boolean" => TypeFacts::TypeofNEBoolean,
            "symbol" => TypeFacts::TypeofNESymbol,
            "undefined" => TypeFacts::NEUndefined,
            "object" => TypeFacts::TypeofNEObject,
            "function" => TypeFacts::TypeofNEFunction,
            _ => return None,
        })
    }
}