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