swc_ecma_transforms_react/jsx/
static_check.rs

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
use swc_ecma_ast::*;

/// We want to use React.createElement, even in the case of
/// jsx, for <div {...props} key={key} /> to distinguish it
/// from <div key={key} {...props} />. This is an intermediary
/// step while we deprecate key spread from props. Afterwards,
/// we will stop using createElement in the transform.
pub(super) fn should_use_create_element(attrs: &[JSXAttrOrSpread]) -> bool {
    let mut seen_prop_spread = false;
    for attr in attrs {
        if seen_prop_spread
            && match attr {
                JSXAttrOrSpread::JSXAttr(attr) => match &attr.name {
                    JSXAttrName::Ident(i) => i.sym == "key",
                    JSXAttrName::JSXNamespacedName(_) => false,
                },
                _ => false,
            }
        {
            return true;
        }

        if let JSXAttrOrSpread::SpreadElement(_) = attr {
            seen_prop_spread = true;
        }
    }

    false
}