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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use crate::prelude::*;
pub mod jsx_parse_errors;

use biome_js_syntax::JsSyntaxKind::*;
use biome_parser::diagnostic::expected_token;
use biome_parser::parse_lists::ParseNodeList;
use biome_rowan::TextRange;

use crate::lexer::{JsLexContext, JsReLexContext, JsSyntaxKind, T};
use crate::syntax::expr::{
    is_nth_at_identifier_or_keyword, parse_expression, parse_name, ExpressionContext,
};
use crate::syntax::js_parse_error::{expected_expression, expected_identifier};
use crate::syntax::jsx::jsx_parse_errors::{
    jsx_expected_attribute, jsx_expected_attribute_value, jsx_expected_children,
    jsx_expected_closing_tag,
};
use crate::syntax::typescript::TypeContext;
use crate::JsSyntaxFeature::TypeScript;
use crate::{parser::RecoveryResult, JsParser, ParseRecoveryTokenSet, ParsedSyntax};
use crate::{Absent, Present};

use super::typescript::parse_ts_type_arguments;

// test jsx jsx_element_on_return
// function f() {
//     return <div></div>
// }

// test jsx jsx_element_on_arrow_function
// const f = () => <div></div>;
// const f = () => (<div></div>);

// test jsx jsx_element_as_statements
// <div />

// test_err js jsx_or_type_assertion
// // SCRIPT
// function f() {
//     let a = <div>a</div>; // JSX
//     let b = <string>b; // type assertion
//     let c = <string>b<a>d; // type assertion
//     let d = <div>a</div>/; // ambiguous: JSX or "type assertion a less than regex /div>/". Probably JSX.
//     let d = <string>a</string>/;
// }

// test jsx jsx_equal_content
// <span></span>;
// <span>=</span>;
pub(crate) fn parse_jsx_tag_expression(p: &mut JsParser) -> ParsedSyntax {
    if !p.at(T![<]) {
        return Absent;
    }

    if !p.nth_at(1, T![>]) && !is_nth_at_identifier_or_keyword(p, 1) {
        return Absent;
    }

    let m = p.start();

    // Safety: Safe because `parse_any_jsx_tag only returns Absent if the parser isn't positioned
    // at the `<` token which is tested for at the beginning of the function.
    parse_any_jsx_tag(p, true).unwrap();
    Present(m.complete(p, JSX_TAG_EXPRESSION))
}

// <a ...> or <a ... />
// ^          ^

// test jsx jsx_element_open_close
// function f() {
//     return <div></div>
// }

// test jsx jsx_element_self_close
// function f() {
//     return <div />
// }

// test jsx jsx_closing_token_trivia
// <closing / /* some comment */ >;
// <open><
// /* some comment */ / open>;

// test_err jsx jsx_invalid_text
// <a> test ></a>;
// <b> invalid }</b>;

/// Parses a JSX tag (fragment or element)
///
/// `in_expression` must be `true` if this element is a direct child of the `JsxElementExpression` (root of an expression).
/// It should be false when parsing any child node.
fn parse_any_jsx_tag(p: &mut JsParser, in_expression: bool) -> ParsedSyntax {
    match parse_any_jsx_opening_tag(p, in_expression) {
        Some(OpeningElement::SelfClosing(marker)) => Present(marker),
        Some(OpeningElement::Fragment(fragment_opening)) => {
            let opening_range = fragment_opening.range(p);
            let fragment = fragment_opening.precede(p);
            parse_jsx_children(p);
            expect_closing_fragment(p, in_expression, opening_range);
            Present(fragment.complete(p, JSX_FRAGMENT))
        }
        Some(OpeningElement::Element { name, opening }) => {
            let opening_range = opening.range(p);
            let element = opening.precede(p);

            parse_jsx_children(p);

            expect_closing_element(p, in_expression, name, opening_range);
            Present(element.complete(p, JSX_ELEMENT))
        }
        None => Absent,
    }
}

enum OpeningElement {
    Fragment(CompletedMarker),
    Element {
        name: Option<CompletedMarker>,
        opening: CompletedMarker,
    },
    SelfClosing(CompletedMarker),
}

fn parse_any_jsx_opening_tag(p: &mut JsParser, in_expression: bool) -> Option<OpeningElement> {
    if !p.at(T![<]) {
        return None;
    }

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

    if p.at(T![>]) {
        // test jsx jsx_fragments
        // <></>;
        // <>abcd</>;
        // <>   whitespace
        // </>;
        // <
        //   /*comment */
        //   >
        //   <
        //   /
        // >;
        p.bump_with_context(T![>], JsLexContext::JsxChild);

        return Some(OpeningElement::Fragment(
            m.complete(p, JSX_OPENING_FRAGMENT),
        ));
    }

    let name = parse_jsx_any_element_name(p).or_add_diagnostic(p, expected_identifier);

    // Don't parse type arguments in JS because it prevents us from doing better error recovery in case the
    // `>` token of the opening element is missing:
    // `<test <inner></test>` The `inner` is it's own element and not the type arguments
    if TypeScript.is_supported(p) {
        // test tsx tsx_element_generics_type
        // <NonGeneric />;
        // <Generic<true> />;
        // <Generic<true>></Generic>;
        let _ = parse_ts_type_arguments(p, TypeContext::default());
    }

    JsxAttributeList.parse_list(p);

    if p.eat(T![/]) {
        // test_err jsx jsx_self_closing_element_missing_r_angle
        // <><test / some test followed by<a /></>;
        expect_jsx_token(p, T![>], !in_expression);

        Some(OpeningElement::SelfClosing(
            m.complete(p, JSX_SELF_CLOSING_ELEMENT),
        ))
    } else {
        // test_err jsx jsx_opening_element_missing_r_angle
        // <><test <inner> some content</inner></test></>
        expect_jsx_token(p, T![>], true);

        Some(OpeningElement::Element {
            opening: m.complete(p, JSX_OPENING_ELEMENT),
            name,
        })
    }
}

fn expect_closing_fragment(
    p: &mut JsParser,
    in_expression: bool,
    opening_range: TextRange,
) -> CompletedMarker {
    let m = p.start();
    p.expect(T![<]);
    p.expect(T![/]);

    // test_err jsx jsx_missing_closing_fragment
    // <>test</test>;
    // <>test<inner> some text</inner>;
    if let Present(name) = parse_jsx_any_element_name(p) {
        p.error(
            p.err_builder(
                "JSX fragment has no corresponding closing tag.",
                opening_range,
            )
            .with_detail(opening_range, "Opening fragment")
            .with_detail(name.range(p), "Closing tag"),
        );
    }

    // test_err jsx jsx_fragment_closing_missing_r_angle
    // <div><>test</ 5 more content</div>
    expect_jsx_token(p, T![>], !in_expression);

    m.complete(p, JSX_CLOSING_FRAGMENT)
}

fn expect_closing_element(
    p: &mut JsParser,
    in_expression: bool,
    opening_name_marker: Option<CompletedMarker>,
    opening_range: TextRange,
) -> CompletedMarker {
    let m = p.start();

    p.expect(T![<]);
    p.expect(T![/]);

    let name_marker = parse_jsx_any_element_name(p);

    // test_err jsx jsx_closing_element_mismatch
    // <test></>;
    // <test></text>;
    // <some><nested></some></nested>;
    // <><5></test></>;
    if let Some(opening_name_marker) = opening_name_marker {
        let opening_name = opening_name_marker.text(p);

        let error = match name_marker {
            Present(name) if name.text(p) != opening_name => {
                let closing_end = if p.at(T![>]) {
                    p.cur_range().end()
                } else {
                    name.range(p).end()
                };

                let closing_range = TextRange::new(m.start(), closing_end);

                Some(jsx_expected_closing_tag(
                    p,
                    opening_name,
                    opening_range,
                    closing_range,
                ))
            }
            Present(_) => None,
            Absent => {
                if p.at(T![>]) {
                    let closing_range = TextRange::new(m.start(), p.cur_range().end());

                    Some(jsx_expected_closing_tag(
                        p,
                        opening_name,
                        opening_range,
                        closing_range,
                    ))
                } else {
                    Some(expected_identifier(p, p.cur_range()))
                }
            }
        };

        if let Some(error) = error {
            p.error(error);
        }
    }

    // test_err jsx jsx_closing_missing_r_angle
    // <><test>abcd</test more content follows here</>
    expect_jsx_token(p, T![>], !in_expression);

    m.complete(p, JSX_CLOSING_ELEMENT)
}

/// Expects a JSX token that may be followed by JSX child content.
/// Ensures that the child content is lexed with the [JsLexContext::JsxChild] context.
fn expect_jsx_token(p: &mut JsParser, token: JsSyntaxKind, before_child_content: bool) {
    if !before_child_content {
        p.expect(token);
    } else if p.at(token) {
        p.bump_with_context(token, JsLexContext::JsxChild);
    } else {
        p.error(expected_token(token));
        // Re-lex the current token as a JSX child.
        p.re_lex(JsReLexContext::JsxChild);
    }
}

struct JsxChildrenList;

impl ParseNodeList for JsxChildrenList {
    type Kind = JsSyntaxKind;
    type Parser<'source> = JsParser<'source>;
    const LIST_KIND: Self::Kind = JsSyntaxKind::JSX_CHILD_LIST;

    fn parse_element(&mut self, p: &mut JsParser) -> ParsedSyntax {
        match p.cur() {
            // test jsx jsx_element_children
            // <a>
            //     <b>
            //        <d></d>
            //        <e></e>
            //     </b>
            //     <c></c>
            // </a>
            T![<] => parse_any_jsx_tag(p, false),
            T!['{'] => parse_jsx_expression_child(p),
            // test jsx jsx_text
            // <a>test</a>;
            // <a>   whitespace handling </a>;
            // <a> multi
            //    line
            //          node
            // </a>;
            // <test>\u3333</test> // no error for invalid unicode escape
            JsSyntaxKind::JSX_TEXT_LITERAL => {
                let m = p.start();
                p.bump(JSX_TEXT_LITERAL);
                ParsedSyntax::Present(m.complete(p, JSX_TEXT))
            }
            _ => ParsedSyntax::Absent,
        }
    }

    fn is_at_list_end(&self, p: &mut JsParser) -> bool {
        let at_l_angle0 = p.at(T![<]);
        let at_slash1 = p.nth_at(1, T![/]);
        at_l_angle0 && at_slash1
    }

    fn recover(&mut self, p: &mut JsParser, parsed_element: ParsedSyntax) -> RecoveryResult {
        parsed_element.or_recover_with_token_set(
            p,
            &ParseRecoveryTokenSet::new(
                JsSyntaxKind::JS_BOGUS,
                token_set![T![<], T![>], T!['{'], T!['}']],
            ),
            jsx_expected_children,
        )
    }
}

#[inline]
fn parse_jsx_children(p: &mut JsParser) {
    JsxChildrenList.parse_list(p);
}

fn parse_jsx_expression_child(p: &mut JsParser) -> ParsedSyntax {
    if !p.at(T!['{']) {
        return ParsedSyntax::Absent;
    }

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

    // test jsx jsx_children_spread
    // <div>{...a}</div>;
    // <div>{...a}After</div>;
    let is_spread = p.eat(T![...]);

    let expr = parse_jsx_assignment_expression(p, is_spread);

    if is_spread {
        // test_err jsx jsx_spread_no_expression
        // <test>{...}</test>
        expr.or_add_diagnostic(p, expected_expression);
    }

    // test jsx jsx_children_expression_then_text
    // <test>
    //     {/* comment */}
    //      some
    //      text
    // </test>

    // test_err jsx jsx_children_expression_missing_r_curly
    // <test>
    //   { 5 + 3
    //   some text
    // </test>
    expect_jsx_token(p, T!['}'], true);

    let kind = if is_spread {
        JsSyntaxKind::JSX_SPREAD_CHILD
    } else {
        JsSyntaxKind::JSX_EXPRESSION_CHILD
    };

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

// test jsx jsx_member_element_name
// <a.b.c.d></a.b.c.d>;
// <a-b.c></a-b.c>;
// <Abcd></Abcd>;
//
// test_err jsx jsx_namespace_member_element_name
// <namespace:a></namespace:a>;
// <namespace:a.b></namespace:a.b>;
fn parse_jsx_any_element_name(p: &mut JsParser) -> ParsedSyntax {
    let name = parse_jsx_name_or_namespace(p);
    name.map(|mut name| {
        if name.kind(p) == JSX_NAME && (p.at(T![.]) || !is_intrinsic_element(name.text(p))) {
            name.change_kind(p, JSX_REFERENCE_IDENTIFIER)
        } else if name.kind(p) == JSX_NAMESPACE_NAME && p.at(T![.]) {
            let error = p.err_builder(
                "JSX property access expressions cannot include JSX namespace names.",
                name.range(p),
            );
            p.error(error);
            name.change_to_bogus(p);
        }

        while p.at(T![.]) {
            let m = name.precede(p);
            p.bump(T![.]);
            parse_name(p).or_add_diagnostic(p, expected_identifier);
            name = m.complete(p, JSX_MEMBER_NAME)
        }

        name
    })
}

/// Tests if this is an intrinsic element name. Intrinsic elements are such elements
/// that are built in, for example HTML elements. This implementation uses React's semantic
/// and assumes that anything starting with a lower case character is an intrinsic element, and
/// that custom components start with an uper case character.
///
/// Resources: [TypeScript's documentation on intrinsic elements](https://www.typescriptlang.org/docs/handbook/jsx.html#intrinsic-elements)
fn is_intrinsic_element(element_name: &str) -> bool {
    if let Some(first) = element_name.chars().next() {
        first.is_lowercase()
    } else {
        false
    }
}

// test jsx jsx_any_name
// <a-b-c-d-e></a-b-c-d-e>;
// <a-b-c-d-e />;
// <if />;
// <namespace:name></namespace:name>;
// <dashed-namespaced:dashed-name />;
fn parse_jsx_name_or_namespace(p: &mut JsParser) -> ParsedSyntax {
    parse_jsx_name(p).map(|identifier| {
        if p.at(T![:]) {
            let m = identifier.precede(p);
            p.bump(T![:]);
            parse_jsx_name(p).or_add_diagnostic(p, expected_identifier);
            m.complete(p, JSX_NAMESPACE_NAME)
        } else {
            identifier
        }
    })
}

fn parse_jsx_name(p: &mut JsParser) -> ParsedSyntax {
    p.re_lex(JsReLexContext::JsxIdentifier);

    if p.at(JSX_IDENT) {
        let name = p.start();
        p.bump(JSX_IDENT);
        Present(name.complete(p, JSX_NAME))
    } else {
        Absent
    }
}

struct JsxAttributeList;
// test jsx jsx_element_attributes
// function f() {
//     return <div string_literal="a" expression={1} novalue el=<a/>></div>;
// }
// <div dashed-name='test' use:validate="abcd" />;
// <div use-dashed_underscore:validate="ahaha" />;
// <div multiline-string='test
//   continues here' />;
// <div invalid-unicode-escape="\u10000\u20000" />;
impl ParseNodeList for JsxAttributeList {
    type Kind = JsSyntaxKind;
    type Parser<'source> = JsParser<'source>;
    const LIST_KIND: Self::Kind = JsSyntaxKind::JSX_ATTRIBUTE_LIST;

    fn parse_element(&mut self, p: &mut JsParser) -> ParsedSyntax {
        if matches!(p.cur(), T!['{'] | T![...]) {
            parse_jsx_spread_attribute(p)
        } else {
            parse_jsx_attribute(p)
        }
    }

    fn is_at_list_end(&self, p: &mut JsParser) -> bool {
        matches!(p.cur(), T![>] | T![/] | T![<])
    }

    fn recover(&mut self, p: &mut JsParser, parsed_element: ParsedSyntax) -> RecoveryResult {
        parsed_element.or_recover_with_token_set(
            p,
            &ParseRecoveryTokenSet::new(
                JsSyntaxKind::JS_BOGUS,
                token_set![T![/], T![>], T![<], T!['{'], T!['}'], T![...], T![ident]],
            ),
            jsx_expected_attribute,
        )
    }
}

fn parse_jsx_attribute(p: &mut JsParser) -> ParsedSyntax {
    if !is_nth_at_identifier_or_keyword(p, 0) {
        return Absent;
    }

    let m = p.start();

    // SAFETY: Guaranteed to succeed because the parser is at an identifier or keyword
    parse_jsx_name_or_namespace(p).unwrap();
    let _ = parse_jsx_attribute_initializer_clause(p);

    Present(m.complete(p, JsSyntaxKind::JSX_ATTRIBUTE))
}

// test jsx jsx_spread_attribute
// let obj = {};
// <a {...obj} />;
//
// test_err jsx jsx_spread_attribute_error
// let obj = {};
// <a {...obj, other} />;
// <a ...obj} />;
// <a {obj} />;
// <div
//       {...{} /*
//       // @ts-ignore */ /* prettier-ignore */
//       invalidProp="HelloWorld"
//     />;
fn parse_jsx_spread_attribute(p: &mut JsParser) -> ParsedSyntax {
    if !matches!(p.cur(), T![...] | T!['{']) {
        return Absent;
    }

    let m = p.start();

    p.expect(T!['{']);
    p.expect(T![...]);

    let argument = parse_expression(p, ExpressionContext::default()).map(|mut expr| {
        if expr.kind(p) == JS_SEQUENCE_EXPRESSION {
            p.error(p.err_builder(
                "Comma operator isn't a valid value for a JSX spread argument.",
                expr.range(p),
            ));
            expr.change_to_bogus(p);
        }

        expr
    });

    argument.or_add_diagnostic(p, expected_expression);

    p.expect(T!['}']);

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

fn parse_jsx_attribute_initializer_clause(p: &mut JsParser) -> ParsedSyntax {
    if !p.at(T![=]) {
        return Absent;
    }

    let m = p.start();

    p.bump_with_context(T![=], JsLexContext::JsxAttributeValue);

    // test_err jsx jsx_element_attribute_missing_value
    // function f() {
    //     return <div string_literal= ></div>;
    // }
    parse_jsx_attribute_value(p).or_add_diagnostic(p, jsx_expected_attribute_value);

    ParsedSyntax::Present(m.complete(p, JsSyntaxKind::JSX_ATTRIBUTE_INITIALIZER_CLAUSE))
}

fn parse_jsx_attribute_value(p: &mut JsParser) -> ParsedSyntax {
    match p.cur() {
        // test jsx jsx_element_attribute_expression
        // <div id={1} />;
        // <div className={prefix`none`} />;
        T!['{'] => parse_jsx_expression_attribute_value(p),
        // test jsx jsx_element_attribute_element
        // <div id=<a/> />;
        T![<] => parse_any_jsx_tag(p, true),
        // test jsx jsx_element_attribute_string_literal
        // <div id="a" />;
        JsSyntaxKind::JSX_STRING_LITERAL => {
            let m = p.start();
            p.bump(JSX_STRING_LITERAL);
            ParsedSyntax::Present(m.complete(p, JSX_STRING))
        }
        _ => ParsedSyntax::Absent,
    }
}
// test_err jsx jsx_element_attribute_expression_error
// <div className={asdf asdf} />;
fn parse_jsx_expression_attribute_value(p: &mut JsParser) -> ParsedSyntax {
    if !p.at(T!['{']) {
        return ParsedSyntax::Absent;
    }

    let m = p.start();
    p.bump(T!['{']);
    parse_jsx_assignment_expression(p, false).or_add_diagnostic(p, expected_expression);
    if !p.expect(T!['}']) && p.nth_at(1, T!['}']) {
        p.parse_as_skipped_trivia_tokens(|p| {
            p.bump_any();
        });
        p.expect(T!['}']);
    }

    ParsedSyntax::Present(m.complete(p, JSX_EXPRESSION_ATTRIBUTE_VALUE))
}

// test jsx jsx_children_expression
// let x;
// let a;
// let b;
// let key;
// let f = () => {};
// <div>
//   {1}
//   {9007199254740991n}
//   {""}
//   {true}
//   {null}
//   {undefined}
//   {/a/}
//   {[]}
//   {x => console.log(x)}
//   {x = 1}
//   {await x}
//   {1 + 1}
//   {f()}
//   {a[b]}
//   {a?1:2}
//   {function f() {}}
//   {function () {}}
//   {a}
//   {import("a.js")}
//   {key in a}
//   {a instanceof Object}
//   {a && b}
//   {new f()}
//   {{}}
//   {(a)}
//   {a++}
//   {++a}
//   {a,b}
//   {a.b}
//   {super.a()}
//   {this}
//   {delete a.a}
//   {void a}
//   {typeof a}
//   {+a}
//   {-a}
//   {!a}
//   {~a}
//   {``}
//   {/* A JSX comment */}
//   {/* Multi
//       line
//   */}
//   {}
// </div>
// function *f() {
//     return <div>
//         {yield a}
//     </div>;
// }

// test_err jsx jsx_children_expressions_not_accepted
// <div>
//   {import.meta}
//   {class A{}}
//   {super()}
//   {new.target}
// </div>
fn parse_jsx_assignment_expression(p: &mut JsParser, is_spread: bool) -> ParsedSyntax {
    let expr = parse_expression(p, ExpressionContext::default());

    expr.map(|mut expr| {
        let msg = if is_spread {
            "This expression is not valid as a JSX spread expression"
        } else {
            "This expression is not valid as a JSX expression."
        };

        let err = match expr.kind(p) {
            JsSyntaxKind::JS_IMPORT_META_EXPRESSION
            | JsSyntaxKind::JS_NEW_TARGET_EXPRESSION
            | JsSyntaxKind::JS_CLASS_EXPRESSION => Some(p.err_builder(msg, expr.range(p))),
            JsSyntaxKind::JS_SEQUENCE_EXPRESSION if is_spread => {
                Some(p.err_builder(msg, expr.range(p)))
            }
            _ => None,
        };

        if let Some(err) = err {
            p.error(err);
            expr.change_to_bogus(p);
        }

        expr
    })
}