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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use crate::js_lexer::T;
use crate::p::P;
// Zig: `p: anytype` for the generic parser instance. Lowered NewParser_ →
// `P<'a, const TS, const SCAN>` and converted to `impl P` methods (an unbounded
// `<P>` cannot access fields). The `Metadata::*` methods that need
// `p.load_name_from_ref` take a closure to avoid the impl-on-foreign-type problem.
// This function is taken from the official TypeScript compiler source code:
// https://github.com/microsoft/TypeScript/blob/master/src/compiler/parser.ts
impl<'a, const TS: bool, const SCAN: bool> P<'a, TS, SCAN> {
pub fn can_follow_type_arguments_in_expression(&mut self) -> bool {
let p = self;
match p.lexer.token {
// These are the only tokens can legally follow a type argument list. So we
// definitely want to treat them as type arg lists.
T::TOpenParen // foo<x>(
| T::TNoSubstitutionTemplateLiteral // foo<T> `...`
// foo<T> `...${100}...`
| T::TTemplateHead => true,
// A type argument list followed by `<` never makes sense, and a type argument list followed
// by `>` is ambiguous with a (re-scanned) `>>` operator, so we disqualify both. Also, in
// this context, `+` and `-` are unary operators, not binary operators.
T::TLessThan
| T::TGreaterThan
| T::TPlus
| T::TMinus
// TypeScript always sees "t_greater_than" instead of these tokens since
// their scanner works a little differently than our lexer. So since
// "t_greater_than" is forbidden above, we also forbid these too.
| T::TGreaterThanEquals
| T::TGreaterThanGreaterThan
| T::TGreaterThanGreaterThanEquals
| T::TGreaterThanGreaterThanGreaterThan
| T::TGreaterThanGreaterThanGreaterThanEquals => false,
// We favor the type argument list interpretation when it is immediately followed by
// a line break, a binary operator, or something that can't start an expression.
_ => p.lexer.has_newline_before || p.is_binary_operator() || !p.is_start_of_expression(),
}
}
} // end impl P (can_follow_type_arguments_in_expression)
impl<'a, const TS: bool, const SCAN: bool> P<'a, TS, SCAN> {
// TODO(port): narrow error set — only `lexer.next()` is fallible here.
pub fn is_ts_arrow_fn_jsx(&mut self) -> Result<bool, bun_core::Error> {
let p = self;
// PORT NOTE: Zig `const old = p.lexer` (value copy). Rust Lexer holds `&mut Log`
// so cannot Clone; use the LexerSnapshot POD via `snapshot()`/`restore()`.
let old_lexer = p.lexer.snapshot();
p.lexer.next()?;
// Look ahead to see if this should be an arrow function instead
let mut is_ts_arrow_fn = false;
if p.lexer.token == T::TConst {
p.lexer.next()?;
}
if p.lexer.token == T::TIdentifier {
p.lexer.next()?;
if p.lexer.token == T::TComma || p.lexer.token == T::TEquals {
is_ts_arrow_fn = true;
} else if p.lexer.token == T::TExtends {
p.lexer.next()?;
is_ts_arrow_fn = p.lexer.token != T::TEquals
&& p.lexer.token != T::TGreaterThan
&& p.lexer.token != T::TSlash;
}
}
// Restore the lexer
p.lexer.restore(&old_lexer);
Ok(is_ts_arrow_fn)
}
// This function is taken from the official TypeScript compiler source code:
// https://github.com/microsoft/TypeScript/blob/master/src/compiler/parser.ts
fn is_binary_operator(&self) -> bool {
let p = self;
match p.lexer.token {
T::TIn => p.allow_in,
T::TQuestionQuestion
| T::TBarBar
| T::TAmpersandAmpersand
| T::TBar
| T::TCaret
| T::TAmpersand
| T::TEqualsEquals
| T::TExclamationEquals
| T::TEqualsEqualsEquals
| T::TExclamationEqualsEquals
| T::TLessThan
| T::TGreaterThan
| T::TLessThanEquals
| T::TGreaterThanEquals
| T::TInstanceof
| T::TLessThanLessThan
| T::TGreaterThanGreaterThan
| T::TGreaterThanGreaterThanGreaterThan
| T::TPlus
| T::TMinus
| T::TAsterisk
| T::TSlash
| T::TPercent
| T::TAsteriskAsterisk => true,
T::TIdentifier => {
p.lexer.is_contextual_keyword(b"as") || p.lexer.is_contextual_keyword(b"satisfies")
}
_ => false,
}
}
// This function is taken from the official TypeScript compiler source code:
// https://github.com/microsoft/TypeScript/blob/master/src/compiler/parser.ts
fn is_start_of_left_hand_side_expression(&mut self) -> bool {
let p = self;
match p.lexer.token {
T::TThis
| T::TSuper
| T::TNull
| T::TTrue
| T::TFalse
| T::TNumericLiteral
| T::TBigIntegerLiteral
| T::TStringLiteral
| T::TNoSubstitutionTemplateLiteral
| T::TTemplateHead
| T::TOpenParen
| T::TOpenBracket
| T::TOpenBrace
| T::TFunction
| T::TClass
| T::TNew
| T::TSlash
| T::TSlashEquals
| T::TIdentifier => true,
T::TImport => p.look_ahead_next_token_is_open_paren_or_less_than_or_dot(),
_ => p.ts_is_identifier(),
}
}
fn look_ahead_next_token_is_open_paren_or_less_than_or_dot(&mut self) -> bool {
let p = self;
// PORT NOTE: Zig value-copied the Lexer; use snapshot()/restore() (see is_ts_arrow_fn_jsx).
let old_lexer = p.lexer.snapshot();
let old_log_disabled = p.lexer.is_log_disabled;
p.lexer.is_log_disabled = true;
let _ = p.lexer.next();
let result = matches!(p.lexer.token, T::TOpenParen | T::TLessThan | T::TDot);
// PORT NOTE: Zig used `defer` for restore; reshaped to linear since there is
// no early return between the defer and end of scope.
p.lexer.restore(&old_lexer);
p.lexer.is_log_disabled = old_log_disabled;
result
}
// This function is taken from the official TypeScript compiler source code:
// https://github.com/microsoft/TypeScript/blob/master/src/compiler/parser.ts
// PORT NOTE: renamed `ts_is_identifier` to avoid clash with lexer/P helpers of the same name.
fn ts_is_identifier(&self) -> bool {
use crate::parser::AwaitOrYield::AllowIdent;
let p = self;
if p.lexer.token == T::TIdentifier {
// If we have a 'yield' keyword, and we're in the [yield] context, then 'yield' is
// considered a keyword and is not an identifier.
if p.fn_or_arrow_data_parse.allow_yield != AllowIdent && p.lexer.identifier == b"yield"
{
return false;
}
// If we have an 'await' keyword, and we're in the [await] context, then 'await' is
// considered a keyword and is not an identifier.
if p.fn_or_arrow_data_parse.allow_await != AllowIdent && p.lexer.identifier == b"await"
{
return false;
}
return true;
}
false
}
fn is_start_of_expression(&mut self) -> bool {
let p = self;
if p.is_start_of_left_hand_side_expression() {
return true;
}
match p.lexer.token {
T::TPlus
| T::TMinus
| T::TTilde
| T::TExclamation
| T::TDelete
| T::TTypeof
| T::TVoid
| T::TPlusPlus
| T::TMinusMinus
| T::TLessThan
| T::TPrivateIdentifier
| T::TAt => true,
_ => {
if p.lexer.token == T::TIdentifier
&& (p.lexer.identifier == b"await" || p.lexer.identifier == b"yield")
{
// Yield/await always starts an expression. Either it is an identifier (in which case
// it is definitely an expression). Or it's a keyword (either because we're in
// a generator or async function, or in strict mode (or both)) and it started a yield or await expression.
return true;
}
// Error tolerance. If we see the start of some binary operator, we consider
// that the start of an expression. That way we'll parse out a missing identifier,
// give a good message about an identifier being missing, and then consume the
// rest of the binary expression.
if p.is_binary_operator() {
return true;
}
p.ts_is_identifier()
}
}
}
} // end impl P (predicate fns)
pub mod identifier {
pub(crate) enum StmtIdentifier {
SType,
SNamespace,
SAbstract,
SModule,
SInterface,
SDeclare,
}
pub(crate) fn for_str(str: &[u8]) -> Option<StmtIdentifier> {
match str.len() {
// "type".len
4 => {
if str == b"type" {
Some(StmtIdentifier::SType)
} else {
None
}
}
// "interface".len == "namespace".len
9 => {
if str == b"interface" {
Some(StmtIdentifier::SInterface)
} else if str == b"namespace" {
Some(StmtIdentifier::SNamespace)
} else {
None
}
}
// "abstract".len
8 => {
if str == b"abstract" {
Some(StmtIdentifier::SAbstract)
} else {
None
}
}
// "declare".len
7 => {
if str == b"declare" {
Some(StmtIdentifier::SDeclare)
} else {
None
}
}
// "module".len
6 => {
if str == b"module" {
Some(StmtIdentifier::SModule)
} else {
None
}
}
_ => None,
}
}
// PERF(port): was `phf::Map<&[u8], Kind>`. phf hashes the full identifier
// (SipHash) on every TIdentifier in a TS type position — including the
// overwhelmingly-common miss case (a user-defined type name). With only
// 16 entries spanning lengths 3..=9, a length gate rejects almost every
// miss on a single usize compare, and hits resolve in ≤2 slice compares.
// Length 6 is the only cluster (6 entries) so it gets a first-byte
// sub-dispatch. Mirrors the `clap::find_param` pattern (12577e958d71).
#[inline]
pub(crate) fn kind_for_identifier(ident: &[u8]) -> Option<Kind> {
match ident.len() {
3 => {
if ident == b"any" {
return Some(Kind::PrimitiveAny);
}
}
5 => {
if ident == b"keyof" {
return Some(Kind::PrefixKeyof);
}
if ident == b"never" {
return Some(Kind::PrimitiveNever);
}
if ident == b"infer" {
return Some(Kind::Infer);
}
}
6 => match ident[0] {
b'u' if ident == b"unique" => return Some(Kind::Unique),
b'o' if ident == b"object" => return Some(Kind::PrimitiveObject),
b'n' if ident == b"number" => return Some(Kind::PrimitiveNumber),
b'b' if ident == b"bigint" => return Some(Kind::PrimitiveBigint),
b's' => {
if ident == b"string" {
return Some(Kind::PrimitiveString);
}
if ident == b"symbol" {
return Some(Kind::PrimitiveSymbol);
}
}
_ => {}
},
7 => {
if ident == b"unknown" {
return Some(Kind::PrimitiveUnknown);
}
if ident == b"boolean" {
return Some(Kind::PrimitiveBoolean);
}
if ident == b"asserts" {
return Some(Kind::Asserts);
}
}
8 => {
if ident == b"abstract" {
return Some(Kind::Abstract);
}
if ident == b"readonly" {
return Some(Kind::PrefixReadonly);
}
}
9 => {
if ident == b"undefined" {
return Some(Kind::PrimitiveUndefined);
}
}
_ => {}
}
None
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Normal,
Unique,
Abstract,
Asserts,
PrefixKeyof,
PrefixReadonly,
PrimitiveAny,
PrimitiveNever,
PrimitiveUnknown,
PrimitiveUndefined,
PrimitiveObject,
PrimitiveNumber,
PrimitiveString,
PrimitiveBoolean,
PrimitiveBigint,
PrimitiveSymbol,
Infer,
}
}
#[derive(enumset::EnumSetType)]
pub enum SkipTypeOptions {
IsReturnType,
IsIndexSignature,
AllowTupleLabels,
DisallowConditionalTypes,
}
// PORT NOTE: Zig nested `Bitset` and `empty` inside `SkipTypeOptions`. Rust
// inherent associated types (`impl Foo { type Bar = ...; }`) are unstable
// (`inherent_associated_types`), so the alias and empty constant are hoisted
// to module scope.
pub(crate) type SkipTypeOptionsBitset = enumset::EnumSet<SkipTypeOptions>;
// ported from: src/js_parser/ast/TypeScript.zig