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
use ;
use cratekotlin_bool_terminal_kinds;
use crate::*;
// Fitzpatrick's ABC rules adapted for Kotlin syntax. Kotlin shares the
// JVM and Java's spec roots: assignments count once per `=` / augmented
// assignment / ++ / --, branches count once per function invocation or
// object construction, conditions count comparison operators plus the
// `else` / `when`-entry / `catch` arms. Compared with the Java impl we
// stay token-level (matching the leaf kind_ids) rather than walking
// `Modifiers` children; the Kotlin grammar exposes the relevant
// operators directly as token nodes inside `binary_expression` and
// `assignment`.
//
// It does *not* have `prefix_expression` / `postfix_expression`, which
// this comment named until #1459. tree-sitter-kotlin-ng spells both
// unary positions as one `unary_expression` told apart only by its
// `operator` field — and believing the two-production version is how
// `kotlin_inspect_container` came to handle the prefix `!` and drop the
// postfix `!!` while reading as though it covered both.
// Returns true when this `=` token initialises an *immutable* (`val`)
// binding, whose initialiser is part of the declaration and therefore not
// an ABC assignment. The decision is structural: the `=` must be a direct
// child of a `property_declaration` or `class_parameter`, and that parent
// must carry a `val` keyword child. A `var`/plain declaration initialiser
// and any standalone `assignment` return false (they count).
// The operand a transparent wrapper wraps, and whether the wrapper
// itself proves that operand is boolean. `None` means the node is not a
// wrapper and the peel stops there.
//
// Only the prefix `!` proves booleanness: `!x` is boolean whatever `x`
// is, while parentheses, a null assertion and a cast are all
// type-preserving (`a!!` and `a as T` are boolean exactly when the slot
// they sit in is). So they inherit the caller's verdict rather than
// setting it.
//
// Two of the four shapes were not here from the start, and both were
// #1459 fallout from #1421 turning the `when` entry's blanket count
// into a condition slot: before that, `when { a!! -> … }` was paid for
// by the entry, and after it by this peel, which scored zero.
//
// - `unary_expression` is one kind for *both* Kotlin unary spellings,
// and the peel recognised only the prefix one. A postfix `a!!` stores
// its operand *before* the token, so the positional read for `!x`
// found the `!!` and stopped on a node the slot had already routed
// here as handled.
// - `as_expression` was not recognised at all, so a cast fell off the
// end of the slot's `else if`.
//
// By field, not index (`.claude/rules/grammar-dispatch.md` §3): both
// kinds name their parts (`operator` / `argument`, `left` / `right`), so
// one read serves the prefix and postfix spellings alike instead of the
// per-spelling index that produced the defect, and it survives a grammar
// re-order. It also survives an interposed `extra`: `when { ! /*c*/ a }`
// scores its condition, where the positional read scored zero.
//
// `parenthesized_expression` names nothing — its only child in
// node-types.json is the unlabelled inner `expression` — so it keeps the
// positional read it has always had, and with it the comment bug:
// `when { ( /*c*/ a) -> … }` still scores zero, because child(1) is the
// comment. Measured, not assumed. That is the same class as the C#
// `if` / `while` / `do` slots tracked in #1455 and left to their own
// change there; it predates #1459 and is recorded here rather than
// widened into it.
// Kotlin ABC unary-conditional walker (Fitzpatrick Rule 9; issue #557).
// tree-sitter-kotlin-ng parses `a && b || c` as a left-nested chain of
// flat `binary_expression` nodes carrying `&&` / `||` operator tokens,
// the same shape as the Java template. Negation surfaces as a
// `unary_expression` whose `operator` field is the `!` token; the
// condition slot may also be wrapped in `parenthesized_expression`, a
// postfix `!!` null assertion, or an `as` cast. All are unwrapped by
// `kotlin_inspect_container` to reach the inner bare operand, and they
// chain: `(a!! as Boolean)` peels three wrappers to one `identifier`.
// Counts each operand of a Kotlin `&&` / `||` chain that no token arm
// already owns. Mirrors `java_count_unary_conditions`: a comparison
// operand is a nested `binary_expression`, absent from
// `kotlin_bool_terminal_kinds!()`, and contributes nothing here because
// its operator token was counted directly; bare identifiers / calls /
// member accesses each add one. An `is` / `in` operand is in the same
// position as a comparison since #1461 — its own arm counts it, here or
// anywhere. Inner chain links and `!` / paren wrappers are routed through
// `kotlin_inspect_container`.
// Count the bare-predicate condition of a Kotlin `if`/`while`/`do-while`
// as one condition — Fitzpatrick's "unary conditional expression",
// mirroring `ruby_count_condition` / `rust_count_condition`.
// tree-sitter-kotlin-ng exposes the predicate via the `condition` field on
// `if_expression`, `while_statement`, and `do_while_statement`, so the
// field lookup is position-independent across all three forms. Since
// #1421 it also scores a subject-less `when` entry's condition, which is
// an `if` predicate in all but spelling. A bare terminal (`if (flag)`)
// counts directly; an `is` / `in` test does not, since #1461 gave it an
// arm of its own that fires here and outside a predicate alike. A
// comparison or boolean chain (`if (a == b)`, `if (a && b)`) is a nested
// `binary_expression` already counted by the comparison-token and
// `&&`/`||` walker arms, so it adds
// nothing here. A predicate wrapped in parentheses, a negation, a
// postfix `!!` null assertion or an `as` cast (`if ((flag))`,
// `if (!flag)`, `if (flag!!)`, `if (v as Boolean)`) is unwrapped by
// `kotlin_inspect_container`. Without this
// arm, idiomatic Kotlin bare predicates reported 0 ABC conditions while
// Kotlin's own cyclomatic counted the decision, breaking the
// conditions >= decisions invariant (#469/#473/#456/#696); issue #773.
// Returns true when the `when_expression` enclosing `entry` carries a
// subject — `when (x) { … }` rather than `when { … }`. The distinction
// decides how the entry's condition is scored (#1421): a subject-ful
// entry lists a *pattern* compared against the subject, a subject-less
// one lists an ordinary boolean expression.
//
// A `when_entry`'s parent IS the `when_expression`, and the grammar
// exposes no field for the subject (`when_expression` has an empty
// `fields` map in node-types.json), so the membership test is a scan of
// the parent's children rather than a `child_by_field_name`. Scanning
// rather than reading a fixed index also keeps a leading `extra` — a
// comment between `when` and `(x)` — from displacing the answer.
//
// The scan stops at the opening brace, which is what keeps it `O(1)`.
// The subject can only sit in the header, between `when` and `{`, so
// everything past the brace is arms — and an unbounded `any()` over a
// *subject-less* `when` never short-circuits, so it walks every arm
// once per arm. That is quadratic in the arm count, and measurable:
// before the bound, a generated 8,000-arm subject-less `when` took
// 2.5 s against 0.03 s for the same-size subject-ful control. The
// bound preserves the `extra` tolerance above — `when /*c*/ (x) {`
// still finds the subject, `when /*c*/ {` still stops at the brace —
// and degrades to the old whole-child scan only when error recovery
// leaves the brace out entirely, where the answer is unchanged.
// Scores one non-`else` `when` entry. Both `when` shapes contribute the
// same single decision that cyclomatic counts, but they pay for it in
// different places, and #1421 is the case where ABC charged for it twice.
//
// A subject-ful entry (`when (x) { in 1..2 -> … }`) lists a *pattern*,
// not an independent boolean expression: the decision is the implicit
// `x == pattern`, which nothing in the source spells, so the entry
// itself contributes the condition. A `range_test`, `type_test` or bare
// constant carries no token the comparison arms would count, and #1383's
// logic applies unchanged.
//
// A subject-less entry (`when { x > 5 -> … }`) lists an ordinary boolean
// expression — textually identical to an `if` predicate, and compiled as
// one — so it goes through the same slot `IfExpression` uses. Before
// this the entry added a blanket 1 *on top of* the comparison operator
// its condition already scored through the token arms, so
// `when { x > 5 -> 1; x < 0 -> 2; else -> 0 }` reported 4 conditions
// against a cyclomatic decision count of 2. The slot scores a bare
// terminal (`when { x -> … }`) directly and leaves a comparison, an
// `is` / `in` test (#1461) or an `&&` / `||` chain to the arms that
// already own it. Suppressing the operator instead would have been the wrong half to
// give way: a compound condition `when { a > 1 && b < 2 -> … }` needs
// both its comparisons, and suppression collapses it to one.
//
// The `condition` field is `multiple` — `when { a, b -> … }` lists
// alternatives — and only the first reaches the slot. That is
// deliberate: cyclomatic scores a multi-alternative entry as one
// decision, so routing every alternative through the slot would move
// `when { x, y -> … }` off that count rather than onto it. Alternatives
// after the first are still seen by the token arms.