big_code_analysis/ops.rs
1// Per-language metric and AST modules deliberately consume the macro-
2// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
3// inside match expressions — explicit imports would list dozens of
4// variants per arm and obscure the per-language token sets that are the
5// point of these files. Allowed at the module level rather than per
6// function so the per-language impl blocks stay readable.
7#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
8
9use std::borrow::Cow;
10
11use crate::checker::Checker;
12use crate::error::MetricsError;
13use crate::getter::Getter;
14use crate::node::{Ancestors, Node};
15use crate::spaces::{SpaceKind, line_span, push_children};
16
17use crate::halstead::{Halstead, HalsteadMaps};
18
19use crate::MetricSuite;
20
21/// All operands and operators of a space.
22#[derive(Debug, Clone)]
23pub struct Ops {
24 /// The name of a function space.
25 ///
26 /// For the top-level (file-level) `Ops` the value is whatever
27 /// `Source::name` the caller supplied to the [`crate::Ast::ops`]
28 /// seam — `Some` or `None`.
29 ///
30 /// For nested spaces, `None` means an error occurred in parsing the
31 /// name of the function space from the AST.
32 pub name: Option<String>,
33 /// `true` when [`Ops::name`] was produced by lossy conversion (the
34 /// original path contained non-UTF-8 bytes and was rendered using
35 /// U+FFFD replacement characters). The explicit-name
36 /// [`crate::Ast::ops`] seam never sets it, since a caller-supplied
37 /// `String` name is UTF-8 by construction, so it is always `false`
38 /// in current code paths. Retained as a wire field for forward
39 /// compatibility; skipped from JSON output when `false` so existing
40 /// schemas keep their shape.
41 pub name_was_lossy: bool,
42 /// The first line of a function space.
43 pub start_line: usize,
44 /// The last line of a function space.
45 pub end_line: usize,
46 /// The space kind.
47 pub kind: SpaceKind,
48 /// All subspaces contained in a function space.
49 pub spaces: Vec<Ops>,
50 /// The **distinct** operands of a space — the deduplicated Halstead
51 /// operand vocabulary (`n2`), one entry per unique operand, not every
52 /// occurrence. Sorted in byte-lexicographic order, so the same input
53 /// always yields the same sequence (#1091).
54 pub operands: Vec<String>,
55 /// The **distinct** operators of a space — the deduplicated Halstead
56 /// operator vocabulary (`n1`), one entry per unique operator, not
57 /// every occurrence. Sorted in byte-lexicographic order, so the same
58 /// input always yields the same sequence (#1091).
59 pub operators: Vec<String>,
60}
61
62// Space nesting is caller-controlled, so the compiler-generated `Drop`
63// glue would recurse once per level and abort the process on a deep tree
64// (#1056). See [`crate::recursion`].
65crate::recursion::impl_iterative_drop!(Ops, spaces);
66
67impl Ops {
68 /// Project this tree into its [`crate::wire::Ops`] form — the
69 /// plain, `Deserialize`-capable record that defines the serialized
70 /// shape.
71 #[must_use]
72 pub fn to_wire(&self) -> crate::wire::Ops {
73 crate::wire::Ops::from(self)
74 }
75
76 fn new<'a, T: Getter>(
77 node: &Node<'a>,
78 code: &[u8],
79 ancestors: Ancestors<'a, '_>,
80 kind: SpaceKind,
81 ) -> Self {
82 let (start_position, end_position) = line_span(node, kind);
83 // The top-level Unit's name is overwritten by `ops_inner` with the
84 // caller-supplied name before returning, so computing it here is
85 // wasted work. Non-top-level Unit spaces have no resolvable name, so
86 // leaving `None` matches the documented "could not be resolved"
87 // semantics rather than inventing the `<anonymous>` placeholder the
88 // default getter returns. Other kinds keep the AST-derived name.
89 // Mirrors the `SpaceKind::Unit` handling in `FuncSpace::new`.
90 let name = (kind != SpaceKind::Unit)
91 .then(|| T::get_func_space_name(node, code, ancestors).map(str::to_owned))
92 .flatten();
93 Self {
94 name,
95 name_was_lossy: false,
96 spaces: Vec::new(),
97 kind,
98 start_line: start_position,
99 end_line: end_position,
100 operators: Vec::new(),
101 operands: Vec::new(),
102 }
103 }
104}
105
106#[derive(Debug, Clone)]
107struct State<'a> {
108 ops: Ops,
109 halstead_maps: HalsteadMaps<'a>,
110}
111
112/// Pushes a synthetic `Unit` root onto the state stack when the grammar
113/// hands us a non-`Unit` root.
114///
115/// Mirrors [`crate::spaces::push_synthetic_unit_root`] on the metrics
116/// seam: some grammars (e.g. tree-sitter-lua / tree-sitter-mozcpp on
117/// unparseable input) return an `ERROR` root that is not classified as a
118/// function space, so without this push the walk would never open a
119/// frame and `ops_inner` would return [`MetricsError::EmptyRoot`] for an
120/// input where `metrics()` succeeds (issue #789). A `Unit` root needs no
121/// wrapper, so nothing is pushed in that case.
122fn push_synthetic_unit_root<T: MetricSuite>(
123 state_stack: &mut Vec<State>,
124 node: &Node,
125 code: &[u8],
126) {
127 // `Ancestors::unknown()`: `node` is the tree root here, so it has
128 // no ancestors to hand over either way.
129 if T::Getter::get_space_kind_with_code(node, code, Ancestors::unknown()) != SpaceKind::Unit {
130 state_stack.push(State {
131 ops: Ops::new::<T::Getter>(node, code, Ancestors::unknown(), SpaceKind::Unit),
132 halstead_maps: HalsteadMaps::new(),
133 });
134 }
135}
136
137// Space-kind classifications `ops_inner` has performed on this thread.
138//
139// The classification only ever reaches `Ops::new`, so running it on a
140// node that opens no space is work thrown away — and nothing about the
141// walk's *output* can tell the two apart. The counter is the
142// observable: it reads one per space after the lookup was moved inside
143// the `func_space` branch, and one per *node* before, which is what
144// makes hoisting it back out a test failure rather than a silent
145// regression (#1110).
146crate::observation::counter!(space_kind_lookups);
147
148/// Classifies a node that is about to open a function space.
149///
150/// Classification happens after the decision that a space opens, the
151/// same way [`crate::spaces::compute`]'s `open_func_space` does it
152/// (#522), and through the same source-aware classifier, so a space
153/// both seams open carries the same [`SpaceKind`] in either walk.
154/// The `_with_code` variant is what lets Elixir's macro-shaped
155/// `defmodule` / `def` declarations — plain `Call` nodes distinguished
156/// only by their target identifier text — come back as `Class` /
157/// `Function` rather than `Unknown` (#275, #1130).
158///
159/// The lookup is a per-language `match` on the node's kind for most
160/// grammars, but C#'s reaches a child scan for a bodied indexer or
161/// property and Elixir's reads the `Call` target text and scans the
162/// ancestor chain for an enclosing `quote` block, so it is not free on
163/// every node either.
164fn classify_space_kind<'a, T: MetricSuite>(
165 node: &Node<'a>,
166 code: &[u8],
167 ancestors: Ancestors<'a, '_>,
168) -> SpaceKind {
169 space_kind_lookups::record();
170 T::Getter::get_space_kind_with_code(node, code, ancestors)
171}
172
173/// Render a space's vocabulary: byte-lexicographically ordered, one
174/// owned `String` per distinct entry.
175///
176/// # Order
177///
178/// `HashMap`'s hasher is randomly seeded per instance, so key order
179/// differs between two runs — and even between two parses in one
180/// process. Without a canonical order the same input renders and
181/// serializes differently every time, which makes `bca ops` output
182/// undiffable and unusable as a cache key (#1091).
183///
184/// Byte-lexicographic, not first-appearance, order: `finalize` merges
185/// a child space's maps into its parent, so an insertion-ordered map
186/// would give the parent "what it saw directly, then whatever each
187/// child contributed" — a walk artifact that shifts when nesting
188/// changes. Sorting is also stable across platforms and hasher
189/// versions, which insertion order via a different map type is not.
190///
191/// # Why the sort runs before the `String`s exist
192///
193/// Every ancestor of a space re-renders that space's whole vocabulary:
194/// `finalize` merges each child's Halstead maps into its parent, so a
195/// parent's key set is a superset of every descendant's and an entry
196/// nested `D` spaces deep is rendered `D + 1` times. Sorting the
197/// borrowed keys first makes each swap a fat pointer rather than a
198/// 24-byte `String`, and — because the `String`s are then allocated in
199/// output order — it hands every later pass over them (the wire
200/// projection, serialization, the `dump_ops` tree) a heap laid out in
201/// the order it reads. Issue #1110 measured both effects.
202///
203/// # Non-UTF-8 keys
204///
205/// Tree-sitter sources are expected to be valid UTF-8; non-UTF-8 bytes
206/// are replaced with the Unicode replacement character to keep the entry
207/// visible (rather than silently dropping it or using a sentinel string
208/// that could collide with a real identifier). That rendering is not
209/// order-preserving — `b"\xffA"` sorts after `"\u{fffd}B"` by raw bytes
210/// and before it once both are rendered — so a vocabulary that actually
211/// lost bytes is re-sorted on the rendered text, which is the order the
212/// pre-#1110 render-then-sort code produced. Valid UTF-8, which is every
213/// key in practice, takes the single sort.
214fn sorted_vocabulary(mut keys: Vec<&[u8]>) -> Vec<String> {
215 keys.sort_unstable();
216 let mut lossy = false;
217 let mut rendered: Vec<String> = keys
218 .into_iter()
219 .map(|key| match String::from_utf8_lossy(key) {
220 Cow::Borrowed(text) => text.to_owned(),
221 Cow::Owned(text) => {
222 lossy = true;
223 text
224 }
225 })
226 .collect();
227 if lossy {
228 rendered.sort_unstable();
229 }
230 rendered
231}
232
233fn compute_operators_and_operands<T: MetricSuite>(state: &mut State) {
234 let maps = &state.halstead_maps;
235
236 // Primitive-type operators live in a second map (keyed by text rather
237 // than by token id), so the operator vocabulary is the concatenation
238 // of both key sets.
239 let operators = maps
240 .operators
241 .keys()
242 .map(|k| T::Getter::get_operator_id_as_str(*k).as_bytes())
243 .chain(maps.primitive_operators.keys().copied())
244 .collect();
245
246 state.ops.operators = sorted_vocabulary(operators);
247 state.ops.operands = sorted_vocabulary(maps.operands.keys().copied().collect());
248}
249
250/// Close up to `diff_level` open spaces, folding each into its parent.
251///
252/// Only the states this pops get their vocabularies computed. The
253/// bottom state is never popped here, so the root's vocabulary is built
254/// once by [`ops_inner`] after the final drain — computing it on every
255/// call would rebuild (and, since #1091, re-sort) the whole file's
256/// vocabulary once per level-drop in the walk, and every result but the
257/// last would be overwritten.
258fn finalize<T: MetricSuite>(state_stack: &mut Vec<State>, diff_level: usize) {
259 for _ in 0..diff_level {
260 if state_stack.len() < 2 {
261 break;
262 }
263 let mut state = state_stack
264 .pop()
265 .expect("state_stack verified to have len >= 2");
266 let last_state = state_stack
267 .last_mut()
268 .expect("state_stack verified to have len >= 1 after pop");
269
270 // Populate the child's ops from its HalsteadMaps before
271 // recording it as a sub-space of the parent.
272 compute_operators_and_operands::<T>(&mut state);
273
274 // Merge child's Halstead maps into parent and record child space.
275 last_state.halstead_maps.merge(&state.halstead_maps);
276 last_state.ops.spaces.push(state.ops);
277 }
278}
279
280/// Context the ops walk carries down the tree alongside each node.
281///
282/// A named pair rather than a `(usize, usize)`: the two counts advance
283/// on different events and swapping them is silent — `level` only moves
284/// at space boundaries while `depth` counts every AST step. Mirrors
285/// [`crate::spaces::compute`]'s `Walk`, minus the comment flag this walk
286/// has no use for.
287#[derive(Clone, Copy)]
288struct Walk {
289 /// Nesting level, used to close op-spaces on the way back up.
290 level: usize,
291 /// AST depth — the number of ancestors this node has, so the root
292 /// sits at `0`. Indexes the ancestor chain the walk maintains.
293 depth: usize,
294}
295
296/// Explicit-name core of the operator/operand walk backing the
297/// [`crate::Ast::ops`] `Source`-based seam. The top-level [`Ops::name`]
298/// is whatever the caller passes in `name`; `name_was_lossy` is left at
299/// its `false` default because an explicit `String` name is never lossy.
300/// Mirrors [`crate::spaces::metrics_inner`].
301pub(crate) fn ops_inner<T: MetricSuite>(
302 parser: &T,
303 name: Option<String>,
304) -> Result<Ops, MetricsError> {
305 let code = parser.code();
306 let node = parser.root();
307 let mut cursor = node.cursor();
308 let mut stack = Vec::new();
309 // Ancestor chain of the node currently being visited, root first,
310 // maintained by the same truncate/push rule as
311 // `spaces::compute::metrics_inner` (#1084).
312 let mut chain: Vec<Node<'_>> = Vec::new();
313 let mut state_stack: Vec<State> = Vec::new();
314 let mut last_level = 0;
315
316 // Mirror `metrics_inner`: wrap a non-`Unit` (e.g. `ERROR`) root in a
317 // synthetic `Unit` frame so the walk always has a frame to populate.
318 // Without this, an `ERROR`-root parse drains the state stack and
319 // `ops_inner` returns `EmptyRoot` for inputs where `metrics()`
320 // succeeds (issue #789).
321 push_synthetic_unit_root::<T>(&mut state_stack, &node, code);
322
323 stack.push((node, Walk { level: 0, depth: 0 }));
324
325 while let Some((node, Walk { level, depth })) = stack.pop() {
326 chain.truncate(depth);
327
328 if level < last_level {
329 finalize::<T>(&mut state_stack, last_level - level);
330 last_level = level;
331 }
332
333 let ancestors = Ancestors::checked(&chain, &node);
334
335 // Same predicate `spaces::compute::metrics_inner` opens on, so
336 // the two walks agree on which nodes become spaces. The
337 // byte-less `is_func || is_func_space` this replaced could not
338 // see Elixir's macro-shaped declarations, which are `Call`
339 // nodes identified by their target text, so `bca ops` opened no
340 // space for a `defmodule` / `def` / `defp` / `defmacro` — only
341 // for `Source` and an explicit `fn … -> … end` (#1130).
342 let func_space = T::Checker::promotes_to_func_space_with_code(&node, code, ancestors);
343
344 let new_level = if func_space {
345 let kind = classify_space_kind::<T>(&node, code, ancestors);
346 let state = State {
347 ops: Ops::new::<T::Getter>(&node, code, ancestors, kind),
348 halstead_maps: HalsteadMaps::new(),
349 };
350 state_stack.push(state);
351 last_level = level + 1;
352 last_level
353 } else {
354 level
355 };
356
357 if let Some(state) = state_stack.last_mut() {
358 T::Halstead::compute(&node, code, ancestors, &mut state.halstead_maps);
359 }
360
361 chain.push(node);
362
363 // Shared with `metrics_inner` (issue #969): `push_children` is
364 // State-independent — it only moves the cursor over child nodes —
365 // so unlike the local `finalize` / `push_synthetic_unit_root`
366 // mirrors (which differ by `State` payload) it is reused directly
367 // rather than duplicated. The source-order-then-reverse ordering
368 // it encapsulates is load-bearing for suppression attribution.
369 // The returned child slice is only useful to `metrics_inner`,
370 // which seeds their cognitive nesting; `ops` just walks them.
371 push_children(
372 &mut cursor,
373 &node,
374 Walk {
375 level: new_level,
376 depth: depth + 1,
377 },
378 &mut stack,
379 );
380 }
381
382 finalize::<T>(&mut state_stack, usize::MAX);
383
384 // Reserved error path: `MetricsError::EmptyRoot` is unreachable
385 // today because the synthetic Unit push above (and every supported
386 // language's root being recognised as a `func_space`) keeps the
387 // state stack non-empty for every input, including ERROR-root,
388 // empty, whitespace-only, and comment-only sources — matching
389 // `metrics_inner`. The `ok_or` is retained so a future walker change
390 // that legitimately drains the stack surfaces a distinct error
391 // variant rather than a bare `None`. See `MetricsError::EmptyRoot`
392 // for the matching variant doc.
393 let mut state = state_stack.pop().ok_or(MetricsError::EmptyRoot)?;
394 // The root is the one state `finalize` never pops, so its vocabulary
395 // is built here — once, from the fully-merged maps.
396 compute_operators_and_operands::<T>(&mut state);
397 state.ops.name = name;
398 Ok(state.ops)
399}
400
401#[cfg(test)]
402#[allow(
403 clippy::float_cmp,
404 clippy::cast_precision_loss,
405 clippy::cast_possible_truncation,
406 clippy::cast_sign_loss,
407 clippy::similar_names,
408 clippy::doc_markdown,
409 clippy::needless_raw_string_hashes,
410 clippy::too_many_lines
411)]
412mod tests {
413 use super::Ops;
414 use crate::{Ast, LANG, Source};
415
416 #[cfg(any(
417 feature = "cpp",
418 feature = "java",
419 feature = "javascript",
420 feature = "mozjs",
421 feature = "perl",
422 feature = "python",
423 feature = "rust",
424 feature = "typescript",
425 ))]
426 #[inline]
427 fn check_ops(
428 lang: LANG,
429 source: &str,
430 file: &str,
431 correct_operators: &mut [&str],
432 correct_operands: &mut [&str],
433 ) {
434 let mut trimmed_bytes = source.trim_end().trim_matches('\n').as_bytes().to_vec();
435 trimmed_bytes.push(b'\n');
436 let ops = Ast::parse(Source::new(lang, &trimmed_bytes).with_name(Some(file.to_owned())))
437 .expect("language feature enabled")
438 .ops()
439 .expect("ops walk must yield a top-level Ops");
440
441 let operators_str: Vec<&str> = ops.operators.iter().map(AsRef::as_ref).collect();
442 let operands_str: Vec<&str> = ops.operands.iter().map(AsRef::as_ref).collect();
443
444 // Only the *expectations* are sorted here: `Ops` is documented to
445 // come back byte-lexicographically ordered (#1091), so comparing
446 // against a sorted expectation without re-sorting the actual value
447 // makes every `check_ops` caller an ordering regression test for
448 // its language.
449 correct_operators.sort_unstable();
450 assert_eq!(&operators_str[..], correct_operators);
451
452 correct_operands.sort_unstable();
453 assert_eq!(&operands_str[..], correct_operands);
454 }
455
456 #[cfg(feature = "python")]
457 #[test]
458 fn python_ops() {
459 check_ops(
460 LANG::Python,
461 "if True:
462 a = 1 + 2",
463 "foo.py",
464 &mut ["if", "=", "+"],
465 &mut ["True", "a", "1", "2"],
466 );
467 }
468
469 #[cfg(feature = "perl")]
470 #[test]
471 fn perl_pattern_operations_render_as_source_spellings() {
472 // #1314 classifies `s///` and `tr///` as Halstead operators.
473 // They are *named* nodes rather than punctuation tokens, so the
474 // `get_operator!` macro's fallback would render each kind's own
475 // name — `substitution_pattern_s`, `transliteration_tr_or_y` —
476 // into `bca ops`, which reads as a bug rather than as Perl.
477 // `PerlCode::get_operator_id_as_str` is hand-written to map
478 // them, and this is what pins that mapping: the counts alone
479 // cannot see it.
480 //
481 // `y///` is a synonym of `tr///` and shares one kind, so the
482 // two source spellings collapse to a single `tr///` entry —
483 // asserted here by the *absence* of a `y///` row in an
484 // exhaustive expectation, not by a negative assertion.
485 //
486 // The trailing `/pat/` keeps a pattern *value* in the fixture,
487 // so the test also shows the split: the operation spellings are
488 // operators while the value is an operand.
489 check_ops(
490 LANG::Perl,
491 "$s =~ s/a/b/;\n$s =~ tr/c/d/;\n$s =~ y/e/f/;\n$t = /pat/;",
492 "foo.pl",
493 &mut ["$", ";", "=", "=~", "s///", "tr///"],
494 &mut ["$s", "$t", "/pat/"],
495 );
496 }
497
498 #[cfg(feature = "python")]
499 #[test]
500 fn python_function_ops() {
501 check_ops(
502 LANG::Python,
503 "def foo():
504 def bar():
505 def toto():
506 a = 1 + 1
507 b = 2 + a
508 c = 3 + 3",
509 "foo.py",
510 &mut ["def", "=", "+"],
511 &mut ["foo", "bar", "toto", "a", "b", "c", "1", "2", "3"],
512 );
513 }
514
515 #[cfg(feature = "cpp")]
516 #[test]
517 fn cpp_ops() {
518 check_ops(
519 LANG::Cpp,
520 "int a, b, c;
521 float avg;
522 avg = (a + b + c) / 3;",
523 "foo.c",
524 &mut ["int", "float", "()", "=", "+", "/", ",", ";"],
525 &mut ["a", "b", "c", "avg", "3"],
526 );
527 }
528
529 #[cfg(feature = "cpp")]
530 #[test]
531 fn cpp_function_ops() {
532 check_ops(
533 LANG::Cpp,
534 "main()
535 {
536 int a, b, c, avg;
537 scanf(\"%d %d %d\", &a, &b, &c);
538 avg = (a + b + c) / 3;
539 printf(\"avg = %d\", avg);
540 }",
541 "foo.c",
542 &mut ["()", "{}", "int", "&", "=", "+", "/", ",", ";"],
543 &mut [
544 "main",
545 "a",
546 "b",
547 "c",
548 "avg",
549 "scanf",
550 "\"%d %d %d\"",
551 "3",
552 "printf",
553 "\"avg = %d\"",
554 ],
555 );
556 }
557
558 #[cfg(feature = "rust")]
559 #[test]
560 fn rust_ops() {
561 check_ops(
562 LANG::Rust,
563 "let: usize a = 5; let b: f32 = 7.0; let c: i32 = 3;",
564 "foo.rs",
565 &mut ["let", "usize", "=", ";", "f32", "i32"],
566 &mut ["a", "b", "c", "5", "7.0", "3"],
567 );
568 }
569
570 #[cfg(feature = "rust")]
571 #[test]
572 fn rust_function_ops() {
573 check_ops(
574 LANG::Rust,
575 "fn main() {
576 let a = 5; let b = 5; let c = 5;
577 let avg = (a + b + c) / 3;
578 println!(\"{}\", avg);
579 }",
580 "foo.rs",
581 &mut ["fn", "()", "{}", "let", "=", "+", "/", ";", "!", ","],
582 &mut ["main", "a", "b", "c", "avg", "5", "3", "println", "\"{}\""],
583 );
584 }
585
586 #[cfg(feature = "javascript")]
587 #[test]
588 fn javascript_ops() {
589 check_ops(
590 LANG::Javascript,
591 "var a, b, c, avg;
592 let x = 1;
593 a = 5; b = 5; c = 5;
594 avg = (a + b + c) / 3;
595 console.log(\"{}\", avg);",
596 "foo.js",
597 &mut ["()", "var", "let", "=", "+", "/", ",", ".", ";"],
598 &mut [
599 "a", "b", "c", "avg", "x", "1", "3", "5", "console", "log", "\"{}\"",
600 ],
601 );
602 }
603
604 #[cfg(feature = "javascript")]
605 #[test]
606 fn javascript_function_ops() {
607 check_ops(
608 LANG::Javascript,
609 "function main() {
610 var a, b, c, avg;
611 let x = 1;
612 a = 5; b = 5; c = 5;
613 avg = (a + b + c) / 3;
614 console.log(\"{}\", avg);
615 }",
616 "foo.js",
617 &mut [
618 "function", "()", "{}", "var", "let", "=", "+", "/", ",", ".", ";",
619 ],
620 &mut [
621 "main", "a", "b", "c", "avg", "x", "1", "3", "5", "console", "log", "\"{}\"",
622 ],
623 );
624 }
625
626 #[cfg(feature = "mozjs")]
627 #[test]
628 fn mozjs_ops() {
629 check_ops(
630 LANG::Mozjs,
631 "var a, b, c, avg;
632 let x = 1;
633 a = 5; b = 5; c = 5;
634 avg = (a + b + c) / 3;
635 console.log(\"{}\", avg);",
636 "foo.js",
637 &mut ["()", "var", "let", "=", "+", "/", ",", ".", ";"],
638 &mut [
639 "a", "b", "c", "avg", "x", "1", "3", "5", "console", "log", "\"{}\"",
640 ],
641 );
642 }
643
644 #[cfg(feature = "mozjs")]
645 #[test]
646 fn mozjs_function_ops() {
647 check_ops(
648 LANG::Mozjs,
649 "function main() {
650 var a, b, c, avg;
651 let x = 1;
652 a = 5; b = 5; c = 5;
653 avg = (a + b + c) / 3;
654 console.log(\"{}\", avg);
655 }",
656 "foo.js",
657 &mut [
658 "function", "()", "{}", "var", "let", "=", "+", "/", ",", ".", ";",
659 ],
660 &mut [
661 "main", "a", "b", "c", "avg", "x", "1", "3", "5", "console", "log", "\"{}\"",
662 ],
663 );
664 }
665
666 #[cfg(feature = "typescript")]
667 #[test]
668 fn typescript_ops() {
669 // Issue #1261: the `: string` annotation counts exactly once,
670 // as the text-keyed `string` operator (PredefinedType wrapper)
671 // — symmetric with `: number` / `: boolean`. Under #313 its
672 // `String2` child also emitted a `"string"` operand, so one
673 // source token tallied twice.
674 check_ops(
675 LANG::Typescript,
676 "var a, b, c, avg;
677 let age: number = 32;
678 let name: string = \"John\"; let isUpdated: boolean = true;
679 a = 5; b = 5; c = 5;
680 avg = (a + b + c) / 3;
681 console.log(\"{}\", avg);",
682 "foo.ts",
683 &mut [
684 "()", "var", "let", "string", "number", "boolean", ":", "=", "+", "/", ",", ".",
685 ";",
686 ],
687 &mut [
688 "a",
689 "b",
690 "c",
691 "avg",
692 "age",
693 "name",
694 "isUpdated",
695 "32",
696 "\"John\"",
697 "true",
698 "3",
699 "5",
700 "console",
701 "log",
702 "\"{}\"",
703 ],
704 );
705 }
706
707 #[cfg(feature = "typescript")]
708 #[test]
709 fn typescript_function_ops() {
710 // Issue #1261: see `typescript_ops` — the `string` type keyword
711 // contributes only the primitive-typed operator, never a
712 // `"string"` operand.
713 check_ops(
714 LANG::Typescript,
715 "function main() {
716 var a, b, c, avg;
717 let age: number = 32;
718 let name: string = \"John\"; let isUpdated: boolean = true;
719 a = 5; b = 5; c = 5;
720 avg = (a + b + c) / 3;
721 console.log(\"{}\", avg);
722 }",
723 "foo.ts",
724 &mut [
725 "function", "()", "{}", "var", "let", "string", "number", "boolean", ":", "=", "+",
726 "/", ",", ".", ";",
727 ],
728 &mut [
729 "main",
730 "a",
731 "b",
732 "c",
733 "avg",
734 "age",
735 "name",
736 "isUpdated",
737 "32",
738 "\"John\"",
739 "true",
740 "3",
741 "5",
742 "console",
743 "log",
744 "\"{}\"",
745 ],
746 );
747 }
748
749 #[cfg(feature = "typescript")]
750 #[test]
751 fn tsx_ops() {
752 // Issue #1261: TSX exposes the `: string` type-keyword child as
753 // `String3` (vs. TS's `String2`); like TS, the keyword counts
754 // only as the `string` operator, never as an operand.
755 check_ops(
756 LANG::Tsx,
757 "var a, b, c, avg;
758 let age: number = 32;
759 let name: string = \"John\"; let isUpdated: boolean = true;
760 a = 5; b = 5; c = 5;
761 avg = (a + b + c) / 3;
762 console.log(\"{}\", avg);",
763 "foo.ts",
764 &mut [
765 "()", "var", "let", "string", "number", "boolean", ":", "=", "+", "/", ",", ".",
766 ";",
767 ],
768 &mut [
769 "a",
770 "b",
771 "c",
772 "avg",
773 "age",
774 "name",
775 "isUpdated",
776 "32",
777 "\"John\"",
778 "true",
779 "3",
780 "5",
781 "console",
782 "log",
783 "\"{}\"",
784 ],
785 );
786 }
787
788 #[cfg(feature = "typescript")]
789 #[test]
790 fn tsx_function_ops() {
791 // Issue #1261: see `tsx_ops` — TSX::String3 (type-keyword
792 // `string`) is an operator only, never an operand.
793 check_ops(
794 LANG::Tsx,
795 "function main() {
796 var a, b, c, avg;
797 let age: number = 32;
798 let name: string = \"John\"; let isUpdated: boolean = true;
799 a = 5; b = 5; c = 5;
800 avg = (a + b + c) / 3;
801 console.log(\"{}\", avg);
802 }",
803 "foo.ts",
804 &mut [
805 "function", "()", "{}", "var", "let", "string", "number", "boolean", ":", "=", "+",
806 "/", ",", ".", ";",
807 ],
808 &mut [
809 "main",
810 "a",
811 "b",
812 "c",
813 "avg",
814 "age",
815 "name",
816 "isUpdated",
817 "32",
818 "\"John\"",
819 "true",
820 "3",
821 "5",
822 "console",
823 "log",
824 "\"{}\"",
825 ],
826 );
827 }
828
829 // Issue #453: a `void` return type (a `predefined_type` wrapper over a
830 // `void` token) and an expression `void` (`void 0`) must collapse to a
831 // single distinct `"void"` operator. `check_ops` asserts the exact
832 // operator list, so a duplicate `"void"` — the pre-fix symptom, where
833 // the wrapper keyed `primitive_operators["void"]` and the inner token
834 // keyed `operators[Void]` — trips the assertion. This pins the lesson-4
835 // `n1 == dedupe(ops.operators)` invariant for the two `void` forms in
836 // one file.
837 #[cfg(feature = "typescript")]
838 #[test]
839 fn typescript_void_return_and_expression_single_operator_453() {
840 check_ops(
841 LANG::Typescript,
842 "function f(): void { return void 0; }",
843 "foo.ts",
844 &mut ["function", "()", "{}", ":", "void", "return", ";"],
845 &mut ["f", "0"],
846 );
847 }
848
849 #[cfg(feature = "typescript")]
850 #[test]
851 fn tsx_void_return_and_expression_single_operator_453() {
852 check_ops(
853 LANG::Tsx,
854 "function f(): void { return void 0; }",
855 "foo.tsx",
856 &mut ["function", "()", "{}", ":", "void", "return", ";"],
857 &mut ["f", "0"],
858 );
859 }
860
861 #[cfg(feature = "java")]
862 #[test]
863 fn java_ops() {
864 check_ops(
865 LANG::Java,
866 "public class Main {
867 public static void main(string args[]) {
868 int a, b, c, avg;
869 a = 5; b = 5; c = 5;
870 avg = (a + b + c) / 3;
871 MessageFormat.format(\"{0}\", avg);
872 }
873 }",
874 "foo.java",
875 &mut [
876 "{}", "void", "()", "[]", ",", ".", ";", "int", "=", "+", "/",
877 ],
878 &mut [
879 "Main",
880 "main",
881 "args",
882 "a",
883 "b",
884 "c",
885 "avg",
886 "5",
887 "3",
888 "MessageFormat",
889 "format",
890 "\"{0}\"",
891 ],
892 );
893 }
894
895 #[cfg(feature = "java")]
896 #[test]
897 fn java_primitive_ops() {
898 check_ops(
899 LANG::Java,
900 "public class Prims {
901 byte a = 1;
902 short b = 2;
903 int c = 3;
904 long d = 4;
905 char e = 'x';
906 float f = 1.0f;
907 double g = 2.0;
908 boolean h = true;
909 boolean i = false;
910 }",
911 "foo.java",
912 // All 8 primitive-type keywords must appear as distinct operators.
913 // true/false appear as operands.
914 &mut [
915 "{}",
916 ";",
917 "=",
918 "byte",
919 "short",
920 "int",
921 "long",
922 "char",
923 "float",
924 "double",
925 "boolean_type",
926 ],
927 &mut [
928 "Prims", "a", "b", "c", "d", "e", "f", "g", "h", "i", "1", "2", "3", "4", "'x'",
929 "1.0f", "2.0", "true", "false",
930 ],
931 );
932 }
933
934 /// A `Unit` space must never carry the synthetic `<anonymous>`
935 /// placeholder that the default getter invents for nodes without a
936 /// `name` field. The public docs describe `None` as the
937 /// "name could not be resolved" state, and the metrics-side
938 /// `FuncSpace::new` already special-cases `SpaceKind::Unit` the same
939 /// way; this pins the `Ops::new` mirror so a regression to the old
940 /// `Some("<anonymous>")` initialisation fails here rather than only
941 /// surfacing for a (currently unreachable) non-top-level `Unit`
942 /// space, where `ops_inner`'s top-level override would not rescue it.
943 /// See issue #755.
944 #[cfg(feature = "rust")]
945 #[test]
946 fn unit_space_name_is_none_not_anonymous() {
947 use crate::getter::Getter;
948 use crate::node::Ancestors;
949 use crate::traits::ParserTrait;
950 use crate::{RustCode, RustParser, SpaceKind};
951
952 let code = b"fn f() {}\n";
953 let parser = RustParser::new(code.to_vec(), std::path::Path::new("foo.rs"), None);
954 let root = parser.root();
955 // The Rust `source_file` root is a `Unit` and has no `name`/`type`
956 // field, so the default getter would invent `<anonymous>`.
957 assert_eq!(SpaceKind::Unit, RustCode::get_space_kind(&root));
958
959 let ops = super::Ops::new::<RustCode>(&root, code, Ancestors::unknown(), SpaceKind::Unit);
960 assert_eq!(
961 ops.name, None,
962 "Unit space must preserve name = None, not invent <anonymous>"
963 );
964 }
965
966 /// Issue #789: an `ERROR`-root parse (here Lua partial input) where
967 /// `metrics()` succeeds must make `ops()` succeed too — the two seams
968 /// should agree. Before the synthetic-Unit-root mirror in `ops_inner`,
969 /// `ops()` returned `Err(MetricsError::EmptyRoot)` because the ERROR
970 /// root is not classified as a function space, so no frame was ever
971 /// pushed. This pins their agreement: both succeed, and the resulting
972 /// top-level `Ops` is a `Unit` whose name is the caller-supplied
973 /// `Source::name` (the intrinsic Unit name stays `None` per #755 until
974 /// `ops_inner` overrides the top-level name).
975 #[cfg(feature = "lua")]
976 #[test]
977 fn lua_error_root_ops_agrees_with_metrics_789() {
978 use crate::{MetricsOptions, SpaceKind};
979
980 // tree-sitter-lua surfaces an ERROR root for this partial input.
981 let src = b"function foo(x)\n return x +\n".to_vec();
982 let name = "partial.lua".to_owned();
983
984 let ast = Ast::parse(Source::new(LANG::Lua, &src).with_name(Some(name.clone())))
985 .expect("lua feature enabled");
986
987 // metrics() must succeed (it already wrapped a synthetic Unit root).
988 let space = ast
989 .metrics(MetricsOptions::default())
990 .expect("metrics must yield a top-level space");
991 assert_eq!(space.kind, SpaceKind::Unit);
992
993 // ops() must now succeed in the same case rather than returning
994 // Err(EmptyRoot).
995 let ops = ast
996 .ops()
997 .expect("ops must agree with metrics and yield a top-level Ops");
998 assert_eq!(ops.kind, SpaceKind::Unit);
999 assert_eq!(
1000 ops.name.as_deref(),
1001 Some(name.as_str()),
1002 "top-level Ops name is the caller-supplied Source::name"
1003 );
1004 }
1005
1006 /// Issue #790: `Ops::operands` / `Ops::operators` are the *distinct*
1007 /// (deduplicated) Halstead operand/operator vocabularies (`n2` / `n1`),
1008 /// not every occurrence. Pin the documented dedup semantics: each
1009 /// vector's length equals its unique-element count. The fixture
1010 /// repeats `+`, `;`, and `=` operators and the `a` operand so a
1011 /// regression to non-deduplicated collection would make `len` exceed
1012 /// the unique count.
1013 #[cfg(feature = "rust")]
1014 #[test]
1015 fn ops_vocabularies_are_distinct_790() {
1016 use std::collections::HashSet;
1017
1018 let src = b"fn main() { let a = 1 + 1; let b = a + a; }\n".to_vec();
1019 let ops = Ast::parse(Source::new(LANG::Rust, &src).with_name(Some("foo.rs".to_owned())))
1020 .expect("rust feature enabled")
1021 .ops()
1022 .expect("ops walk must yield a top-level Ops");
1023
1024 let unique_operators: HashSet<&String> = ops.operators.iter().collect();
1025 assert_eq!(
1026 ops.operators.len(),
1027 unique_operators.len(),
1028 "Ops::operators must be the distinct operator vocabulary (n1)"
1029 );
1030
1031 let unique_operands: HashSet<&String> = ops.operands.iter().collect();
1032 assert_eq!(
1033 ops.operands.len(),
1034 unique_operands.len(),
1035 "Ops::operands must be the distinct operand vocabulary (n2)"
1036 );
1037 }
1038
1039 /// Assert that every space in the tree carries sorted vocabularies,
1040 /// and return how many spaces were checked so a caller can prove the
1041 /// walk actually descended.
1042 ///
1043 /// The length floor is what keeps this from going quietly vacuous:
1044 /// `is_sorted` is trivially true for an empty or single-entry
1045 /// vector, so without it a fixture that stopped producing real
1046 /// vocabularies would keep passing while covering nothing.
1047 fn assert_sorted_spaces(ops: &Ops, lang: LANG) -> usize {
1048 /// Smallest vocabulary in which an ordering is observable.
1049 const MIN_OBSERVABLE: usize = 2;
1050
1051 let mut stack = vec![ops];
1052 let mut visited = 0;
1053
1054 while let Some(space) = stack.pop() {
1055 visited += 1;
1056 for (field, values) in [
1057 ("operators", &space.operators),
1058 ("operands", &space.operands),
1059 ] {
1060 assert!(
1061 values.len() >= MIN_OBSERVABLE && values.is_sorted(),
1062 "{lang:?} {field} of space {:?} (@{}) must hold at least \
1063 {MIN_OBSERVABLE} entries and be sorted: {values:?}",
1064 space.name,
1065 space.start_line
1066 );
1067 }
1068 stack.extend(space.spaces.iter());
1069 }
1070
1071 visited
1072 }
1073
1074 /// Every space's vocabularies come back sorted, in every language.
1075 ///
1076 /// The operator vocabulary is the union of two maps — one keyed by
1077 /// token id, one keyed by text, which is where primitive types such
1078 /// as C++ `int` land — and sorting is what interleaves them rather
1079 /// than leaving the second concatenated onto the first (#1091). The
1080 /// `spans_both_maps` pair per case names one entry from each map, so
1081 /// a fixture that stopped exercising the text-keyed map fails here
1082 /// instead of silently narrowing the test's reach.
1083 #[test]
1084 fn ops_vocabularies_are_sorted_1091() {
1085 /// `(language, file name, source, (text-keyed operator,
1086 /// token-id-keyed operator that must sort after it))`.
1087 type Case = (
1088 LANG,
1089 &'static str,
1090 &'static str,
1091 (&'static str, &'static str),
1092 );
1093
1094 let cases: &[Case] = &[
1095 #[cfg(feature = "rust")]
1096 (
1097 LANG::Rust,
1098 "rust.rs",
1099 "fn zeta(quux: u32) -> u32 { let mid = quux + 1; \
1100 let alpha = |beta: u32| beta * mid; alpha(mid) - quux }\n",
1101 ("u32", "|"),
1102 ),
1103 #[cfg(feature = "cpp")]
1104 (
1105 LANG::Cpp,
1106 "cpp.cpp",
1107 "int zeta(int quux) { double mid = quux + 1; \
1108 char alpha = 'z'; return quux - mid + alpha; }\n",
1109 ("int", "return"),
1110 ),
1111 #[cfg(feature = "java")]
1112 (
1113 LANG::Java,
1114 "Java.java",
1115 "class Zeta { int quux(int mid) { long alpha = mid + 1; \
1116 boolean beta = alpha > 2; return beta ? mid : 0; } }\n",
1117 ("long", "return"),
1118 ),
1119 #[cfg(feature = "python")]
1120 (
1121 LANG::Python,
1122 "python.py",
1123 "def zeta(quux):\n mid = quux + 1\n \
1124 def alpha(beta):\n return beta * mid\n return alpha(mid) - quux\n",
1125 // Python has no primitive-type operators; both entries
1126 // come from the token-id map, so this pair only pins the
1127 // ordering, not the interleaving.
1128 ("def", "return"),
1129 ),
1130 #[cfg(feature = "typescript")]
1131 (
1132 LANG::Typescript,
1133 "ts.ts",
1134 "function zeta(quux: number): number { const mid: number = quux + 1; \
1135 const alpha = (beta: number) => beta * mid; return alpha(mid) - quux; }\n",
1136 ("number", "return"),
1137 ),
1138 ];
1139
1140 for (lang, file, source, (from_text_map, sorts_after)) in cases {
1141 let ops = Ast::parse(
1142 Source::new(*lang, source.as_bytes()).with_name(Some((*file).to_owned())),
1143 )
1144 .expect("language feature enabled")
1145 .ops()
1146 .expect("ops walk must yield a top-level Ops");
1147
1148 let position = |needle: &str| {
1149 ops.operators
1150 .iter()
1151 .position(|op| op == needle)
1152 .unwrap_or_else(|| {
1153 panic!(
1154 "{lang:?} operators must contain {needle:?}: {:?}",
1155 ops.operators
1156 )
1157 })
1158 };
1159 assert!(
1160 position(from_text_map) < position(sorts_after),
1161 "{lang:?} must order {from_text_map:?} before {sorts_after:?}: {:?}",
1162 ops.operators
1163 );
1164
1165 assert!(
1166 assert_sorted_spaces(&ops, *lang) > 1,
1167 "{lang:?} sample must nest at least one sub-space"
1168 );
1169 }
1170 }
1171
1172 /// Two parses of the same bytes in one process must agree exactly.
1173 ///
1174 /// `RandomState` bumps its thread-local seed per instance, so the
1175 /// pre-fix code could — and did — order two `HashMap`s built from
1176 /// identical keys differently within a single run. This is the
1177 /// in-process form of the cross-run churn in #1091.
1178 #[test]
1179 #[cfg(feature = "rust")]
1180 fn ops_are_stable_across_repeated_parses_1091() {
1181 use std::fmt::Write as _;
1182
1183 // Enough distinct operands, in enough distinct spaces, that
1184 // agreement by coincidence is not a plausible explanation for a
1185 // pass.
1186 let mut src = String::new();
1187 for i in 0..40 {
1188 writeln!(src, "fn name{i}(arg{i}: u32) -> u32 {{ arg{i} + {i} }}")
1189 .expect("writing to a String cannot fail");
1190 }
1191
1192 let parse = || {
1193 Ast::parse(Source::new(LANG::Rust, src.as_bytes()).with_name(Some("foo.rs".to_owned())))
1194 .expect("rust feature enabled")
1195 .ops()
1196 .expect("ops walk must yield a top-level Ops")
1197 };
1198
1199 let (first, second) = (parse(), parse());
1200 assert!(
1201 first.operands.len() >= 40 && first.spaces.len() >= 40,
1202 "sample must have a wide vocabulary across many spaces, got {} operands \
1203 in {} spaces",
1204 first.operands.len(),
1205 first.spaces.len()
1206 );
1207 // `Ops` has no `PartialEq`, and the nested spaces are the half a
1208 // top-level vector comparison would miss, so compare the whole
1209 // serialized tree.
1210 let render =
1211 |ops: &Ops| serde_json::to_string(&ops.to_wire()).expect("wire Ops serializes to JSON");
1212 assert_eq!(render(&first), render(&second));
1213 }
1214
1215 /// A vocabulary that lost bytes is ordered by its *rendered* text.
1216 ///
1217 /// #1110 moved the sort ahead of the lossy UTF-8 rendering, which is
1218 /// only order-preserving while every key is valid UTF-8. Here it is
1219 /// not: the two string operands are `"\xffA"` and `"\u{fffd}B"`, so
1220 /// by raw bytes the first sorts *after* the second (`0xff` > `0xef`)
1221 /// and by rendered text — both start `U+FFFD`, then `A` before `B` —
1222 /// it sorts before. The rendered order is what the pre-#1110
1223 /// render-then-sort code produced and what `Ops` documents, so
1224 /// dropping the fallback re-sort flips this pair and fails here.
1225 #[test]
1226 #[cfg(feature = "rust")]
1227 fn ops_vocabulary_orders_lossy_entries_by_rendered_text_1110() {
1228 let mut src = b"fn f() { let a = \"".to_vec();
1229 src.push(0xff);
1230 src.extend_from_slice(b"A\"; let b = \"");
1231 src.extend_from_slice(&[0xef, 0xbf, 0xbd]);
1232 src.extend_from_slice(b"B\"; }\n");
1233
1234 let ops = Ast::parse(Source::new(LANG::Rust, &src).with_name(Some("foo.rs".to_owned())))
1235 .expect("rust feature enabled")
1236 .ops()
1237 .expect("ops walk must yield a top-level Ops");
1238
1239 let position = |needle: &str| {
1240 ops.operands
1241 .iter()
1242 .position(|operand| operand == needle)
1243 .unwrap_or_else(|| panic!("operands must contain {needle:?}: {:?}", ops.operands))
1244 };
1245 assert!(
1246 position("\"\u{fffd}A\"") < position("\"\u{fffd}B\""),
1247 "lossy entries must be ordered by rendered text, got {:?}",
1248 ops.operands
1249 );
1250 assert!(
1251 ops.operands.is_sorted(),
1252 "the whole vocabulary must be sorted as rendered, got {:?}",
1253 ops.operands
1254 );
1255 }
1256
1257 /// The walk classifies a space kind once per space, not once per node.
1258 ///
1259 /// Nothing in the output distinguishes the two: the classification
1260 /// only ever reaches `Ops::new`, so running it on every node produces
1261 /// the same tree and merely throws the extra answers away. #1110
1262 /// moved the call inside the `func_space` branch, mirroring
1263 /// `spaces::compute::open_func_space`; the counter is what makes
1264 /// hoisting it back out a failure. The node-count assertion is what
1265 /// makes the counts distinguishable — a fixture whose nodes and
1266 /// spaces were equal in number could not tell the two apart.
1267 // test-lang-gates: hand-written(rust) — the one grammar that
1268 // makes the fixture list non-empty, so the non-vacuity
1269 // assertion cannot fire on a minimal build
1270 #[test]
1271 // Gated on the language that guarantees a non-empty case list, so
1272 // the emptiness assertion below cannot fire on a minimal build.
1273 #[cfg(feature = "rust")]
1274 fn ops_classifies_space_kind_once_per_space_1110() {
1275 let cases: &[(LANG, &str, &str)] = &[
1276 #[cfg(feature = "rust")]
1277 (
1278 LANG::Rust,
1279 "foo.rs",
1280 "fn outer(a: u32) -> u32 { fn inner(b: u32) -> u32 { b + 1 } inner(a) * 2 }\n",
1281 ),
1282 #[cfg(feature = "python")]
1283 (
1284 LANG::Python,
1285 "foo.py",
1286 "def outer(a):\n def inner(b):\n return b + 1\n return inner(a) * 2\n",
1287 ),
1288 #[cfg(feature = "cpp")]
1289 (
1290 LANG::Cpp,
1291 "foo.cpp",
1292 "struct S { int m(int a) { return a + 1; } }; int f(int b) { return b * 2; }\n",
1293 ),
1294 #[cfg(feature = "java")]
1295 (
1296 LANG::Java,
1297 "Foo.java",
1298 "class C { int m(int a) { return a + 1; } int n(int b) { return b * 2; } }\n",
1299 ),
1300 #[cfg(feature = "javascript")]
1301 (
1302 LANG::Javascript,
1303 "foo.js",
1304 "function outer(a) { function inner(b) { return b + 1; } return inner(a) * 2; }\n",
1305 ),
1306 ];
1307 crate::test_support::assert_fixtures_present(cases);
1308
1309 for (lang, file, source) in cases {
1310 let ast = crate::test_support::parse_named(*lang, file, source);
1311
1312 let before = super::space_kind_lookups::observed();
1313 let ops = ast.ops().expect("ops walk must yield a top-level Ops");
1314 let lookups = super::space_kind_lookups::observed() - before;
1315
1316 let mut spaces = 0;
1317 let mut stack = vec![&ops];
1318 while let Some(space) = stack.pop() {
1319 spaces += 1;
1320 stack.extend(space.spaces.iter());
1321 }
1322
1323 let mut nodes = 0;
1324 let mut cursor = vec![ast.as_tree_sitter().root_node()];
1325 while let Some(node) = cursor.pop() {
1326 nodes += 1;
1327 let mut walker = node.walk();
1328 cursor.extend(node.children(&mut walker));
1329 }
1330
1331 assert!(
1332 nodes > spaces * 4,
1333 "{lang:?} fixture must have many more nodes ({nodes}) than spaces ({spaces}) \
1334 for the two counts to be distinguishable"
1335 );
1336 assert_eq!(
1337 lookups, spaces,
1338 "{lang:?} must classify once per space, not once per node ({nodes} nodes)"
1339 );
1340 }
1341 }
1342
1343 /// One flattened space: `(depth, kind, name, start_line, end_line)`.
1344 #[cfg(feature = "elixir")]
1345 type FlatSpace = (usize, crate::SpaceKind, String, usize, usize);
1346
1347 /// Flattens an `Ops` tree in preorder, so a test can pin the whole
1348 /// tree in one `assert_eq!` and see the surrounding spaces when one
1349 /// is wrong.
1350 ///
1351 /// `end_line` is carried as well as `start_line` because a change to
1352 /// the promote predicate can move a space's *extent* without moving
1353 /// its head — a `def` that swallows its sibling would keep the same
1354 /// start line.
1355 #[cfg(feature = "elixir")]
1356 fn flatten(ops: &Ops, depth: usize, out: &mut Vec<FlatSpace>) {
1357 out.push((
1358 depth,
1359 ops.kind,
1360 ops.name.clone().unwrap_or_else(|| "<none>".to_owned()),
1361 ops.start_line,
1362 ops.end_line,
1363 ));
1364 for child in &ops.spaces {
1365 flatten(child, depth + 1, out);
1366 }
1367 }
1368
1369 #[cfg(feature = "elixir")]
1370 fn elixir_ops_tree(source: &str) -> Vec<FlatSpace> {
1371 let ops = crate::test_support::parse_named(LANG::Elixir, "foo.ex", source)
1372 .ops()
1373 .expect("ops walk must yield a top-level Ops");
1374 let mut flat = Vec::new();
1375 flatten(&ops, 0, &mut flat);
1376 flat
1377 }
1378
1379 /// Issue #1130: Elixir's `defmodule` / `def` are `Call` nodes whose
1380 /// target identifier text spells the keyword, so only the
1381 /// source-aware promote predicate can recognise them. Before the
1382 /// fix `ops()` returned the bare file-level `Unit` for this input
1383 /// while `metrics()` returned the full module/function tree.
1384 #[cfg(feature = "elixir")]
1385 #[test]
1386 fn elixir_ops_opens_module_and_function_spaces_1130() {
1387 use crate::SpaceKind::{Class, Function, Unit};
1388
1389 assert_eq!(
1390 elixir_ops_tree("defmodule Foo do\n def bar(x) do\n x + 1\n end\nend\n"),
1391 vec![
1392 (0, Unit, "foo.ex".to_owned(), 1, 5),
1393 (1, Class, "Foo".to_owned(), 1, 5),
1394 (2, Function, "bar".to_owned(), 2, 4),
1395 ],
1396 );
1397 }
1398
1399 /// An `AnonymousFunction` is the one Elixir space the byte-less
1400 /// predicate could already see, so this pins that the source-aware
1401 /// predicate did not lose it — and that it nests under the `def`
1402 /// space rather than being reparented to the file root.
1403 #[cfg(feature = "elixir")]
1404 #[test]
1405 fn elixir_ops_opens_anonymous_function_space() {
1406 use crate::SpaceKind::{Class, Function, Unit};
1407
1408 assert_eq!(
1409 elixir_ops_tree(
1410 "defmodule Foo do\n def bar(list) do\n \
1411 Enum.map(list, fn x -> x * 2 end)\n end\nend\n"
1412 ),
1413 vec![
1414 (0, Unit, "foo.ex".to_owned(), 1, 5),
1415 (1, Class, "Foo".to_owned(), 1, 5),
1416 (2, Function, "bar".to_owned(), 2, 4),
1417 (3, Function, "<anonymous>".to_owned(), 3, 3),
1418 ],
1419 );
1420 }
1421
1422 /// Issue #310: a `def` inside `quote do … end` is a code *template*
1423 /// emitted later by macro expansion, not a declaration of the
1424 /// enclosing module, so it opens no space. This is the case only the
1425 /// source-aware predicate can get right — the byte-less one never
1426 /// saw any `def` at all, so it was accidentally "correct" here while
1427 /// being wrong everywhere else.
1428 #[cfg(feature = "elixir")]
1429 #[test]
1430 fn elixir_ops_skips_def_inside_quote_block_310() {
1431 use crate::SpaceKind::{Class, Function, Unit};
1432
1433 // The quoted `def` heads line 4. Its name resolves to the
1434 // `<anonymous>` placeholder (the head is `unquote(name)`, not a
1435 // literal identifier), so the absence of a fourth entry — not a
1436 // name match — is what pins it out of the tree.
1437 assert_eq!(
1438 elixir_ops_tree(
1439 "defmodule Foo do\n defmacro gen(name) do\n quote do\n \
1440 def unquote(name)(x) do\n x + 1\n end\n end\n end\nend\n",
1441 ),
1442 vec![
1443 (0, Unit, "foo.ex".to_owned(), 1, 9),
1444 (1, Class, "Foo".to_owned(), 1, 9),
1445 (2, Function, "gen".to_owned(), 2, 8),
1446 ],
1447 );
1448 }
1449}