wasm_bindgen_shared/
identifier.rs

1use alloc::string::String;
2
3/// Returns whether a character is a valid JS identifier start character.
4///
5/// This is only ever-so-slightly different from `XID_Start` in a few edge
6/// cases, so we handle those edge cases manually and delegate everything else
7/// to `unicode-ident`.
8fn is_id_start(c: char) -> bool {
9    match c {
10        '\u{037A}' | '\u{0E33}' | '\u{0EB3}' | '\u{309B}' | '\u{309C}' | '\u{FC5E}'
11        | '\u{FC5F}' | '\u{FC60}' | '\u{FC61}' | '\u{FC62}' | '\u{FC63}' | '\u{FDFA}'
12        | '\u{FDFB}' | '\u{FE70}' | '\u{FE72}' | '\u{FE74}' | '\u{FE76}' | '\u{FE78}'
13        | '\u{FE7A}' | '\u{FE7C}' | '\u{FE7E}' | '\u{FF9E}' | '\u{FF9F}' => true,
14        '$' | '_' => true,
15        _ => unicode_ident::is_xid_start(c),
16    }
17}
18
19/// Returns whether a character is a valid JS identifier continue character.
20///
21/// This is only ever-so-slightly different from `XID_Continue` in a few edge
22/// cases, so we handle those edge cases manually and delegate everything else
23/// to `unicode-ident`.
24fn is_id_continue(c: char) -> bool {
25    match c {
26        '\u{037A}' | '\u{309B}' | '\u{309C}' | '\u{FC5E}' | '\u{FC5F}' | '\u{FC60}'
27        | '\u{FC61}' | '\u{FC62}' | '\u{FC63}' | '\u{FDFA}' | '\u{FDFB}' | '\u{FE70}'
28        | '\u{FE72}' | '\u{FE74}' | '\u{FE76}' | '\u{FE78}' | '\u{FE7A}' | '\u{FE7C}'
29        | '\u{FE7E}' => true,
30        '$' | '\u{200C}' | '\u{200D}' => true,
31        _ => unicode_ident::is_xid_continue(c),
32    }
33}
34
35fn maybe_valid_chars(name: &str) -> impl Iterator<Item = Option<char>> + '_ {
36    let mut chars = name.chars();
37    // Always emit at least one `None` item - that way `is_valid_ident` can fail without
38    // a separate check for empty strings, and `to_valid_ident` will always produce at least
39    // one underscore.
40    core::iter::once(chars.next().filter(|&c| is_id_start(c))).chain(chars.map(|c| {
41        if is_id_continue(c) {
42            Some(c)
43        } else {
44            None
45        }
46    }))
47}
48
49/// Returns whether a string is a valid JavaScript identifier.
50/// Defined at https://tc39.es/ecma262/#prod-IdentifierName.
51pub fn is_valid_ident(name: &str) -> bool {
52    maybe_valid_chars(name).all(|opt| opt.is_some())
53}
54
55/// Converts a string to a valid JavaScript identifier by replacing invalid
56/// characters with underscores.
57pub fn to_valid_ident(name: &str) -> String {
58    maybe_valid_chars(name)
59        .map(|opt| opt.unwrap_or('_'))
60        .collect()
61}