big_code_analysis_ast/getter/tsx.rs
1//! `Getter` implementation for TSX.
2#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
3
4use super::*;
5
6const GENERIC_ANGLES: GenericAngleKinds = GenericAngleKinds {
7 lists: &[Tsx::TypeArguments as u16, Tsx::TypeParameters as u16],
8 openers: &[Tsx::LT as u16],
9 closers: &[Tsx::GT as u16],
10 is_misparse: never_misparsed,
11};
12
13impl Getter for TsxCode {
14 // The mapping is the `opens` annotations of `roles/tsx.rs` (#1696);
15 // see `getter/typescript.rs`.
16 impl_byte_less_space_kind!(tsx);
17
18 fn get_space_kind_with_code<'a>(
19 node: &Node<'a>,
20 code: &[u8],
21 ancestors: Ancestors<'a, '_>,
22 ) -> SpaceKind {
23 crate::roles::tsx::opens_space(node, code, ancestors)
24 }
25
26 fn get_func_space_name<'a, 'tree>(
27 node: &Node<'tree>,
28 code: &'a [u8],
29 ancestors: Ancestors<'tree, '_>,
30 ) -> Option<&'a str> {
31 // A class static block has no name token and no naming parent to
32 // fall back on, so it would otherwise land on `<anonymous>`
33 // alongside every arrow and function expression (#1184).
34 // role-bypass: allow(FuncOpener) — a naming rule, not whether
35 // the block opens a space
36 if node.kind_id() == Tsx::ClassStaticBlock as u16 {
37 return Some("<static-init>");
38 }
39 if let Some(name) = node.child_by_field_name("name") {
40 return node_text(code, &name);
41 }
42 // Otherwise the name comes from the binding site: a pair
43 // (`foo: function () {}`) or a variable declaration
44 // (`var aFun = function () {}`). The two differ only in which
45 // field carries the name, so they collapse to one lookup.
46 let bound_name = ancestors.parent(node).and_then(|parent| {
47 // role-bypass: allow(DocTarget) — which field of the parent carries the
48 // function's name; naming, not whether the pair takes a doc comment
49 let field = match parent.kind_id().into() {
50 Tsx::Pair => "key",
51 Tsx::VariableDeclarator => "name",
52 _ => return None,
53 };
54 parent.child_by_field_name(field)
55 });
56 bound_name.map_or(Some("<anonymous>"), |name| node_text(code, &name))
57 }
58
59 // TSX exposes two anonymous `"string"` aliases: `String2` (kind_id
60 // 261, the string-literal alias — ordinary and JSX-attribute
61 // literals both parse as it), which is an operand like any other
62 // string literal, and `String3` (kind_id 141, the `: string` type
63 // keyword — the role TS's `String2` plays, emitted only as the
64 // child of a `predefined_type` wrapper). `String3` is deliberately
65 // NOT an operand: the wrapper already counts as the text-keyed
66 // `"string"` operator, so #313's listing of the child too counted
67 // one source token as operator AND operand while `: number` /
68 // `: boolean` counted once (#1261; see the TS invocation).
69 //
70 // TSX's TS-only member-expression productions `NestedIdentifier`
71 // and `MemberExpression4` were operand extras until #1263 and are
72 // now deliberately absent, matching the `MemberExpression*` drop in
73 // the macro body: a member access or a `namespace N.M` header
74 // contributes its identifier leaves plus the `.` operator, never
75 // the composite text as well.
76 //
77 // `LTSLASH` / `SLASHGT` are the JSX closing and self-closing tag
78 // delimiters. TSX is the only one of the two TypeScript grammars
79 // with JSX, so the `.ts` invocation has no such variants to pass
80 // (#1395), and the same holds for the `jsx:` text kinds (#1483).
81 //
82 // The type-declaration keywords, the member modifiers and
83 // `predefined_type` are the rows of `roles/tsx.rs` (#1693), a copy
84 // of TypeScript's; the `TypeIdentifier` operand: see the TS
85 // invocation (#1557).
86 //
87 // `Satisfies` / `Keyof` / `Infer` / `Is` / `Asserts2`, the type-level
88 // operator keywords: see the TS invocation (#1561).
89 //
90 // `generic_angles`: see the TS invocation (#1559). A JSX tag's `<` /
91 // `>` hang from the JSX element nodes, not from a generic list, so
92 // they stay `<` and `>`.
93 // role-bypass: allow(StringLit) — the operand classification of the literal
94 // alias the `StringLit` row owns, a projection the row does not answer
95 impl_js_family_get_op_type!(
96 Tsx,
97 roles: tsx,
98 op_extras: [Satisfies, Keyof, Infer, Is, Asserts2, LTSLASH, SLASHGT],
99 operand_extras: [Identifier2, String2, TypeIdentifier],
100 generic_angles: GENERIC_ANGLES,
101 jsx: [JsxText, HtmlCharacterReference],
102 );
103
104 get_operator!(Tsx);
105}