leekscript-analysis 0.1.0

LeekScript semantic analysis: scope, validation, type checking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! Helpers to extract names and structure from syntax nodes (`VarDecl`, `FunctionDecl`, `ClassDecl`, etc.).

use sipha::red::{SyntaxElement, SyntaxNode, SyntaxToken};
use sipha::types::{IntoSyntaxKind, Span};

use super::scope::MemberVisibility;
use leekscript_core::syntax::{Kind, FIELD_RHS};
use leekscript_core::Type;

/// Declaration kind for a variable declaration node.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VarDeclKind {
    Var,
    Global,
    Const,
    Let,
    /// Typed form (e.g. `integer x = 0`) with no leading keyword.
    Typed,
}

/// Info extracted from a `NodeVarDecl` (var/global/const/let or typed).
pub struct VarDeclInfo {
    pub kind: VarDeclKind,
    pub name: String,
    pub name_span: Span,
}

/// Collect identifier tokens (name, span) from node's subtree in source order, stopping at first "=".
fn idents_before_assign(
    node: &SyntaxNode,
    idents: &mut Vec<(String, Span)>,
    first_token_text: &mut Option<String>,
) -> bool {
    use sipha::red::SyntaxElement;
    for elem in node.children() {
        match elem {
            SyntaxElement::Token(t) if !t.is_trivia() => {
                if first_token_text.is_none() {
                    *first_token_text = Some(t.text().to_string());
                }
                if t.text() == "=" {
                    return true;
                }
                if t.kind_as::<Kind>() == Some(Kind::TokIdent) {
                    idents.push((t.text().to_string(), t.text_range()));
                }
            }
            SyntaxElement::Node(n) => {
                if idents_before_assign(&n, idents, first_token_text) {
                    return true;
                }
            }
            _ => {}
        }
    }
    false
}

/// Returns the right-hand side expression of a `NodeBinaryExpr`.
/// Prefers the named field "rhs" when present; otherwise uses the last node child.
/// When the grammar labels RHS, it may wrap it in `NodeExpr`; we unwrap so the returned node is the inner expression.
#[must_use]
pub fn binary_expr_rhs(node: &SyntaxNode) -> Option<SyntaxNode> {
    if node.kind_as::<Kind>() != Some(Kind::NodeBinaryExpr) {
        return None;
    }
    let rhs = node
        .field_by_id(FIELD_RHS)
        .or_else(|| node.child_nodes().last())?;
    if rhs.kind_as::<Kind>() == Some(Kind::NodeExpr) {
        rhs.first_child_node().or(Some(rhs))
    } else {
        Some(rhs)
    }
}

/// Returns the member name (identifier after the dot) from a `NodeMemberExpr`.
#[must_use]
pub fn member_expr_member_name(node: &SyntaxNode) -> Option<String> {
    if node.kind_as::<Kind>() != Some(Kind::NodeMemberExpr) {
        return None;
    }
    let mut saw_dot = false;
    for child in node.children() {
        if let SyntaxElement::Token(t) = &child {
            if !t.is_trivia() {
                if t.text() == "." {
                    saw_dot = true;
                } else if saw_dot {
                    return Some(t.text().to_string());
                }
            }
        }
    }
    None
}

/// Returns the receiver name (identifier or keyword before the dot) from a `NodeMemberExpr`.
/// For `Cell.getCell` returns `Some("Cell")`, for `this.update` returns `Some("this")`.
#[must_use]
pub fn member_expr_receiver_name(node: &SyntaxNode) -> Option<String> {
    if node.kind_as::<Kind>() != Some(Kind::NodeMemberExpr) {
        return None;
    }
    let receiver = node.first_child_node()?;
    primary_expr_resolvable_name(&receiver)
}

/// Returns the declaration kind and name from a `NodeVarDecl`.
/// For "var x", "global T x": name is the first identifier after the keyword.
/// For typed form "Array<EffectOverTime> arr" or "integer? y": name is the *last* identifier before "=" (type names come first).
#[must_use]
pub fn var_decl_info(node: &SyntaxNode) -> Option<VarDeclInfo> {
    if node.kind_as::<Kind>() != Some(Kind::NodeVarDecl) {
        return None;
    }
    let mut idents = Vec::new();
    let mut first_token_text: Option<String> = None;
    idents_before_assign(node, &mut idents, &mut first_token_text);
    let first_token_text = first_token_text.as_deref();
    let last_idx = idents.len().saturating_sub(1);
    let (kind, name_idx) = match first_token_text {
        Some("var") => (VarDeclKind::Var, 0),
        Some("global") => (VarDeclKind::Global, last_idx),
        Some("const") => (VarDeclKind::Const, 0),
        Some("let") => (VarDeclKind::Let, 0),
        _ => (VarDeclKind::Typed, last_idx),
    };
    let (name, name_span) = idents.get(name_idx).cloned()?;
    Some(VarDeclInfo {
        kind,
        name,
        name_span,
    })
}

/// Info extracted from a `NodeFunctionDecl` (name and parameter counts).
pub struct FunctionDeclInfo {
    pub name: String,
    pub name_span: Span,
    /// Minimum number of arguments (params without a default value).
    pub min_arity: usize,
    /// Maximum number of arguments (total params; for overloads we store multiple ranges).
    pub max_arity: usize,
}

/// Number of argument expressions in a `NodeCallExpr`. Used for type inference so the call
/// is inferred as the function's return type. The grammar uses lparen, optional(expr, `zero_or_more(comma`, expr)), rparen;
/// the optional may be one node (with 0, 1, or more children) or optional + `zero_or_more` as siblings.
#[must_use]
pub fn call_argument_count(node: &SyntaxNode) -> usize {
    if node.kind_as::<Kind>() != Some(Kind::NodeCallExpr) {
        return 0;
    }
    let content_nodes: Vec<SyntaxNode> = node.child_nodes().collect();
    if content_nodes.is_empty() {
        return 0;
    }
    let first_children = content_nodes[0].child_nodes().count();
    if content_nodes.len() == 1 {
        // Single node: optional(expr, zero_or_more(...)) with 0, 1, or more expr children.
        return first_children;
    }
    // First node = optional (0 or 1 expr), rest = one zero_or_more node per (comma, expr).
    first_children.min(1) + content_nodes.len().saturating_sub(1)
}

/// Returns the syntax node for the `i`-th argument expression of a `NodeCallExpr` (0-based), for error spans.
#[must_use]
pub fn call_argument_node(node: &SyntaxNode, i: usize) -> Option<SyntaxNode> {
    if node.kind_as::<Kind>() != Some(Kind::NodeCallExpr) {
        return None;
    }
    let content_nodes: Vec<SyntaxNode> = node.child_nodes().collect();
    let args: Vec<SyntaxNode> = if content_nodes.len() == 1 {
        content_nodes[0].child_nodes().collect()
    } else {
        let mut v = Vec::new();
        if let Some(expr) = content_nodes
            .first()
            .and_then(sipha::red::SyntaxNode::first_child_node)
        {
            v.push(expr);
        }
        for n in &content_nodes[1..] {
            if let Some(expr) = n.first_child_node() {
                v.push(expr);
            }
        }
        v
    };
    args.into_iter().nth(i)
}

/// Returns true if this `NodeParam` has a default value (`= expr`).
pub fn param_has_default(node: &SyntaxNode) -> bool {
    if node.kind_as::<Kind>() != Some(Kind::NodeParam) {
        return false;
    }
    node.descendant_tokens().iter().any(|t| t.text() == "=")
}

/// Returns name and parameter counts from a `NodeFunctionDecl`.
/// For default parameters, `min_arity` is the number of required params; `max_arity` is total.
#[must_use]
pub fn function_decl_info(node: &SyntaxNode) -> Option<FunctionDeclInfo> {
    if node.kind_as::<Kind>() != Some(Kind::NodeFunctionDecl) {
        return None;
    }
    let tokens: Vec<SyntaxToken> = node.non_trivia_tokens().collect();
    let lparen_idx = tokens.iter().position(|t| t.text() == "(")?;
    let name_token = tokens.get(lparen_idx.checked_sub(1)?)?;
    let name = name_token.text().to_string();
    let name_span = name_token.text_range();
    let params: Vec<SyntaxNode> = node
        .child_nodes()
        .filter(|n| n.kind_as::<Kind>() == Some(Kind::NodeParam))
        .collect();
    let min_arity = params.iter().take_while(|p| !param_has_default(p)).count();
    let max_arity = params.len();
    Some(FunctionDeclInfo {
        name,
        name_span,
        min_arity,
        max_arity,
    })
}

/// Info extracted from a `NodeClassDecl` (name and optional super class).
pub struct ClassDeclInfo {
    pub name: String,
    pub name_span: Span,
    /// Name of the class this extends, if any.
    pub super_class: Option<String>,
}

/// Returns class name and optional super class from a `NodeClassDecl`.
#[must_use]
pub fn class_decl_info(node: &SyntaxNode) -> Option<ClassDeclInfo> {
    if node.kind_as::<Kind>() != Some(Kind::NodeClassDecl) {
        return None;
    }
    let tokens: Vec<SyntaxToken> = node.non_trivia_tokens().collect();
    let class_idx = tokens.iter().position(|t| t.text() == "class")?;
    let name_token = tokens.get(class_idx + 1)?;
    let name = name_token.text().to_string();
    let name_span = name_token.text_range();
    let super_class = tokens
        .iter()
        .skip(class_idx + 2)
        .position(|t| t.text() == "extends")
        .and_then(|extends_offset| {
            let idx = class_idx + 2 + extends_offset + 1;
            tokens
                .get(idx)
                .filter(|t| t.text() != "{")
                .map(|t| t.text().to_string())
        });
    Some(ClassDeclInfo {
        name,
        name_span,
        super_class,
    })
}

/// If the node is or contains a simple identifier (single identifier token),
/// returns its text and span. Handles `NodeExpr` and `NodePrimaryExpr` (expr may not wrap in `NodeExpr` in the grammar).
pub fn expr_identifier(node: &SyntaxNode) -> Option<(String, Span)> {
    let kind = node.kind_as::<Kind>()?;
    if kind != Kind::NodeExpr && kind != Kind::NodePrimaryExpr {
        return None;
    }
    let first = node.first_token()?;
    let last = node.last_token()?;
    if first.offset() != last.offset() {
        return None;
    }
    if first.kind_as::<Kind>() == Some(Kind::TokIdent) {
        Some((first.text().to_string(), first.text_range()))
    } else {
        None
    }
}

/// Name to use for type/scope resolution from a primary expr: identifier, or "this"/"super" when
/// the single token is the corresponding keyword (class methods don't use the `function` keyword).
#[must_use]
pub fn primary_expr_resolvable_name(node: &SyntaxNode) -> Option<String> {
    if let Some((name, _)) = expr_identifier(node) {
        return Some(name);
    }
    let kind = node.kind_as::<Kind>()?;
    if kind != Kind::NodePrimaryExpr {
        return None;
    }
    let first = node.first_token()?;
    let last = node.last_token()?;
    if first.offset() != last.offset() {
        return None;
    }
    match first.kind_as::<Kind>() {
        Some(Kind::KwThis) => Some("this".to_string()),
        Some(Kind::KwSuper) => Some("super".to_string()),
        _ => None,
    }
}

/// If this primary is a `new ClassName(...)` constructor call, returns `Some((class_name, num_args))`.
#[must_use]
pub fn primary_expr_new_constructor(node: &SyntaxNode) -> Option<(String, usize)> {
    if node.kind_as::<Kind>()? != Kind::NodePrimaryExpr {
        return None;
    }
    let elements: Vec<SyntaxElement> = node
        .children()
        .filter(|e| match e {
            SyntaxElement::Token(t) => !t.is_trivia(),
            SyntaxElement::Node(_) => true,
        })
        .collect();
    let first = elements.first()?;
    let first_tok = match first {
        SyntaxElement::Token(t) => t,
        _ => return None,
    };
    if first_tok.kind_as::<Kind>() != Some(Kind::KwNew) {
        return None;
    }
    let second = elements.get(1)?;
    let class_name = match second {
        SyntaxElement::Token(t) if t.kind_as::<Kind>() == Some(Kind::TokIdent) => {
            t.text().to_string()
        }
        SyntaxElement::Node(n) => n
            .first_token()
            .filter(|t| t.kind_as::<Kind>() == Some(Kind::TokIdent))?
            .text()
            .to_string(),
        _ => return None,
    };
    let arg_count = node.find_all_nodes(Kind::NodeExpr.into_syntax_kind()).len();
    Some((class_name, arg_count))
}

/// Check if this node is a simple identifier expression (for resolution).
#[allow(dead_code)]
pub fn is_identifier_expr(node: &SyntaxNode) -> bool {
    expr_identifier(node).is_some()
}

/// Returns the direct child node of `NodeForInStmt` that is the iterable expression (the expr after `in`).
#[must_use]
pub fn for_in_iterable_expr(for_in_node: &SyntaxNode) -> Option<SyntaxNode> {
    if for_in_node.kind_as::<Kind>() != Some(Kind::NodeForInStmt) {
        return None;
    }
    let mut seen_in = false;
    for child in for_in_node.children() {
        if let SyntaxElement::Token(t) = &child {
            if !t.is_trivia() && t.text() == "in" {
                seen_in = true;
                continue;
            }
        }
        if seen_in {
            if let SyntaxElement::Node(n) = child {
                return Some(n.clone());
            }
        }
    }
    None
}

/// Variable name(s) and span(s) from a `NodeForInStmt`: key and optionally value (for key : valueVar in expr).
/// Skips `type_expr` nodes and for/var/in/paren tokens so we get only the loop variable identifiers.
pub fn for_in_loop_vars(node: &SyntaxNode) -> Vec<(String, Span)> {
    if node.kind_as::<Kind>() != Some(Kind::NodeForInStmt) {
        return Vec::new();
    }
    let skip_tokens: &[&str] = &["for", "(", ")", "var", "in", ":"];
    let mut vars = Vec::new();
    let mut state = 0u8; // 0 = need key, 1 = need colon or in, 2 = need value
    for child in node.children() {
        match child {
            SyntaxElement::Token(t) if !t.is_trivia() => {
                let text = t.text();
                if text == "in" {
                    break;
                }
                if skip_tokens.contains(&text) {
                    if text == ":" && state == 1 {
                        state = 2;
                    }
                    continue;
                }
                if state == 0 {
                    vars.push((text.to_string(), t.text_range()));
                    state = 1;
                } else if state == 2 {
                    vars.push((text.to_string(), t.text_range()));
                    break;
                }
            }
            SyntaxElement::Node(n) => {
                if n.kind_as::<Kind>() == Some(Kind::NodeTypeExpr) {
                    continue;
                }
                if state == 0 {
                    if let Some(tok) = n.first_token() {
                        if !tok.is_trivia() && !skip_tokens.contains(&tok.text()) {
                            vars.push((tok.text().to_string(), tok.text_range()));
                            state = 1;
                        }
                    }
                } else if state == 2 {
                    if let Some(tok) = n.first_token() {
                        if !tok.is_trivia() && !skip_tokens.contains(&tok.text()) {
                            vars.push((tok.text().to_string(), tok.text_range()));
                            break;
                        }
                    }
                }
            }
            _ => {}
        }
    }
    vars
}

/// True if this `NodeExpr` is a ternary expression (cond ? then : else).
#[must_use]
pub fn is_ternary_expr(node: &SyntaxNode) -> bool {
    if node.kind_as::<Kind>() != Some(Kind::NodeExpr) {
        return false;
    }
    let mut has_question = false;
    let mut has_colon = false;
    for child in node.children() {
        if let SyntaxElement::Token(t) = child {
            if !t.is_trivia() {
                match t.text() {
                    "?" => has_question = true,
                    ":" => has_colon = true,
                    _ => {}
                }
            }
        }
    }
    has_question && has_colon
}

/// Null-check ops: then branch is non-null when condition is true.
const NULL_CHECK_OPS: &[&str] = &["!=", "!==", "==", "==="];

/// Returns (`var_name`, `then_is_non_null`) if `node` is a null-check condition (e.g. `var != null`, `var === null`).
/// Searches recursively for a binary with one identifier and one null. `root` is used to get parent/siblings.
#[must_use]
pub fn null_check_from_condition(
    condition_node: &SyntaxNode,
    root: &SyntaxNode,
) -> Option<(String, bool)> {
    find_null_check_binary(condition_node, root)
}

fn find_null_check_binary(node: &SyntaxNode, root: &SyntaxNode) -> Option<(String, bool)> {
    if node.kind_as::<Kind>() == Some(Kind::NodeBinaryExpr) {
        let op = node.children().find_map(|c| {
            if let SyntaxElement::Token(t) = c {
                if !t.is_trivia() {
                    let text = t.text();
                    if NULL_CHECK_OPS.contains(&text) {
                        return Some(text.to_string());
                    }
                }
            }
            None
        })?;
        let rhs = binary_expr_rhs(node)?;
        let lhs = prev_sibling_node(node, root)?;
        let (var_name, _var_on_left) = if let Some((name, _)) = expr_identifier(&lhs) {
            if is_null_literal(&rhs) {
                (name, true)
            } else {
                return find_null_check_binary_recurse(node, root);
            }
        } else if let Some((name, _)) = expr_identifier(&rhs) {
            if is_null_literal(&lhs) {
                (name, false)
            } else {
                return find_null_check_binary_recurse(node, root);
            }
        } else {
            return find_null_check_binary_recurse(node, root);
        };
        // then_is_non_null: for "var != null" or "null != var", then branch is non-null
        let then_is_non_null = op == "!=" || op == "!==";
        return Some((var_name, then_is_non_null));
    }
    find_null_check_binary_recurse(node, root)
}

fn find_null_check_binary_recurse(node: &SyntaxNode, root: &SyntaxNode) -> Option<(String, bool)> {
    for child in node.child_nodes() {
        if let Some(r) = find_null_check_binary(&child, root) {
            return Some(r);
        }
    }
    None
}

/// Previous sibling that is a node (skips tokens).
fn prev_sibling_node(node: &SyntaxNode, root: &SyntaxNode) -> Option<SyntaxNode> {
    let ancestors: Vec<SyntaxNode> = node.ancestors(root);
    let parent = ancestors.first()?;
    let mut idx = None;
    for (i, c) in parent.children().enumerate() {
        if let SyntaxElement::Node(n) = c {
            if n.text_range() == node.text_range() {
                idx = Some(i);
                break;
            }
        }
    }
    let i = idx?;
    parent
        .children()
        .take(i)
        .filter_map(|e| e.as_node().cloned())
        .last()
}

fn is_null_literal(node: &SyntaxNode) -> bool {
    node.first_token().is_some_and(|t| t.text() == "null")
}

/// Index of `node` among `parent`'s children (including tokens). Returns `None` if not a direct child.
#[must_use]
pub fn node_index_in_parent(node: &SyntaxNode, parent: &SyntaxNode) -> Option<usize> {
    for (i, c) in parent.children().enumerate() {
        if let SyntaxElement::Node(n) = c {
            if n.text_range() == node.text_range() {
                return Some(i);
            }
        }
    }
    None
}

/// Parameter name and span from a `NodeParam` (for scope building).
#[must_use]
pub fn param_name(node: &SyntaxNode) -> Option<(String, Span)> {
    if node.kind_as::<Kind>() != Some(Kind::NodeParam) {
        return None;
    }
    let tokens: Vec<SyntaxToken> = node.non_trivia_tokens().collect();
    let name_token = tokens
        .iter()
        .take_while(|t| t.text() != "=")
        .last()
        .or_else(|| tokens.first())?;
    Some((name_token.text().to_string(), name_token.text_range()))
}

/// Field name, optional declared type, and whether it's static from a `NodeClassField`.
/// Returns (name, type, `is_static`) where type is None if the field has no type annotation.
#[must_use]
pub fn class_field_info(node: &SyntaxNode) -> Option<(String, Option<Type>, bool)> {
    use super::type_expr::{parse_type_expr, TypeExprResult};
    use sipha::types::IntoSyntaxKind;
    if node.kind_as::<Kind>() != Some(Kind::NodeClassField) {
        return None;
    }
    let tokens: Vec<SyntaxToken> = node.non_trivia_tokens().collect();
    let is_static = tokens.first().is_some_and(|t| t.text() == "static");
    let name_token = tokens
        .iter()
        .take_while(|t| t.text() != "=" && t.text() != ";")
        .last()?;
    let name = name_token.text().to_string();
    // Type may be nested (e.g. inside grammar choice/sequence); search descendants.
    let type_expr_node = node.find_node(Kind::NodeTypeExpr.into_syntax_kind());
    let ty = type_expr_node.and_then(|te| match parse_type_expr(&te) {
        TypeExprResult::Ok(t) => Some(t),
        TypeExprResult::Err(_) => None,
    });
    Some((name, ty, is_static))
}

/// Visibility of a class member from the preceding modifier. **Public by default** when no modifier.
/// The modifier that applies is the last visibility keyword before this member that is not "consumed" by another member (i.e. no other member's range contains that keyword).
#[must_use]
pub fn class_member_visibility(node: &SyntaxNode, root: &SyntaxNode) -> MemberVisibility {
    let class_decl = node
        .ancestors(root)
        .into_iter()
        .find(|a| a.kind_as::<Kind>() == Some(Kind::NodeClassDecl));
    let class_decl = match class_decl {
        Some(c) => c,
        None => return MemberVisibility::Public,
    };
    let node_start = node.text_range().start;
    let kind_field = Kind::NodeClassField.into_syntax_kind();
    let kind_func = Kind::NodeFunctionDecl.into_syntax_kind();
    // All member nodes in this class (for "who consumes this keyword" check).
    let members: Vec<SyntaxNode> = class_decl
        .find_all_nodes(kind_field)
        .into_iter()
        .chain(class_decl.find_all_nodes(kind_func))
        .collect();
    // Collect (token_end, vis) for each visibility keyword before this node.
    let mut vis_keywords: Vec<(u32, MemberVisibility)> = Vec::new();
    for token in class_decl.descendant_tokens() {
        if token.is_trivia() {
            continue;
        }
        let range = token.text_range();
        // Include visibility keywords that are before or at the start of this member (so we see the modifier that applies to this member).
        if range.start <= node_start {
            match token.text() {
                "public" => vis_keywords.push((range.end, MemberVisibility::Public)),
                "private" => vis_keywords.push((range.end, MemberVisibility::Private)),
                "protected" => vis_keywords.push((range.end, MemberVisibility::Protected)),
                _ => {}
            }
        }
        if range.start > node_start {
            break; // past the start of this member
        }
    }
    for (end, vis) in vis_keywords.into_iter().rev() {
        let sibling_consumes_keyword = members.iter().any(|sib| {
            let r = sib.text_range();
            sib.text_range() != node.text_range() && r.start <= end && r.end > end
        });
        if !sibling_consumes_keyword {
            return vis;
        }
    }
    MemberVisibility::Public
}

/// True if this `NodeFunctionDecl` is a static class method (has "static" as preceding sibling in class body).
#[must_use]
pub fn class_method_is_static(node: &SyntaxNode, root: &SyntaxNode) -> bool {
    if node.kind_as::<Kind>() != Some(Kind::NodeFunctionDecl) {
        return false;
    }
    let ancestors: Vec<SyntaxNode> = node.ancestors(root);
    let parent = match ancestors.first() {
        Some(p) => p,
        None => return false,
    };
    let node_start = node.text_range().start;
    // Last non-trivia token in parent that ends before this node (source order) - if "static", method is static.
    let mut last_before: Option<String> = None;
    for token in parent.descendant_tokens() {
        if token.is_trivia() {
            continue;
        }
        let range = token.text_range();
        if range.end <= node_start {
            last_before = Some(token.text().to_string());
        } else {
            break;
        }
    }
    last_before.as_deref() == Some("static")
}