cuke-dedup 0.12.0

Static analysis for duplicate and reusable Cucumber step definitions
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
//! Exact Ruby handler trees with explicit lexical capture identity.
use super::{descendants, text};
use crate::model::{
    bounded_source_snippet, BehaviorEvent, ControlFlowOperation, HandlerFingerprint,
    HandlerSemantics,
};
use std::borrow::Cow;
use std::collections::BTreeSet;
use tree_sitter::Node;

/// Builds handler identity from bound syntax, captures, and assertion effects.
pub(super) fn fingerprint(
    block: Node<'_>,
    root: Node<'_>,
    source: &str,
    assertions: Option<&super::assertions::AssertionBindings>,
) -> HandlerFingerprint {
    let nodes = descendants(block);
    let bindings = assertions.map_or_else(
        || super::bindings::Bindings::collect(block, root, source),
        |assertions| assertions.bindings(block, root, source),
    );
    let comparable = !bindings.uncertain
        && !assertions.is_some_and(|assertions| assertions.known_ineligible(block, root, source))
        && !nodes.iter().any(|node| {
            (block.kind() == "method"
                && matches!(
                    node.kind(),
                    "self" | "instance_variable" | "class_variable" | "global_variable"
                )
                && !assertions.is_some_and(|assertions| assertions.known_field(*node)))
                || (node != &block
                    && matches!(
                        node.kind(),
                        "method"
                            | "singleton_method"
                            | "class"
                            | "module"
                            | "singleton_class"
                            | "heredoc_body"
                            | "heredoc_beginning"
                            | "optional_parameter"
                            | "keyword_parameter"
                            | "ERROR"
                    ))
        });
    let (tokens, alpha) = syntax_tokens(block, source, &bindings);
    let captures = &bindings.captures;
    let normalized = if captures.is_empty() {
        serde_json::to_string(&tokens)
    } else {
        serde_json::to_string(&(tokens, captures))
    }
    .expect("string tokens serialize");
    let handler_source = text(block, source);
    let source_snippet = bounded_source_snippet(handler_source, MAX_HANDLER_SNIPPET_CHARS, true);
    let legacy_snippet_bytes = handler_source
        .chars()
        .take(MAX_HANDLER_SNIPPET_CHARS)
        .map(char::len_utf8)
        .sum::<usize>();
    let exact_source = exact_source(block, source);
    let exact_key = if captures.is_empty() {
        serde_json::to_string(&exact_source)
    } else {
        serde_json::to_string(&(exact_source, captures))
    }
    .expect("handler source serializes");
    // Exact spelling selects equality while the opaque representation absorbs any bytes removed
    // by report-only snippet normalization. The combined comparison-work charge therefore remains
    // the normalized identity twice plus the legacy raw snippet prefix.
    let exact_hash = crate::model::stable_fingerprint(&exact_key);
    let exact_len = normalized
        .len()
        .checked_add(legacy_snippet_bytes)
        .and_then(|total| total.checked_sub(source_snippet.len()))
        .expect("bounded snippet fits the legacy comparison-work charge");
    let exact: String = exact_hash.chars().cycle().take(exact_len).collect();
    // An untrusted receiverless assertion factory must retain exact structural identity too.
    let exact_policy = untrusted_expect(&nodes, source, &bindings);
    // Literal abstraction needs the complete operation stream that configured assertion
    // evidence computes; ordinary handlers without it keep exact identity.
    let structural = (!exact_policy)
        .then(|| parameterized_calls(block, source, &bindings))
        .flatten()
        .or_else(|| {
            assertions
                .and_then(|assertions| assertions.ordinary_structural(block, source, &bindings))
        })
        .or_else(|| assertions.map(|_| structural_tokens(&alpha, captures)))
        .unwrap_or_else(|| exact.clone());
    let mut behavior_signature = if bindings.lexical {
        vec![BehaviorEvent::Method("ruby:lexical-file".to_owned())]
    } else {
        vec![]
    };
    let events = assertions.map(|assertions| assertions.events(block, source, &bindings));
    let assertion_evidence = events.as_ref().is_some_and(|events| {
        events
            .iter()
            .any(|event| matches!(event, BehaviorEvent::Assertion { .. }))
    });
    // An untrusted `expect` chain anywhere in the handler keeps the exact-handler policy under
    // either regime: duplicates by alpha identity stay, near and action similarity are withdrawn.
    if assertion_evidence {
        // Configured assertion evidence keeps the complete-syntax stream bound to its values.
        behavior_signature.extend(events.unwrap_or_default());
        if !exact_policy {
            behavior_signature.push(BehaviorEvent::Method("ruby:complete-events".to_owned()));
        }
    } else if let Some(actions) = (!exact_policy)
        .then(|| action_stream(block, source, &bindings))
        .flatten()
    {
        behavior_signature.extend(actions);
        behavior_signature.push(BehaviorEvent::Method("ruby:action-events".to_owned()));
    } else {
        // An unmodeled construct keeps the exact-handler policy: no marker, no near overlap.
        behavior_signature.extend(events.unwrap_or_default());
    }
    let trivial = (bindings.calls.is_empty()
        && !nodes
            .iter()
            .any(|node| matches!(node.kind(), "call" | "assignment" | "operator_assignment")))
        || stub_body(block, source);
    let alpha_normalized =
        serde_json::to_string(&(alpha, &captures)).expect("binding tokens serialize");
    HandlerSemantics {
        exact,
        normalized,
        alpha: alpha_normalized,
        structural,
        events: behavior_signature,
        source_snippet,
        comparable,
        trivial,
    }
    .finish(str::to_owned)
}

/// Raw handler spelling without a named method's declaration name, which is registration plumbing.
fn exact_source(block: Node<'_>, source: &str) -> String {
    if block.kind() != "method" {
        return text(block, source).to_owned();
    }
    let parameters = block
        .child_by_field_name("parameters")
        .map_or("", |parameters| text(parameters, source));
    let body = block
        .child_by_field_name("body")
        .map_or("", |body| text(body, source));
    format!("method\0{parameters}\0{body}")
}

const MAX_HANDLER_SNIPPET_CHARS: usize = 2_000;

/// Abstract only whole straight-line handlers; any unmodeled effect preserves exact identity.
pub(super) fn parameterized_calls(
    block: Node<'_>,
    source: &str,
    bindings: &super::bindings::Bindings,
) -> Option<String> {
    if block.child_by_field_name("parameters").is_some() {
        return None;
    }
    let body = block.child_by_field_name("body")?;
    let mut cursor = body.walk();
    let mut calls = Vec::new();
    let mut literals = 0;
    for call in body.named_children(&mut cursor).filter(|n| !n.is_extra()) {
        if call.kind() != "call" || call.child_by_field_name("block").is_some() {
            return None;
        }
        let (receiver_kind, receiver_name, operator) = match call.child_by_field_name("receiver") {
            Some(receiver) => {
                if !matches!(
                    receiver.kind(),
                    "identifier" | "self" | "instance_variable" | "global_variable" | "constant"
                ) {
                    return None;
                }
                (
                    receiver.kind(),
                    bindings
                        .names
                        .get(&receiver.id())
                        .map_or(text(receiver, source), String::as_str),
                    text(call.child_by_field_name("operator")?, source),
                )
            }
            None => ("implicit_self", "", ""),
        };
        let mut arguments = Vec::new();
        if let Some(args) = call.child_by_field_name("arguments") {
            let mut cursor = args.walk();
            for arg in args.named_children(&mut cursor).filter(|n| !n.is_extra()) {
                match arg.kind() {
                    "string"
                        if !descendants(arg).iter().any(|n| n.kind() == "interpolation")
                            && super::literal_string(text(arg, source)).is_some() => {}
                    "integer" | "float" => {}
                    _ => return None,
                }
                arguments.push(arg.kind());
                literals += 1;
            }
        }
        calls.push((
            receiver_kind,
            receiver_name,
            operator,
            text(call.child_by_field_name("method")?, source),
            arguments,
        ));
    }
    (literals > 0).then(|| {
        serde_json::to_string(&("ruby:literal-calls", calls, &bindings.captures))
            .expect("call structure serializes")
    })
}

/// Abstracts literal values out of alpha tokens; behaviour events keep the values.
fn structural_tokens(alpha: &SyntaxTokens<'_, '_>, captures: &BTreeSet<String>) -> String {
    let abstracted: Vec<(&str, &str)> = alpha
        .iter()
        .map(|(kind, value)| match *kind {
            "integer" | "float" | "string_content" | "escape_sequence" => (*kind, "<literal>"),
            _ => value
                .strip_prefix("const:")
                .and_then(|rest| rest.split_once(':'))
                .map_or((*kind, value.as_ref()), |(kind, _)| ("constant", kind)),
        })
        .collect();
    serde_json::to_string(&("ruby:structural", abstracted, captures))
        .expect("structural tokens serialize")
}

// Preserve topology as well as leaves; Ruby whitespace can alter call nesting.
type SyntaxTokens<'tree, 'source> = Vec<(&'tree str, Cow<'source, str>)>;
/// Exact and alpha-renamed token streams of a subtree, with declared callable bodies kept.
pub(super) fn syntax_tokens<'tree, 'source>(
    root: Node<'tree>,
    source: &'source str,
    bindings: &super::bindings::Bindings,
) -> (SyntaxTokens<'tree, 'source>, SyntaxTokens<'tree, 'source>) {
    tokens_with_elision(root, source, bindings, false)
}

/// Alpha tokens for behaviour events: declared callable bodies are syntax, not behaviour.
pub(super) fn event_tokens<'tree, 'source>(
    root: Node<'tree>,
    source: &'source str,
    bindings: &super::bindings::Bindings,
) -> SyntaxTokens<'tree, 'source> {
    tokens_with_elision(root, source, bindings, true).1
}

/// Token streams of a subtree; with `elide`, declared callable bodies collapse to one placeholder leaf.
fn tokens_with_elision<'tree, 'source>(
    root: Node<'tree>,
    source: &'source str,
    bindings: &super::bindings::Bindings,
    elide: bool,
) -> (SyntaxTokens<'tree, 'source>, SyntaxTokens<'tree, 'source>) {
    // Preserve tree structure: Ruby whitespace can change call nesting without changing leaves.
    let mut tokens = Vec::new();
    let mut alpha = Vec::new();
    let mut pending = vec![(root, false)];
    while let Some((node, closing)) = pending.pop() {
        if elide && node != root && bindings.elided.contains(&node.id()) {
            tokens.push((node.kind(), Cow::Borrowed("<declared>")));
            alpha.push((node.kind(), Cow::Borrowed("<declared>")));
            continue;
        }
        if root.kind() == "method"
            && node.parent() == Some(root)
            && (root.child_by_field_name("name") == Some(node)
                || matches!(node.kind(), "def" | "end"))
        {
            continue;
        }
        if node.kind() == "comment"
            || (node
                .parent()
                .is_some_and(|parent| matches!(parent.kind(), "block" | "do_block"))
                && matches!(node.kind(), "{" | "}" | "do" | "end"))
        {
            continue;
        }
        let kind = match node.kind() {
            "block" | "do_block" => "block",
            "block_body" | "body_statement" => "body",
            other => other,
        };
        if closing {
            tokens.push((")", Cow::Borrowed("")));
            alpha.push((")", Cow::Borrowed("")));
            continue;
        }
        if node.kind() == "line"
            || (node.kind() == "identifier" && text(node, source) == "__LINE__")
        {
            tokens.push((
                node.kind(),
                Cow::Owned((node.start_position().row + 1).to_string()),
            ));
            alpha.push(tokens.last().unwrap().clone());
        } else if node.child_count() == 0 {
            tokens.push((kind, Cow::Borrowed(text(node, source))));
            alpha.push((
                kind,
                bindings.names.get(&node.id()).map_or_else(
                    || Cow::Borrowed(text(node, source)),
                    |name| Cow::Owned(name.clone()),
                ),
            ));
        } else {
            tokens.push((kind, Cow::Borrowed("(")));
            alpha.push((kind, Cow::Borrowed("(")));
            pending.push((node, true));
            let mut cursor = node.walk();
            let children: Vec<_> = node.children(&mut cursor).collect();
            pending.extend(children.into_iter().rev().map(|node| (node, false)));
        }
    }
    (tokens, alpha)
}

/// Receiver-chain root of a call: intermediate calls collapse, a receiverless root call keeps
/// its name as the base (`expect(x).to` → `expect`).
fn chain_base<'a>(call: Node<'a>) -> Option<Node<'a>> {
    let mut root = super::bindings::unparenthesized(call.child_by_field_name("receiver")?);
    while root.kind() == "call" {
        match root.child_by_field_name("receiver") {
            Some(receiver) => root = super::bindings::unparenthesized(receiver),
            None => break,
        }
    }
    Some(root)
}

/// Whether `node`, through transparent parentheses, is the receiver of a call.
fn is_receiver(node: Node<'_>) -> bool {
    let mut value = node;
    while let Some(parent) = value
        .parent()
        .filter(|parent| parent.kind() == "parenthesized_statements")
    {
        value = parent;
    }
    value
        .parent()
        .is_some_and(|parent| parent.child_by_field_name("receiver") == Some(value))
}

/// Whether a callable literal is handed to a call, directly or inside a container argument.
fn in_argument_position(node: Node<'_>) -> bool {
    std::iter::successors(node.parent(), |node| node.parent())
        .take_while(|node| !matches!(node.kind(), "block" | "do_block" | "lambda" | "program"))
        .any(|node| node.kind() == "argument_list")
}

/// Whether the handler uses `expect` without configured trust: a receiverless `expect(...)` or
/// `expect { }` (a configured factory always carries a receiver), or a bare `expect` method read.
fn untrusted_expect(
    nodes: &[Node<'_>],
    source: &str,
    bindings: &super::bindings::Bindings,
) -> bool {
    nodes.iter().copied().any(|node| match node.kind() {
        "call" => {
            node.child_by_field_name("receiver").is_none()
                && node
                    .child_by_field_name("method")
                    .is_some_and(|method| text(method, source) == "expect")
        }
        "identifier" => text(node, source) == "expect" && bindings.calls.contains(&node.id()),
        _ => false, // fail-closed: every other node is neither a call nor a method read
    })
}

/// Action events for a handler whose every construct is modeled: `base#method` per call with
/// arguments dropped, bare helper calls, callbacks and control flow. `None` keeps the exact
/// handler policy for anything unmodeled or for an untrusted `expect` chain.
pub(super) fn action_stream(
    block: Node<'_>,
    source: &str,
    bindings: &super::bindings::Bindings,
) -> Option<Vec<BehaviorEvent>> {
    let declared = super::bindings::declared_callables(block, bindings);
    let mut events = Vec::new();
    let mut pending = vec![(block, false)];
    while let Some((node, expanded)) = pending.pop() {
        if node != block
            && (declared.contains_key(&node.id())
                || node.is_extra()
                || (block.kind() == "method" && block.child_by_field_name("name") == Some(node)))
        {
            continue;
        }
        if node != block
            && matches!(node.kind(), "block" | "do_block" | "lambda")
            && node
                .child_by_field_name("parameters")
                .is_some_and(|parameters| parameters.named_child_count() != 0)
        {
            return None;
        }
        // A callable literal is a callback only when handed to a call; anywhere else it is
        // syntax, like a function expression the JavaScript frontend never enters.
        if node != block {
            if let Some(callable) = super::bindings::callable_literal(node, source) {
                if in_argument_position(callable) {
                    // `lambda { |x| }` and `proc { |x| }` carry parameters on their block.
                    if super::bindings::callable_parameterized(callable) {
                        return None;
                    }
                    let statements = super::bindings::callable_body(callable)
                        .map(super::bindings::named_children)
                        .unwrap_or_default();
                    pending.extend(statements.into_iter().rev().map(|node| (node, expanded)));
                }
                continue;
            }
        }
        match node.kind() {
            "call" => {
                if bindings.unresolved_invocations.contains(&node.id()) {
                    return None;
                }
                if let Some(statements) =
                    super::bindings::expansion_statements(bindings, &declared, node, expanded)
                {
                    pending.extend(statements.into_iter().rev().map(|node| (node, true)));
                    continue;
                }
                // A receiverless root call is the base of the chain it starts: `helper.run` and
                // `helper().run` are one call sequence in Ruby.
                if node.child_by_field_name("receiver").is_some() || !is_receiver(node) {
                    let method = node
                        .child_by_field_name("method")
                        .map_or("()", |method| text(method, source));
                    let base = match chain_base(node) {
                        None => String::new(),
                        Some(root) => match root.kind() {
                            "call" => text(root.child_by_field_name("method")?, source).to_owned(),
                            "identifier" | "constant" => bindings
                                .names
                                .get(&root.id())
                                .cloned()
                                .unwrap_or_else(|| text(root, source).to_owned()),
                            "scope_resolution" | "self" | "instance_variable"
                            | "class_variable" | "global_variable" => text(root, source).to_owned(),
                            _ => serde_json::to_string(&event_tokens(root, source, bindings))
                                .expect("root tokens serialize"),
                        },
                    };
                    let operator = node
                        .child_by_field_name("operator")
                        .map_or("", |operator| text(operator, source));
                    events.push(BehaviorEvent::Call(format!(
                        "ruby:action:{base}{operator}#{method}"
                    )));
                }
            }
            "identifier" => {
                if bindings.calls.contains(&node.id()) && !is_receiver(node) {
                    events.push(BehaviorEvent::Call(format!(
                        "ruby:action:#{}",
                        text(node, source)
                    )));
                }
            }
            "if" | "if_modifier" | "elsif" | "conditional" => {
                events.push(BehaviorEvent::ControlFlow(ControlFlowOperation::If));
            }
            // Negated forms are opposite conditions, never the shared `if`/`while` operation.
            "unless" | "unless_modifier" | "until" | "until_modifier" => {
                events.push(BehaviorEvent::Legacy(format!(
                    "ruby:control:{}",
                    node.kind().trim_end_matches("_modifier")
                )));
            }
            "case" => events.push(BehaviorEvent::ControlFlow(ControlFlowOperation::Switch)),
            "while" | "while_modifier" => {
                events.push(BehaviorEvent::ControlFlow(ControlFlowOperation::While));
            }
            "return" => events.push(BehaviorEvent::ControlFlow(ControlFlowOperation::Return)),
            "block"
            | "do_block"
            | "lambda"
            | "block_body"
            | "body_statement"
            | "method"
            | "argument_list"
            | "parenthesized_statements"
            | "then"
            | "else"
            | "when"
            | "pattern"
            | "do"
            | "next"
            | "break"
            | "assignment"
            | "operator_assignment"
            | "array"
            | "hash"
            | "pair"
            | "hash_key_symbol"
            | "string"
            | "string_content"
            | "escape_sequence"
            | "simple_symbol"
            | "delimited_symbol"
            | "integer"
            | "float"
            | "true"
            | "false"
            | "nil"
            | "regex"
            | "constant"
            | "scope_resolution"
            | "self"
            | "instance_variable"
            | "class_variable"
            | "global_variable"
            | "block_parameters"
            | "lambda_parameters"
            | "method_parameters"
            | "identifier_suffix" => {}
            // fail-closed: element references, operators, super, yield, splats,
            // interpolation, rescue and every other construct keep the exact handler policy
            _ => return None,
        }
        pending.extend(
            super::bindings::named_children(node)
                .into_iter()
                .rev()
                .map(|child| (child, expanded)),
        );
    }
    Some(events)
}

/// Whether a handler only marks its step as pending, as the TypeScript frontend's trivial handler
/// rule does: an empty body, or one statement that is a stub expression, a `return`/`next` of one,
/// or a `raise`/`fail` whose arguments carry a stub label.
fn stub_body(handler: Node<'_>, source: &str) -> bool {
    let block = if handler.kind() == "lambda" {
        handler.child_by_field_name("body")
    } else {
        Some(handler)
    };
    let statements: Vec<_> = block
        .and_then(|block| block.child_by_field_name("body"))
        .map(|body| {
            if matches!(body.kind(), "block_body" | "body_statement") {
                let mut cursor = body.walk();
                body.named_children(&mut cursor)
                    .filter(|statement| statement.kind() != "comment")
                    .collect()
            } else {
                vec![body]
            }
        })
        .unwrap_or_default();
    match statements.as_slice() {
        [] => true,
        [statement] => {
            let statement = unparenthesized(*statement);
            match statement.kind() {
                "return" | "next" => statement.named_child(0).is_none_or(|arguments| {
                    arguments.named_child_count() == 1
                        && arguments
                            .named_child(0)
                            .is_some_and(|value| stub_expression(value, source))
                }),
                "call"
                    if statement.child_by_field_name("receiver").is_none()
                        && statement
                            .child_by_field_name("method")
                            .is_some_and(|method| {
                                matches!(text(method, source), "raise" | "fail")
                            }) =>
                {
                    statement
                        .child_by_field_name("arguments")
                        .is_some_and(|arguments| {
                            // Any label in the arguments marks the stub, as TypeScript's
                            // `contains_explicit_stub_marker` does for `throw`; a method name
                            // called on a receiver is a member, never a marker.
                            descendants(arguments).iter().any(|node| {
                                matches!(node.kind(), "string" | "identifier" | "constant")
                                    && !node.parent().is_some_and(|call| {
                                        call.child_by_field_name("receiver").is_some()
                                            && call.child_by_field_name("method") == Some(*node)
                                    })
                                    && stub_label(text(*node, source))
                            })
                        })
                }
                _ => stub_expression(statement, source),
            }
        }
        _ => false, // fail-closed: two or more statements do real work and stay compared
    }
}

/// A call or bare identifier whose name is a stub label. `nil`, strings and a bare identifier that
/// is a local leave the handler call-free, which is trivial already.
fn stub_expression(node: Node<'_>, source: &str) -> bool {
    let node = unparenthesized(node);
    match node.kind() {
        "identifier" => stub_label(text(node, source)),
        "call" => node
            .child_by_field_name("method")
            .is_some_and(|method| stub_label(text(method, source))),
        _ => false, // fail-closed: any other expression is ordinary behaviour and stays compared
    }
}

/// The single statement inside nested parentheses, or `node` itself.
fn unparenthesized(mut node: Node<'_>) -> Node<'_> {
    while node.kind() == "parenthesized_statements" && node.named_child_count() == 1 {
        node = node.named_child(0).unwrap();
    }
    node
}

/// Whether text names a pending marker once punctuation and case are dropped.
fn stub_label(text: &str) -> bool {
    let normalized: String = text
        .chars()
        .filter(|character| character.is_alphanumeric())
        .flat_map(char::to_lowercase)
        .collect();
    matches!(normalized.as_str(), "pending" | "notimplemented" | "todo")
}