tsz_parser/parser/node_modifiers.rs
1//! Modifier query utilities for `NodeArena`.
2//!
3//! Provides shared helpers for checking whether a node's modifier list
4//! contains a specific `SyntaxKind`. This consolidates the `has_*_modifier`
5//! pattern that was previously duplicated across binder, checker, emitter,
6//! and lowering crates.
7
8use super::base::{NodeIndex, NodeList};
9use super::node::NodeArena;
10use tsz_scanner::SyntaxKind;
11
12impl NodeArena {
13 /// Check whether a modifier list contains a modifier of the given kind.
14 ///
15 /// This is the single source of truth for the "scan modifiers for a kind"
16 /// pattern used across all pipeline stages.
17 #[inline]
18 pub fn has_modifier(&self, modifiers: &Option<NodeList>, kind: SyntaxKind) -> bool {
19 if let Some(mods) = modifiers {
20 for &mod_idx in &mods.nodes {
21 if let Some(mod_node) = self.get(mod_idx)
22 && mod_node.kind == kind as u16
23 {
24 return true;
25 }
26 }
27 }
28 false
29 }
30
31 /// Like [`has_modifier`](Self::has_modifier) but accepts `Option<&NodeList>`.
32 #[inline]
33 pub fn has_modifier_ref(&self, modifiers: Option<&NodeList>, kind: SyntaxKind) -> bool {
34 if let Some(mods) = modifiers {
35 for &mod_idx in &mods.nodes {
36 if let Some(mod_node) = self.get(mod_idx)
37 && mod_node.kind == kind as u16
38 {
39 return true;
40 }
41 }
42 }
43 false
44 }
45
46 /// Find the first modifier of the given kind, returning its `NodeIndex`.
47 #[inline]
48 pub fn find_modifier(
49 &self,
50 modifiers: &Option<NodeList>,
51 kind: SyntaxKind,
52 ) -> Option<NodeIndex> {
53 if let Some(mods) = modifiers {
54 for &mod_idx in &mods.nodes {
55 if let Some(mod_node) = self.get(mod_idx)
56 && mod_node.kind == kind as u16
57 {
58 return Some(mod_idx);
59 }
60 }
61 }
62 None
63 }
64}