big-code-analysis-ast 2.6.0

Parse and classification layer behind big-code-analysis (internal, version-locked)
Documentation
//! `Getter` implementation for TypeScript.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]

use super::*;

const GENERIC_ANGLES: GenericAngleKinds = GenericAngleKinds {
    lists: &[
        Typescript::TypeArguments as u16,
        Typescript::TypeParameters as u16,
    ],
    openers: &[Typescript::LT as u16],
    closers: &[Typescript::GT as u16],
    is_misparse: never_misparsed,
};

impl Getter for TypescriptCode {
    // The mapping is the `opens` annotations of `roles/typescript.rs`
    // (#1696): every function form and the ES2022 `static { … }` block
    // (#1184) open a `Function` space, a class in its three spellings a
    // `Class`, an interface an `Interface`. The rows read no bytes; the
    // binding gates read the chain, but none of them decides an
    // `opens`, so the byte-less spelling forwards with no chain and
    // answers the same.
    impl_byte_less_space_kind!(typescript);

    fn get_space_kind_with_code<'a>(
        node: &Node<'a>,
        code: &[u8],
        ancestors: Ancestors<'a, '_>,
    ) -> SpaceKind {
        crate::roles::typescript::opens_space(node, code, ancestors)
    }

    fn get_func_space_name<'a, 'tree>(
        node: &Node<'tree>,
        code: &'a [u8],
        ancestors: Ancestors<'tree, '_>,
    ) -> Option<&'a str> {
        // A class static block has no name token and no naming parent to
        // fall back on, so it would otherwise land on `<anonymous>`
        // alongside every arrow and function expression (#1184).
        // role-bypass: allow(FuncOpener) — a naming rule, not whether
        // the block opens a space
        if node.kind_id() == Typescript::ClassStaticBlock as u16 {
            return Some("<static-init>");
        }
        if let Some(name) = node.child_by_field_name("name") {
            return node_text(code, &name);
        }
        // Otherwise the name comes from the binding site: a pair
        // (`foo: function () {}`) or a variable declaration
        // (`var aFun = function () {}`). The two differ only in which
        // field carries the name, so they collapse to one lookup.
        let bound_name = ancestors.parent(node).and_then(|parent| {
            // role-bypass: allow(DocTarget) — which field of the parent carries the
            // function's name; naming, not whether the pair takes a doc comment
            let field = match parent.kind_id().into() {
                Typescript::Pair => "key",
                Typescript::VariableDeclarator => "name",
                _ => return None,
            };
            parent.child_by_field_name(field)
        });
        bound_name.map_or(Some("<anonymous>"), |name| node_text(code, &name))
    }

    // TypeScript's only operand extra is `TypeIdentifier` (#1557,
    // below). `NestedIdentifier` and `MemberExpression4` — the TS-only
    // member-expression productions — were listed until #1263 and are
    // now deliberately absent, matching the `MemberExpression*` drop in
    // the macro body: `namespace N.M` contributes the operands `N` and
    // `M` plus the `.` operator, and `a.b` contributes `a` and `b`,
    // never the composite text as well. The composite was billed on top
    // of leaves the walker already reached, which is grammar-dispatch
    // section 5's container/leaf double-count.
    //
    // TS's anonymous `"string"` alias `String2` (kind_id 135, the
    // `: string` type keyword, emitted only as the child of a
    // `predefined_type` wrapper) is deliberately NOT an operand either:
    // the wrapper already counts as the text-keyed `"string"` operator
    // via `is_primitive`, so #313's listing of the child too counted one
    // source token as operator AND operand — the mirror image of the
    // #453 `void` collision — while `: number` / `: boolean` counted
    // once (#1261). `Checker::is_string` no longer matches the keyword
    // either, so the #313 parity rationale is retired rather than
    // contradicted.
    //
    // No `jsx:` argument: the `.ts` grammar's enum carries `JsxText` in
    // its shared externals, but no production emits it (#1483).
    //
    // The type-declaration keywords (`interface`, `enum`, `type`,
    // `namespace`, `module`, `declare`, `global`), the member modifiers
    // (`public` … `accessor`) and `predefined_type`, with their gates,
    // are the `DeclKeyword` / `Modifier` / `Primitive` rows of
    // `roles/typescript.rs` (#1693), which carry the #1552 / #1557 /
    // #1563 / #453 derivations; `roles:` names that module.
    //
    // `TypeIdentifier` is the operand extra: every type *name* — a
    // class, interface or alias name, a type parameter, an annotation
    // (`x: Foo`), a generic (`Map<K, V>`), the tail of `ns.T` — is this
    // one leaf kind, and it was in neither arm before #1557, so TS lost
    // every one while C#, Rust and the C family bill theirs. No
    // classified node wraps it (`generic_type` and
    // `nested_type_identifier` are unlisted), so it bills once.
    //
    // #1561 adds the type-level operator keywords, the siblings of the
    // `as` / `typeof` / `extends` the macro already bills: `satisfies`,
    // `keyof`, `infer`, the type-predicate `is` and `asserts`. Each is
    // its keyword leaf; the `satisfies_expression`, `index_type_query`,
    // `infer_type`, `type_predicate` and `asserts` wrappers stay
    // unlisted, so one keyword bills once. `Asserts2` is the keyword
    // token and the unsuffixed `Asserts` the node wrapping it, so
    // `asserts a is T` bills `asserts` and `is` once each. Which of the
    // two bills is unobservable — every node holds exactly one token and
    // both render `asserts` — so the leaf is kept, as everywhere else
    // here; listing both is what the tests catch. Used as names
    // (`const keyof = 1`, `o.is`, a `satisfies` parameter) the words
    // parse as identifiers. One valid spelling the pinned grammar gets
    // wrong is `let satisfies = 1`: it reads `let` as an identifier and
    // recovers a `satisfies_expression` around an ERROR, so that
    // statement is mis-scored whatever this list holds.
    //
    // `generic_angles`: a type-argument or type-parameter list's `<`
    // bills as the `<>` pair and its `>` as nothing (#1559). A `<T>x`
    // type assertion wraps a `type_arguments` node, so it is a pair too;
    // a comparison's `<` / `>` sit under `binary_expression` and stay
    // their own operators.
    //
    impl_js_family_get_op_type!(
        Typescript,
        roles: typescript,
        op_extras: [Satisfies, Keyof, Infer, Is, Asserts2],
        operand_extras: [TypeIdentifier],
        generic_angles: GENERIC_ANGLES,
    );

    get_operator!(Typescript);
}