big_code_analysis_ast/getter/javascript.rs
1//! `Getter` implementation for JavaScript.
2#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
3
4use super::*;
5
6impl Getter for JavascriptCode {
7 // The mapping is the `opens` annotations of `roles/javascript.rs`
8 // (#1696): every function form and the ES2022 `static { … }` block
9 // (#1184) open a `Function` space, a class either spelling a
10 // `Class`. The rows read no bytes; the binding gates read the
11 // chain, but none of them decides an `opens`, so the byte-less
12 // spelling forwards with no chain and answers the same.
13 impl_byte_less_space_kind!(javascript);
14
15 fn get_space_kind_with_code<'a>(
16 node: &Node<'a>,
17 code: &[u8],
18 ancestors: Ancestors<'a, '_>,
19 ) -> SpaceKind {
20 crate::roles::javascript::opens_space(node, code, ancestors)
21 }
22
23 fn get_func_space_name<'a, 'tree>(
24 node: &Node<'tree>,
25 code: &'a [u8],
26 ancestors: Ancestors<'tree, '_>,
27 ) -> Option<&'a str> {
28 // A class static block has no name token and no naming parent to
29 // fall back on, so it would otherwise land on `<anonymous>`
30 // alongside every arrow and function expression (#1184).
31 // role-bypass: allow(FuncOpener) — a naming rule, not whether
32 // the block opens a space
33 if node.kind_id() == Javascript::ClassStaticBlock as u16 {
34 return Some("<static-init>");
35 }
36 if let Some(name) = node.child_by_field_name("name") {
37 return node_text(code, &name);
38 }
39 // Otherwise the name comes from the binding site: a pair
40 // (`foo: function () {}`) or a variable declaration
41 // (`var aFun = function () {}`). The two differ only in which
42 // field carries the name, so they collapse to one lookup.
43 let bound_name = ancestors.parent(node).and_then(|parent| {
44 // role-bypass: allow(DocTarget) — which field of the parent carries the
45 // function's name; naming, not whether the pair takes a doc comment
46 let field = match parent.kind_id().into() {
47 Javascript::Pair => "key",
48 Javascript::VariableDeclarator => "name",
49 _ => return None,
50 };
51 parent.child_by_field_name(field)
52 });
53 bound_name.map_or(Some("<anonymous>"), |name| node_text(code, &name))
54 }
55
56 // JSX closing / self-closing tag delimiters. See the macro's
57 // `$op_extras` note: they are per-language extras because
58 // TypeScript, which shares this macro, has no JSX (#1395).
59 // `jsx:` bills element text and its entities as operands, for the
60 // same reason (#1483).
61 //
62 // `Staticget` is one token spelling `static get` followed by a
63 // newline, the static getter whose name starts the next line. It is
64 // billed beside the shared `static` (#1563) so that form bills a
65 // modifier rather than nothing; being one node it is one operator.
66 // role-bypass: allow(StringLit) — the operand classification of the literal
67 // alias the `StringLit` row owns, a projection the row does not answer
68 impl_js_family_get_op_type!(
69 Javascript,
70 roles: javascript,
71 op_extras: [LTSLASH, SLASHGT],
72 operand_extras: [Identifier2, String2],
73 jsx: [JsxText, HtmlCharacterReference],
74 );
75
76 get_operator!(Javascript);
77}