biome_js_parser 0.5.7

Biome's JavaScript parser
Documentation
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use crate::parser::rewrite_parser::{RewriteMarker, RewriteParser, RewriteToken};
use crate::parser::JsParserCheckpoint;
use crate::prelude::*;
use crate::rewrite::{rewrite_events, RewriteParseEvents};
use crate::syntax::class::parse_initializer_clause;
use crate::syntax::expr::{
    is_at_identifier, parse_conditional_expr, parse_unary_expr, ExpressionContext,
};
use crate::syntax::js_parse_error::{
    expected_assignment_target, expected_identifier, expected_object_member_name,
    invalid_assignment_error,
};
use crate::syntax::object::{is_at_object_member_name, parse_object_member_name};
use crate::syntax::pattern::{ParseArrayPattern, ParseObjectPattern, ParseWithDefaultPattern};
use crate::JsParser;
use crate::ParsedSyntax::{Absent, Present};
use biome_js_syntax::{JsSyntaxKind::*, *};
use biome_parser::diagnostic::expected_any;
use biome_rowan::AstNode;

// test js assignment_target
// foo += bar = b ??= 3;
// a.foo -= bar;
// (foo = bar);
// (((foo))) = bar;
// a["test"] = bar;
// a.call().chain().member = x;
// ++count === 3
// a['b'] = c[d] = "test"

// test_err js invalid_assignment_target
// ++a = b;
// (++a) = b;
// (a = b;
// a?.b = b;
// a?.["b"] = b;
// (a +) = b;

// test ts ts_non_null_assignment
// let a;
// a! &= 2;
// let b = { a: null };
// b.a! &= 5

// test ts ts_as_assignment
// let a: any;
// type B<A> = { a: A };
// (a as string) = "string";
// ((a as any) as string) = null;
// ({ b: a as string } = { b: "test" });
// ([ a as string ] = [ "test" ]);
// for (a as string in []) {}
// (a as B<string>) = { a: "test" };
// (<number> a) += 1

// test_err ts ts_as_assignment_no_parenthesize
// let a: any;
// a as string = "string";
// (a() as string) = "string";
// <number> a = 3;

// test ts ts_satisfies_assignment
// let a: any;
// type B<A> = { a: A };
// (a satisfies string) = "string";
// ((a satisfies any) satisfies string) = null;
// ({ b: a satisfies string } = { b: "test" });
// ([ a satisfies string ] = [ "test" ]);
// for (a satisfies string in []) {}
// (a satisfies B<string>) = { a: "test" };

// test_err ts ts_satisfies_assignment_no_parenthesize
// let a: any;
// a satisfies string = "string";
// (a() satisfies string) = "string";

/// Converts the passed in lhs expression to an assignment pattern
/// The passed checkpoint allows to restore the parser to the state before it started parsing the expression.
pub(crate) fn expression_to_assignment_pattern(
    p: &mut JsParser,
    target: CompletedMarker,
    checkpoint: JsParserCheckpoint,
) -> CompletedMarker {
    match target.kind(p) {
        JS_OBJECT_EXPRESSION => {
            p.rewind(checkpoint);
            ObjectAssignmentPattern.parse_object_pattern(p).unwrap()
        }
        JS_ARRAY_EXPRESSION => {
            p.rewind(checkpoint);
            ArrayAssignmentPattern.parse_array_pattern(p).unwrap()
        }
        _ => expression_to_assignment(p, target, checkpoint),
    }
}

// test js array_or_object_member_assignment
// [{
//   get y() {
//     throw new Test262Error('The property should not be accessed.');
//   },
//   set y(val) {
//     setValue = val;
//   }
// }.y = 42] = [23];
// ({ x: {
//   get y() {
//     throw new Test262Error('The property should not be accessed.');
//   },
//   set y(val) {
//     setValue = val;
//   }
// }.y = 42 } = { x: 23 });
pub(crate) fn parse_assignment_pattern(p: &mut JsParser) -> ParsedSyntax {
    let checkpoint = p.checkpoint();
    let assignment_expression = parse_conditional_expr(p, ExpressionContext::default());

    assignment_expression
        .map(|expression| expression_to_assignment_pattern(p, expression, checkpoint))
}

/// Re-parses an expression as an assignment.
pub(crate) fn expression_to_assignment(
    p: &mut JsParser,
    target: CompletedMarker,
    checkpoint: JsParserCheckpoint,
) -> CompletedMarker {
    try_expression_to_assignment(p, target, checkpoint).unwrap_or_else(
        // test_err js js_regex_assignment
        // /=0*_:m/=/*_:|
        |mut invalid_assignment_target| {
            // Doesn't seem to be a valid assignment target. Recover and create an error.
            invalid_assignment_target.change_kind(p, JS_BOGUS_ASSIGNMENT);

            p.error(invalid_assignment_error(
                p,
                invalid_assignment_target.range(p),
            ));

            invalid_assignment_target
        },
    )
}

pub(crate) enum AssignmentExprPrecedence {
    Unary,
    Conditional,
}

impl AssignmentExprPrecedence {
    fn parse_expression(&self, p: &mut JsParser, context: ExpressionContext) -> ParsedSyntax {
        match self {
            AssignmentExprPrecedence::Unary => parse_unary_expr(p, context),
            AssignmentExprPrecedence::Conditional => parse_conditional_expr(p, context),
        }
    }
}

pub(crate) fn parse_assignment(
    p: &mut JsParser,
    expr_kind: AssignmentExprPrecedence,
    context: ExpressionContext,
) -> ParsedSyntax {
    let checkpoint = p.checkpoint();
    let assignment_expression = expr_kind.parse_expression(p, context);

    assignment_expression.map(|expr| expression_to_assignment(p, expr, checkpoint))
}

struct ArrayAssignmentPatternElement;

impl ParseWithDefaultPattern for ArrayAssignmentPatternElement {
    #[inline]
    fn pattern_with_default_kind() -> JsSyntaxKind {
        JS_ARRAY_ASSIGNMENT_PATTERN_ELEMENT
    }

    #[inline]
    fn expected_pattern_error(p: &JsParser, range: TextRange) -> ParseDiagnostic {
        expected_assignment_target(p, range)
    }

    #[inline]
    fn parse_pattern(&self, p: &mut JsParser) -> ParsedSyntax {
        parse_assignment_pattern(p)
    }
}

struct ArrayAssignmentPattern;

// test js array_assignment_target
// [foo, bar] = baz;
// [,,,b,,c,] = baz;
// [a = "test", a.b, call().b] = baz;
// [((a))] = baz;
//
// test_err js array_assignment_target_err
// [a a, ++b, ] = test;
// [a, c, ...rest,] = test;
// [a = , = "test"] = test;
// [[a b] [c]]= test;
// [a: b] = c
impl ParseArrayPattern<ArrayAssignmentPatternElement> for ArrayAssignmentPattern {
    #[inline]
    fn bogus_pattern_kind() -> JsSyntaxKind {
        JS_BOGUS_ASSIGNMENT
    }

    #[inline]
    fn array_pattern_kind() -> JsSyntaxKind {
        JS_ARRAY_ASSIGNMENT_PATTERN
    }

    // test js array_assignment_target_rest
    // ([ ...abcd ] = a);
    // ([ ...(abcd) ] = a);
    // ([ ...m.test ] = c);
    // ([ ...m[call()] ] = c);
    // ([ ...any.expression().b ] = c);
    // ([ ...[x, y] ] = b);
    // ([ ...[ ...a ] ] = c);
    //
    // test_err js array_assignment_target_rest_err
    // ([ ... ] = a);
    // ([ ...c = "default" ] = a);
    // ([ ...rest, other_assignment ] = a);
    #[inline]
    fn rest_pattern_kind() -> JsSyntaxKind {
        JS_ARRAY_ASSIGNMENT_PATTERN_REST_ELEMENT
    }

    fn list_kind() -> JsSyntaxKind {
        JS_ARRAY_ASSIGNMENT_PATTERN_ELEMENT_LIST
    }

    #[inline]
    fn expected_element_error(p: &JsParser, range: TextRange) -> ParseDiagnostic {
        expected_any(&["assignment target", "rest element", "comma"], range, p)
    }

    #[inline]
    fn pattern_with_default(&self) -> ArrayAssignmentPatternElement {
        ArrayAssignmentPatternElement
    }
}

struct ObjectAssignmentPattern;

// test js object_assignment_target
// ({} = {});
// ({ bar, baz } = {});
// ({ bar: [baz = "baz"], foo = "foo", ...rest } = {});
impl ParseObjectPattern for ObjectAssignmentPattern {
    #[inline]
    fn bogus_pattern_kind() -> JsSyntaxKind {
        JS_BOGUS_ASSIGNMENT
    }

    #[inline]
    fn object_pattern_kind() -> JsSyntaxKind {
        JS_OBJECT_ASSIGNMENT_PATTERN
    }

    fn list_kind() -> JsSyntaxKind {
        JS_OBJECT_ASSIGNMENT_PATTERN_PROPERTY_LIST
    }

    #[inline]
    fn expected_property_pattern_error(p: &JsParser, range: TextRange) -> ParseDiagnostic {
        expected_any(&["assignment target", "rest property"], range, p)
    }

    // test js property_assignment_target
    // ({x}= {});
    // ({x: y}= {});
    // ({x: y.test().z}= {});
    // ({x: ((z))}= {});
    // ({x: z["computed"]}= {});
    // ({x = "default"}= {});
    // ({x: y = "default"}= {});
    // ({0: y, [computed]: z} = {});
    //
    // test_err js property_assignment_target_err
    // ({:y} = {});
    // ({=y} = {});
    // ({:="test"} = {});
    // ({:=} = {});
    // ({ a b } = {});
    fn parse_property_pattern(&self, p: &mut JsParser) -> ParsedSyntax {
        let m = p.start();

        let kind = if (is_at_identifier(p) || p.at(T![=])) && !p.nth_at(1, T![:]) {
            parse_assignment(
                p,
                AssignmentExprPrecedence::Conditional,
                ExpressionContext::default(),
            )
            .or_add_diagnostic(p, expected_identifier);
            JS_OBJECT_ASSIGNMENT_PATTERN_SHORTHAND_PROPERTY
        } else if is_at_object_member_name(p) || p.at(T![:]) || p.nth_at(1, T![:]) {
            parse_object_member_name(p).or_add_diagnostic(p, expected_object_member_name);
            p.expect(T![:]);
            parse_assignment_pattern(p).or_add_diagnostic(p, expected_assignment_target);
            JS_OBJECT_ASSIGNMENT_PATTERN_PROPERTY
        } else {
            m.abandon(p);
            return Absent;
        };

        parse_initializer_clause(p, ExpressionContext::default()).ok();

        Present(m.complete(p, kind))
    }

    // test js rest_property_assignment_target
    // ({ ...abcd } = a);
    // ({ ...(abcd) } = a);
    // ({ ...m.test } = c);
    // ({ ...m[call()] } = c);
    // ({ ...any.expression().b } = c);
    // ({ b: { ...a } } = c);
    //
    // test_err js rest_property_assignment_target_err
    // ({ ... } = a);
    // ({ ...c = "default" } = a);
    // ({ ...{a} } = b);
    // ({ ...rest, other_assignment } = a);
    // ({ ...rest, } = a);
    fn parse_rest_property_pattern(&self, p: &mut JsParser) -> ParsedSyntax {
        if !p.at(T![...]) {
            return Absent;
        }

        let m = p.start();
        p.bump(T![...]);

        let target = parse_assignment_pattern(p).or_add_diagnostic(p, expected_assignment_target);

        if let Some(mut target) = target {
            if matches!(
                target.kind(p),
                JS_OBJECT_ASSIGNMENT_PATTERN | JS_ARRAY_ASSIGNMENT_PATTERN
            ) {
                target.change_kind(p, JS_BOGUS_ASSIGNMENT);
                p.error(p.err_builder(
                    "object and array assignment targets are not allowed in rest patterns",
                    target.range(p),
                ));
            }
        }

        Present(m.complete(p, JS_OBJECT_ASSIGNMENT_PATTERN_REST))
    }
}

fn try_expression_to_assignment(
    p: &mut JsParser,
    target: CompletedMarker,
    checkpoint: JsParserCheckpoint,
) -> Result<CompletedMarker, CompletedMarker> {
    if !matches!(
        target.kind(p),
        JS_PARENTHESIZED_EXPRESSION
            | JS_STATIC_MEMBER_EXPRESSION
            | JS_COMPUTED_MEMBER_EXPRESSION
            | JS_IDENTIFIER_EXPRESSION
            | TS_NON_NULL_ASSERTION_EXPRESSION
            | TS_TYPE_ASSERTION_EXPRESSION
            | TS_AS_EXPRESSION
            | TS_SATISFIES_EXPRESSION
    ) {
        return Err(target);
    }

    // At this point it's guaranteed that the root node can be mapped to an assignment,
    // but it's not yet guaranteed if it is valid or not (for example, a static member expression
    // is valid, except if it uses optional chaining).
    let mut reparse_assignment = ReparseAssignment::new();
    rewrite_events(&mut reparse_assignment, checkpoint, p);

    Ok(reparse_assignment.result.unwrap())
}

struct ReparseAssignment {
    // Stores the unfinished parents
    // Index 0: Re-mapped kind of the node
    // Index 1: Started marker. A `None` marker means that this node should be dropped
    //          from the re-written tree
    parents: Vec<(JsSyntaxKind, Option<RewriteMarker>)>,
    // Stores the completed assignment node (valid or invalid).
    result: Option<CompletedMarker>,
    // Tracks if the visitor is still inside an assignment
    inside_assignment: bool,
}

impl ReparseAssignment {
    pub fn new() -> Self {
        Self {
            parents: Vec::default(),
            result: None,
            inside_assignment: true,
        }
    }
}

/// Rewrites expressions to assignments
/// * Converts parenthesized expression to parenthesized assignment
/// * Converts computed/static member expressions to computed/static member assignment.
///   Validates that the operator isn't `?.` .
/// * Converts identifier expressions to identifier assignment, drops the inner reference identifier
impl RewriteParseEvents for ReparseAssignment {
    fn start_node(&mut self, kind: JsSyntaxKind, p: &mut RewriteParser) {
        if !self.inside_assignment {
            self.parents.push((kind, Some(p.start())));
            return;
        }

        // Make sure to also add the kind to the match in `try_expression_to_assignment`
        let mapped_kind = match kind {
            JS_PARENTHESIZED_EXPRESSION => JS_PARENTHESIZED_ASSIGNMENT,
            JS_STATIC_MEMBER_EXPRESSION => {
                self.inside_assignment = false;
                JS_STATIC_MEMBER_ASSIGNMENT
            }
            JS_COMPUTED_MEMBER_EXPRESSION => {
                self.inside_assignment = false;
                JS_COMPUTED_MEMBER_ASSIGNMENT
            }
            JS_IDENTIFIER_EXPRESSION => JS_IDENTIFIER_ASSIGNMENT,
            TS_NON_NULL_ASSERTION_EXPRESSION => TS_NON_NULL_ASSERTION_ASSIGNMENT,
            TS_AS_EXPRESSION => TS_AS_ASSIGNMENT,
            TS_SATISFIES_EXPRESSION => TS_SATISFIES_ASSIGNMENT,
            TS_TYPE_ASSERTION_EXPRESSION => TS_TYPE_ASSERTION_ASSIGNMENT,
            JS_REFERENCE_IDENTIFIER => {
                self.parents.push((kind, None)); // Omit reference identifiers
                return;
            }
            _ => {
                self.inside_assignment = false;
                if AnyTsType::can_cast(kind)
                    && matches!(
                        self.parents.last(),
                        Some((
                            TS_AS_ASSIGNMENT
                                | TS_SATISFIES_ASSIGNMENT
                                | TS_TYPE_ASSERTION_ASSIGNMENT,
                            _
                        ))
                    )
                {
                    kind
                } else {
                    JS_BOGUS_ASSIGNMENT
                }
            }
        };

        self.parents.push((mapped_kind, Some(p.start())));
    }

    fn finish_node(&mut self, p: &mut RewriteParser) {
        let (kind, m) = self.parents.pop().unwrap();

        if let Some(m) = m {
            let mut completed = m.complete(p, kind);

            match kind {
                JS_IDENTIFIER_ASSIGNMENT => {
                    // test_err js eval_arguments_assignment
                    // eval = "test";
                    // arguments = "test";
                    let name = completed.text(p);
                    if matches!(name, "eval" | "arguments") && p.is_strict_mode() {
                        let error = p.err_builder(
                            format!("Illegal use of `{}` as an identifier in strict mode", name),
                            completed.range(p),
                        );
                        p.error(error);

                        completed.change_to_bogus(p);
                    }
                }
                JS_BOGUS_ASSIGNMENT => {
                    let range = completed.range(p);
                    p.error(
                        p.err_builder(
                            format!("Invalid assignment to `{}`", completed.text(p)),
                            range,
                        )
                        .with_hint("This expression cannot be assigned to"),
                    );
                }
                _ => {}
            }

            self.result = Some(completed.into());
        }

        if AnyTsType::can_cast(kind)
            && matches!(
                self.parents.last(),
                Some((
                    TS_TYPE_ASSERTION_ASSIGNMENT | TS_AS_ASSIGNMENT | TS_SATISFIES_ASSIGNMENT,
                    _
                ))
            )
        {
            self.inside_assignment = true;
        }
    }

    fn token(&mut self, token: RewriteToken, p: &mut RewriteParser) {
        let parent = self.parents.last_mut();

        if let Some((parent_kind, _)) = parent {
            if matches!(
                *parent_kind,
                JS_COMPUTED_MEMBER_ASSIGNMENT | JS_STATIC_MEMBER_ASSIGNMENT
            ) && token.kind == T![?.]
            {
                *parent_kind = JS_BOGUS_ASSIGNMENT
            }
        }

        p.bump(token)
    }
}