vvv-lang 0.2.0

Language support for vvv: the ast-grep adapter and one module per language
Documentation
//! What in tree-sitter-typescript's grammar counts as a declaration or an
//! identifier, and what its modifiers mean.

use vvv_core::HighlightKind as H;
use vvv_core::ModifierAt::Parent;
use vvv_core::ReachKind as R;
use vvv_core::SignatureRule;
use vvv_core::SymbolKind::{self, *};
use vvv_core::{
    Grammar, HighlightRule, ImportGrammar, ImportRule, PathSyntax, ReExportRule, Semantics,
    SymbolRule, VisibilityRule,
};

/// Comments (JSDoc included) and decorators above a declaration belong to it.
const LEADING: &[&str] = &["comment", "decorator"];

/// A top-level declaration: `export` wraps it in an `export_statement`.
const fn decl(node: &'static str, kind: SymbolKind) -> SymbolRule {
    SymbolRule::new(node, "name", kind)
        .leading(LEADING)
        .visibility(Parent("export_statement"))
        .move_scope_wrappers(&["export_statement", "ambient_declaration"])
}

const SYMBOLS: &[SymbolRule] = &[
    decl("function_declaration", Function),
    decl("function_signature", Function).movable(false),
    decl("generator_function_declaration", Function),
    SymbolRule::new("method_definition", "name", Method).leading(LEADING),
    SymbolRule::new("method_signature", "name", Method).leading(LEADING),
    SymbolRule::new("abstract_method_signature", "name", Method).leading(LEADING),
    decl("class_declaration", Class),
    decl("abstract_class_declaration", Class),
    decl("interface_declaration", Interface),
    decl("type_alias_declaration", TypeAlias),
    decl("enum_declaration", Enum),
    SymbolRule::new("enum_assignment", "name", Variant).leading(LEADING),
    SymbolRule::self_named("property_identifier", Variant).under("enum_body"),
    decl("variable_declarator", Variable).movable(false),
    SymbolRule::new("public_field_definition", "name", Field).leading(LEADING),
    SymbolRule::new("property_signature", "name", Field).leading(LEADING),
];

pub(crate) const SEMANTICS: Semantics = Semantics {
    // `import { X } from './b'` hides which names were taken, so a file import
    // is treated as opening the module.
    import_scopes_names: true,
    addressable: &[Function, Class, Interface, TypeAlias, Enum, Variable],
    // A file is a module: nothing crosses it without `export`.
    visibility: &[
        VisibilityRule::exact("export", R::Everyone),
        VisibilityRule::exact("export default", R::Everyone),
    ],
    default_visibility: R::Declaring,
};

const IDENTIFIERS: &[&str] = &[
    "identifier",
    "type_identifier",
    "property_identifier",
    "shorthand_property_identifier",
    "shorthand_property_identifier_pattern",
];

const IMPORTS: ImportGrammar = ImportGrammar::new(
    PathSyntax::Posix,
    &[
        ImportRule::field("import_statement", "source"),
        ImportRule::field("export_statement", "source"),
        ImportRule::pattern("import($PATH)", "PATH"),
        ImportRule::pattern("require($PATH)", "PATH"),
    ],
)
.reexports(ReExportRule::Statement("export_statement"));

const HIGHLIGHTS: &[HighlightRule] = &[
    HighlightRule::new("comment", H::Comment),
    HighlightRule::new("string", H::String),
    HighlightRule::new("template_string", H::String),
    HighlightRule::new("regex", H::String),
    HighlightRule::new("number", H::Number),
    HighlightRule::new("true", H::Number),
    HighlightRule::new("false", H::Number),
    HighlightRule::new("null", H::Number),
    HighlightRule::new("undefined", H::Number),
    HighlightRule::new("type_identifier", H::Type),
    HighlightRule::new("predefined_type", H::Type),
    HighlightRule::new("decorator", H::Attribute),
    HighlightRule::new("identifier", H::Function).under("function_declaration"),
    HighlightRule::new("identifier", H::Function).under("call_expression"),
    HighlightRule::new("property_identifier", H::Function).under("method_definition"),
    HighlightRule::new("property_identifier", H::Function).under("method_signature"),
    HighlightRule::new("this", H::Keyword),
    HighlightRule::new("super", H::Keyword),
];

pub(crate) const GRAMMAR: Grammar = Grammar {
    import_scopes: &[],
    binding_markers: &[],
    module_scopes: None,
    macro_scopes: None,
    signatures: &[
        SignatureRule::callable("variable_declarator", "value", "body"),
        SignatureRule::callable("public_field_definition", "value", "body"),
        SignatureRule::header("function_expression", "body"),
        SignatureRule::header("generator_function", "body"),
        SignatureRule::header("function_declaration", "body"),
        SignatureRule::header("generator_function_declaration", "body"),
        SignatureRule::header("method_definition", "body"),
        SignatureRule::whole("method_signature"),
        SignatureRule::whole("abstract_method_signature"),
        SignatureRule::header("class_declaration", "body"),
        SignatureRule::header("abstract_class_declaration", "body"),
        SignatureRule::header("interface_declaration", "body"),
        SignatureRule::header("enum_declaration", "body"),
        SignatureRule::whole("type_alias_declaration"),
        SignatureRule::whole("property_signature"),
    ],
    navigation_values: &["identifier"],
    calls: &[vvv_core::CallRule {
        node: "call_expression",
        callee: "function",
    }],
    callees: &[vvv_core::CalleeRule {
        node: "member_expression",
        field: "property",
        kind: Some(vvv_core::CallKind::Member),
    }],
    anonymous_callables: &[
        "arrow_function",
        "function_expression",
        "generator_function",
    ],
    pattern_constructors: &[],
    pattern_containers: &[],
    qualified_imports: &[
        vvv_core::QualifiedImportRule {
            node: "member_expression",
            object: "object",
            member: "property",
        },
        vvv_core::QualifiedImportRule {
            node: "nested_type_identifier",
            object: "module",
            member: "name",
        },
    ],
    bindings: &[
        vvv_core::BindingRule {
            node: "type_parameter",
            name: Some("name"),
            scopes: &[
                "function_declaration",
                "function_signature",
                "class_declaration",
                "class",
                "abstract_class_declaration",
                "function_type",
                "method_signature",
                "call_signature",
                "construct_signature",
                "constructor_type",
                "abstract_method_signature",
                "interface_declaration",
                "type_alias_declaration",
                "method_definition",
                "arrow_function",
                "function_expression",
                "generator_function_declaration",
                "generator_function",
            ],
            kind: TypeParameter,
            namespace: vvv_core::BindingNamespace::Type,
            after: false,
        },
        vvv_core::BindingRule {
            node: "required_parameter",
            name: Some("pattern"),
            scopes: &[
                "function_declaration",
                "function_signature",
                "method_definition",
                "arrow_function",
                "function_expression",
                "generator_function_declaration",
                "generator_function",
            ],
            kind: Parameter,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "optional_parameter",
            name: Some("pattern"),
            scopes: &[
                "function_declaration",
                "function_signature",
                "method_definition",
                "arrow_function",
                "function_expression",
                "generator_function_declaration",
                "generator_function",
            ],
            kind: Parameter,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "arrow_function",
            name: Some("parameter"),
            scopes: &["arrow_function"],
            kind: Parameter,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "function_expression",
            name: Some("name"),
            scopes: &["function_expression"],
            kind: Function,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "generator_function",
            name: Some("name"),
            scopes: &["generator_function"],
            kind: Function,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "catch_clause",
            name: Some("parameter"),
            scopes: &["catch_clause"],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        // The construct-aware adapter handles direct let/const declarations.
        // Table fallback and other binding forms retain conservative barriers.
        vvv_core::BindingRule {
            node: "lexical_declaration",
            name: None,
            scopes: &["statement_block", "for_statement"],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "variable_declaration",
            name: None,
            scopes: &[
                "function_declaration",
                "method_definition",
                "arrow_function",
                "function_expression",
                "generator_function_declaration",
                "generator_function",
            ],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "for_in_statement",
            name: None,
            scopes: &[
                "function_declaration",
                "method_definition",
                "arrow_function",
                "function_expression",
                "generator_function_declaration",
                "generator_function",
            ],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "class_declaration",
            name: None,
            scopes: &["statement_block"],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "type_alias_declaration",
            name: None,
            scopes: &["statement_block"],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "interface_declaration",
            name: None,
            scopes: &["statement_block"],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "enum_declaration",
            name: None,
            scopes: &["statement_block"],
            kind: Variable,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "function_declaration",
            name: Some("name"),
            scopes: &["statement_block"],
            kind: Function,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
        vvv_core::BindingRule {
            node: "generator_function_declaration",
            name: Some("name"),
            scopes: &["statement_block"],
            kind: Function,
            namespace: vvv_core::BindingNamespace::Value,
            after: false,
        },
    ],
    lexical_qualified: &[
        "member_expression",
        "nested_type_identifier",
        "pair",
        "pair_pattern",
    ],
    lexical_boundaries: &[],
    lexical_containers: &[],
    lexical_barriers: &[
        "arrow_function",
        "function_expression",
        "catch_clause",
        "internal_module",
        "mapped_type_clause",
        "conditional_type",
        "function_signature",
        "method_signature",
        "call_signature",
        "construct_signature",
        "function_type",
        "constructor_type",
        "generator_function_declaration",
        "generator_function",
        "abstract_class_declaration",
        "abstract_method_signature",
    ],
    named_imports: &[
        vvv_core::NamedImportRule {
            node: "import_clause",
            statement: "import_statement",
            name: None,
            imported: Some("default"),
            reexport: false,
        },
        vvv_core::NamedImportRule {
            node: "namespace_import",
            statement: "import_statement",
            name: None,
            imported: Some("*"),
            reexport: false,
        },
        vvv_core::NamedImportRule {
            node: "import_specifier",
            name: Some("name"),
            imported: None,
            statement: "import_statement",
            reexport: false,
        },
        vvv_core::NamedImportRule {
            node: "export_specifier",
            name: Some("name"),
            imported: None,
            statement: "export_statement",
            reexport: true,
        },
    ],
    named_modules: true,
    non_named_exports: &["export default"],
    symbols: SYMBOLS,
    identifiers: IDENTIFIERS,
    imports: IMPORTS,
    highlights: HIGHLIGHTS,
    navigation_types: &["type_identifier"],
    navigation_barriers: &["statement_block", "internal_module"],
    navigation_bindings: &[],
};