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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
// Expression-specific lookahead helpers for arrow function and type argument disambiguation
//
// These functions scan raw bytes to disambiguate syntactic constructs that
// look similar initially but parse differently:
// - Arrow functions vs parenthesized expressions: `(x) => y` vs `(x)`
// - Generic arrow functions vs comparison: `<T>() => x` vs `a < b`
// - Type arguments vs comparison chain: `foo<T>()` vs `foo < a`
//
// All functions operate on byte slices for performance (no tokenization needed).
use TypeArgScan;
use ;
use crate;
use ;
/// `<` at `pos` is `<=` comparison operator, not an angle bracket open
/// `>` at `pos` is preceded by `=`, making it part of `=>` arrow operator
/// `>` at `pos` is a `>=` token rather than a type-argument close — every `>` an `=`
/// follows, as tsc's re-scan reads it (`reScanGreaterToken`), so `f<T>==c` closes no list
/// (`f < T >= = c`, a syntax error to tsc and to acorn-typescript alike). The one carve-out
/// is `>=>`, a close glued to an arrow (`<T>(x): A<B>=> x`), which keeps its `>` as the
/// close the type parser needs.
/// A matched arrow head's byte extents, built by the parser's three head
/// predicates and read by the consequent-context rule
/// ([`super::Parser::parse_arrow_or_rewind`]) — which asks two questions of it,
/// both keyed on what the match already found so neither re-walks the head.
pub
/// tsc's `isModifierKind` set, minus `async` — the words that may open a
/// parameter-property parameter (`(public a)`). `async` is excluded because it is
/// the arrow's own modifier, and because `(async a)` reads as two names.
///
/// Order is alphabetical for reading only; the lookup is a linear compare over fourteen
/// short words on a cold path.
const PARAM_MODIFIERS: & = &;
/// Whether the parameter list opening at the `(` at `paren` is one tsc reads as a
/// signature **without asking** — `Tristate.True` from
/// `isParenthesizedArrowFunctionExpressionWorker`, where
/// `tryParseParenthesizedArrowFunctionExpression` parses committed and passes
/// `allowReturnTypeInArrowFunction: true`.
///
/// The distinction is load-bearing for the consequent-context rule, and in BOTH
/// directions. tsc's `true` reaches the arrow's **body**, so an annotated head
/// inside a committed arrow's body keeps its own annotation and the conditional
/// runs out of `:`; speculating on such a head instead truncates the body, finds
/// the `:` the inner annotation would have taken, and keeps the outer arrow — a
/// reading neither tsc nor acorn has. And the converse: a head tsc leaves
/// `Unknown` (`(a)`, `(a, b)`, `(a = 1)`, `([a])`, `({a})`, every `<T>(…)`) has a
/// parenthesized-expression reading, so committing it would reject a program tsc
/// accepts.
///
/// Each arm below is one of tsc's, in its order — a `)`, a binding-pattern open, a
/// rest `...`, a parameter property, then the first name's follower. One
/// deliberate deviation: this reads any identifier-shaped word where tsc asks
/// `isIdentifier()`, so a **reserved** word (`(if: T)`, `(true?: T)`) commits here
/// and does not there. That cannot move a verdict — `(<reserved> :` and
/// `(<reserved> ?` spell no parenthesized expression either, so both readings
/// reject — and it keeps a keyword table out of a byte scan. `this` needs no such
/// argument: tsc admits it explicitly.
/// Scan an arrow head's parameter list from the `(` at `start`, returning the `)`
/// that closes it when `=>` follows — `None` where the pattern does not hold.
///
/// Handles nested parentheses, the strings / templates / comments / regexes a
/// `(` or `)` can hide inside ([`matching_paren_close`]), and an optional
/// return-type annotation after the `)` (so both `(...) =>` and `(...): type =>`
/// match).
pub
/// Find the `)` closing the `(` at `start`, stepping over every literal a `(` or `)`
/// can hide inside — strings, templates, comments, and **regex literals**.
///
/// The regex step is what separates this from
/// [`matching_delimiter_close`]: that walk leaves a `/…/`
/// significant, so a pattern holding an unescaped `)` (`/\)=>/`) hands it a close the
/// source does not have. Every caller that acts on the `)` ALONE — with no follow-token
/// filter behind it to refuse a false one — must use this walk, because a false close
/// picks up whatever byte follows it as if it were the group's own
/// ([`paren_list_arrow_end`]'s `=>`).
pub
/// Check if `=>` follows (possibly with type annotation `: type`)
/// Whether the return-type annotation opening at the `:` at `colon` is followed
/// **immediately** by `=>` — the signal that the `(…)` before it was an arrow's
/// parameter list, not a parenthesized expression.
///
/// Mirrors tsc's rule (`parseParenthesizedArrowFunctionExpression`): scan exactly
/// ONE type, then require `=>` right after it. Hunting for any `=>` at bracket
/// depth 0 is not enough, and tsc's own source names the counterexample —
/// "`a ? (b): c` will have `(b):` parsed as a signature with a return type
/// annotation". That counterexample's other half — `a ? (b) : c => d`, where the
/// one type IS followed by `=>` and the `:` is still the conditional's — is not
/// this scan's to settle: it needs the arrow's whole extent, so the parser asks it
/// by speculation (`Parser::parse_arrow_or_rewind`), as tsc does. Two shapes put a
/// stray `=>` within reach of such a scan, and in neither is the `:` a return-type
/// colon:
///
/// - a depth-0 `?`, which in a type is only a conditional type's `?` and so
/// requires an `extends` before it (`a ? (b, c) : d ? (p) => 1 : e`)
/// - a `=>` belonging to a **function type** in the annotation, which is the
/// type's own arrow and not the signature's (`a ? (b, c) : (p) => 1`)
///
/// Bytes that cannot occur at the top level of a type end the scan, so an
/// assignment (`d = …`), a non-null assertion (`d! ? …`) or a regex body
/// containing `=>` stops it rather than being scanned through.
/// Where the type scan stands in the type grammar — which shapes the next token
/// may legally take. The `Full`/`Operand` split is the byte-scan form of the
/// type parser's own `fn_type_disallowed` flag; the two encode the same rule and
/// must move together.
/// Type-level prefix keywords that take an operand of their own, so a `(` right
/// after one still belongs to the type (`keyof (A | B)`, `typeof import('m')`)
/// rather than ending it.
const TYPE_PREFIX_WORDS: & = &;
/// Type-level keywords after which a *whole* type may start again, function and
/// construct types included — the [`TypePos::Full`] counterpart of
/// [`TYPE_PREFIX_WORDS`]. `new`/`abstract` open a construct type, whose
/// `(params) =>` is the function-type shape. A type predicate's `is` hands its
/// operand to the same type entry as the plain `: type` branch — acorn-typescript's
/// `tsParseTypeOrTypePredicateAnnotation` calls `tsParseTypeAnnotation` either way,
/// and tsv's two predicate paths likewise call `Parser::parse_type` — so `x is T`
/// stands where the return-type colon itself stood, and `x is (A | B)[]` /
/// `x is (p: P) => R` scan like any other annotation.
///
/// `extends` belongs to this class too but keeps its own arm: it additionally arms
/// the conditional-type `?`. Matching is whole-word, so `isFoo` stays an atom.
const TYPE_FULL_POSITION_WORDS: & = &;
/// Whether the `(` at `paren` opens a **function type**'s parameter list rather
/// than a parenthesized type — acorn-typescript's
/// `tsIsUnambiguouslyStartOfFunctionType`, which decides whether a following
/// `=>` belongs to the type (`(b: B) => C`) or terminates it (`(B | C) => …`,
/// where the `=>` is the enclosing arrow's).
///
/// True for `()`, `(...`, and a parameter start (identifier, `this`, or a
/// balanced `{…}`/`[…]` pattern) followed by `:`, `,`, `?`, `=`, or by `) =>`.
///
/// Three callers ask it, for three different `(`s, because it is one question about
/// one shape:
///
/// - the return-type scan above, deciding whether a `=>` past the group is the type's
/// own or the enclosing arrow's;
/// - the type-argument head
/// ([`is_type_arguments_start`](super::Parser::is_type_arguments_start)'s `(` arm),
/// where a parameter list behind the `<` makes every byte to the region's own `>` the
/// function type's;
/// - the type parser's own `(` (`Parser::parse_parenthesized_or_function_type`), at a
/// full-type position: a parameter list, or else a parenthesized type whatever its
/// first token is.
///
/// **tsc's `isUnambiguouslyStartOfFunctionType` admits one shape more**: it runs
/// `parseModifiers` ahead of the parameter start, so `(readonly a: T) => U` and
/// `(public a: T) => U` are function types to the compiler. acorn-typescript's
/// `tsSkipParameterStart` has no modifier step and rejects both outright, so this —
/// the rule the AST wire answers to — is acorn's. tsc's extra step is spelled
/// separately, for the one site that owes it an answer
/// ([`paren_starts_modified_parameter_list`]).
pub
/// [`paren_starts_function_type`] with tsc's MODIFIER step ahead of the parameter
/// start — `parseModifiers` in `skipParameterStart`, which is what makes
/// `(readonly a: T) => U` and `(public a: T) => U` function types to the compiler.
///
/// Only the type-argument head asks it, and for one reason: a parameter property is
/// a *grammar* error rather than a parse failure, so the compiler and prettier both
/// read `f<(readonly a: T) => U>(x)` as a generic call and complain about the
/// modifier. Reading the region as a comparison instead would print a different
/// program, so tsv claims it too and lets the type parse produce the rejection.
/// acorn-typescript rejects the input outright, so there is no wire to match and
/// nothing is lost by taking tsc's shape here.
///
/// A modifier is only one where a parameter start follows it (tsc's
/// `canFollowModifier`): everywhere else the same word is the parameter's own NAME,
/// which is what keeps `(readonly: T)`, `(readonly)` and `(readonly, b)` reading
/// exactly as [`paren_starts_function_type`] reads them, and `(readonly + 1)` /
/// `(readonly.a)` out of this arm entirely. At least one modifier must be consumed,
/// so every plain parameter list is the other predicate's alone.
pub
/// The body of [`paren_starts_function_type`], asked at the first significant byte
/// INSIDE the `(` rather than at the paren — so the modifier step can re-ask it past
/// the modifiers it consumed.
/// Check whether an `=>` follows the identifier that ends at `ident_end`
///
/// Detects single-parameter arrow functions without parentheses: `x => expr`
/// Returns `true` if pattern `identifier =>` is found (with optional whitespace/comments).
///
/// `ident_end` is the END of the name, not its start: the caller is positioned on a
/// token the lexer has already scanned and delimited, so the name's extent is a fact
/// the parser holds rather than one this scan re-derives. Re-deriving it walked the
/// whole name a second time — a mean of 6.7 bytes over 518,615 calls per pass across
/// the corpus, the single hottest source line in this lookahead on all three boards —
/// and re-derived it through a class that is deliberately **wider** than the lexer's
/// (`skip_identifier` steps over any multi-byte sequence rather than validating
/// `ID_Continue`, see its module). The two therefore agree on every name the lexer
/// accepted — measured over all 518,615 calls, with no disagreement — and can differ
/// only where the next byte is a non-ASCII non-identifier one, i.e. only where the
/// source is already a lex error. Taking the token's own end is the narrower answer as
/// well as the free one.
pub
/// Scan through angle brackets `<...>` for type parameters
///
/// Assumes `pos` is at `<`. Returns position after closing `>`, or 0 if not found.
/// Handles nested angle brackets, comments, and arrow functions in constraints: `<T extends () => void>`
pub
/// Scan for closing `>` at angle depth 0, tracking all delimiter depths.
///
/// Used by `is_type_arguments_start` to verify that a sequence like `<T | U>`
/// or `<T, (x: number) => void>` is actually type arguments (finds matching `>`).
///
/// Returns `true` if a matching `>` is found before hitting an unbalanced
/// `)`, `]`, `}`, or `;` at depth 0, and the close isn't followed by a token
/// that refuses the list: a `>`-led token (`>`, `>>`, `>>>`, `>>=`, `>>>=`, a lone
/// `>=` only when glued), an `=` glued to the close (see
/// [`type_args_close_is_joined_or_gt_followed`]), or an expression-starting token on the
/// same line (`/=` included, as a regular expression's head).
///
/// The follower half is the one the two readings disagree on. [`TypeArgScan::Relex`] asks
/// about the REGION alone and stops at the matching `>`: past a line terminator the
/// [`TypeArgScan::Parse`] arm below commits the list ahead of any expression, and whether the
/// printer puts a break there is unknowable where the question is asked. (Which followers
/// commit, for tsc and for acorn-typescript, is stated in `docs/conformance_prettier_ts.md`
/// §Relational chain type-argument parens; where tsc refuses one this parse commits on,
/// [`TypeArgScan`]'s soundness property is why the pair still stands.) The `>`-led token is
/// NOT part of that half — a `>>` / `>>=` is one token the printer can never split, so both
/// readings reject it.
pub
/// Whether the token at `after` — the first past a type-argument list whose closing `>`
/// ends at `close_end` — refuses the list by joining or following that `>`. Two rules,
/// both tsc's:
///
/// - A `>` or `=` GLUED to the close means the close was never one: in an expression the
/// longest punctuator swallows it (`reScanGreaterToken`), so `a<b>>c` is `a < (b >> c)` —
/// acorn re-reads it the same way — `f<T>>= c` is `f < T >>= c`, a comparison assigned
/// to, and `f<() => U>=c` leaves `() => U` nothing to be. The glued `=` reaches here only
/// from a head that committed its list before the follower was read (a function type) or
/// through `>=>`; [`is_greater_equal_op`] keeps every other `>=` from closing at all.
/// - Past a gap, any `>`-led token disqualifies the list
/// (`canFollowTypeArgumentsInExpression` — the close and the `>` would be ambiguous with
/// a re-scanned `>>`): `f<T> > c`, `f<T> >> c`, `f<T> >>= c`, `f<T> >>>= c`.
///
/// The one exception is a spaced lone `>=`: acorn-typescript reads `f<T> >= c` as
/// `(f<T>) >= c` where tsc refuses the list, and tsv keeps acorn's reading (the printer
/// repairs it to the pair every parser reads alike). acorn also reads the list ahead of a
/// spaced `>>=` / `>>>=`, but refuses that as an assignment target except inside a paren
/// or a call argument list, where it skips the check; tsv follows tsc there.
/// Whether the token at `after`, the first past a type-argument list that ended at
/// `close_end`, makes the list's `>` a comparison operator: a token that can start an
/// expression, with no line break before it (acorn's
/// `tokenCanStartExpression && !hasPrecedingLineBreak` bail). `(` (call), a template
/// (tagged template), and other non-expression tokens (`;`, `,`, `)`, `.`, an operator…)
/// continue the instantiation — and across a line break an expression-starting token
/// leaves the `<…>` an instantiation too, whether it then begins a new statement via ASI
/// (`x<y>⏎c`) or continues as an operand (`x<y>⏎++c`). This is acorn-typescript's rule; the
/// followers tsc refuses beyond it — a `<` on any line, a `+` / `-` past a line break — are
/// asked of the list once it is read ([`type_args_follower_refused_by_tsc_alone`]). The
/// whole follower split, tsc's beside acorn's, is in `docs/conformance_prettier_ts.md`
/// §Relational chain type-argument parens.
/// Whether the token at `after`, the first past a type-argument list whose closing `>`
/// ends at `close_end`, is one tsc's `canFollowTypeArgumentsInExpression` refuses and
/// acorn-typescript does not: a lone `<` on any line, or a lone `+` / `-` past a line
/// terminator.
///
/// tsc turns `<`, `>`, `+` and `-` away BEFORE it asks about a line break — a list
/// followed by `<` never makes sense to it, a `>` would be ambiguous with a re-scanned
/// `>>`, and in that position `+` and `-` are unary operators. acorn-typescript refuses
/// the `>`-led run too ([`type_args_close_is_joined_or_gt_followed`]) and, on the list's
/// own line, the `+` / `-` ([`expression_follows_type_args_close`]); what it keeps is a `<`
/// on any line and a `+` / `-` past a line break (`f<T><U>(z)` is its call
/// `(f<T>)<U>(z)`, `f<T>⏎+c` its sum `(f<T>) + c`). Those are what this answers, so the
/// lookahead keeps acorn's verdict and the parser asks this of a list it has read
/// (`Parser::parse_type_arguments_or_rewind`), taking tsc's comparison only where it
/// parses. `<<`, `<=`, `++`, `--`, `+=` and `-=` are other tokens, past which tsc keeps the
/// list (`f<T>⏎++c` is an instantiation and a new statement, `f<T> <= c` an instantiation
/// compared).
pub
/// Whether the token at `after` REFUSES the type-argument list that ended at `close_end` —
/// the follower half of [`scan_for_closing_angle_bracket`]'s [`TypeArgScan::Parse`]
/// reading, for the parser to ask once the list is parsed and its close is known.
///
/// Every head but one has been asked already: its lookahead reached the close and read this
/// follower before committing. A FUNCTION-TYPE head commits without it
/// (`is_type_arguments_start`'s `(` and `<` arms), because `(…) =>` behind a `<` has no
/// comparison reading to fall back to — so where the follower refuses, no reading is left
/// at all and the parser rejects (`f<(a: T) => U> + 1`, `f<() => U> >> c`), as tsc and
/// acorn-typescript both do.
///
/// `as` and `satisfies` are the one place the two oracles part on such a head, and the one
/// word this differs from the scan on. To tsc they are binary operators, which continue an
/// instantiation (`f<(a: T) => U> as C`); acorn-typescript lexes them as plain names, which
/// start an expression. The scan follows acorn because a plain head has the chain acorn
/// reads (`a < b > as`); a function-type head has none, so the compiler's reading is the
/// only one there is.
pub
/// Where a generic function type's RETURN begins — the byte past its `=>` — if `pos` (the
/// first byte after an inner `<` that directly follows a type-argument-opening `<`)
/// begins one's type-parameter list: the only type that can start with `<`, so the only
/// valid reading of a `<<` at a type-argument position (`f<<T>(v: T) => void>()`). `Some`
/// iff the list's matching `>` is followed by a parenthesized parameter list whose
/// matching `)` is followed by `=>`. The `=>` requirement is what separates
/// the split from a shift-comparison chain whose right operand is
/// parenthesized (`a << b > (c)`): an arrow can never be a relational
/// operand, so real shift code never has `(…) =>` after the would-be close.
///
/// A generic CONSTRUCT type has the same tail behind its `new`
/// ([`construct_type_arrow_end`]), where `pos` is the first byte after that `<`.
pub
/// Where the RETURN type begins — the byte past the `=>` — if the `(` at `paren` opens a
/// parameter list whose matching `)` is followed by `=>`: the tail shared by a plain
/// function type (`(params) =>`), a generic function type's split (`<…>(params) =>`) and a
/// construct type (`new (params) =>`). `paren` must point at the `(`; comments between `)`
/// and `=>` are skipped. The `=>` is the signal that separates these types from a
/// shift/comparison chain or a `new Foo()` value (neither of which has `(…) =>`).
///
/// The offset is for the callers that GRADE the return rather than commit on the arrow
/// alone — a function or construct type inside an index, where an arrow function is as
/// well-formed; every other caller reads only whether there is one.
///
/// The close comes from [`matching_paren_close`], the REGEX-AWARE walk, because the
/// `=>` this reads is whatever byte follows the `)` — so a pattern holding an
/// unescaped `)` (`a < (b, /\)=>/) > c`, a comparison chain to both oracles) would
/// otherwise hand it a close inside the literal and an arrow that is the pattern's own
/// text.
pub
/// Where a construct-signature type's RETURN begins — the byte past its `=>` — if `pos`
/// begins one, `new (params) => R` or the generic `new <T>(params) => R`: the
/// `new`-prefixed sibling of [`paren_starts_function_type`]. `Some` iff a whole-word `new`
/// is followed by a parenthesized parameter list — behind a type-parameter list or not —
/// whose matching `)` is followed by `=>`. The `=>` is what separates the construct TYPE
/// from a `new Foo()` value expression, so `a < new Foo() > (c)` stays a comparison while
/// `f<new () => T>(x)` is a generic call — the same [`paren_list_arrow_end`] tail the
/// plain and generic function-type heads require, the second of which
/// ([`generic_function_type_arrow_end`]) reads the generic form here too: a `new` takes no
/// `<` behind it in an expression, so `new <T>(…) =>` has no other reading. At a
/// type-argument list's own level callers still gate on the closing-`>` follow-token
/// scan, so a construct type only reads as type arguments when a call/tagged-template/end
/// token actually follows the `>`. `pos` may point at `new` directly or (for
/// `abstract new`) past the `abstract` keyword.
pub
/// Find the `>` closing the angle-bracket list opened just before `pos`
/// (`pos` is the first byte after the `<`), or `None` if an unbalanced `)`,
/// `]`, `}`, or a top-level `;` intervenes.
///
// TODO: this walk is O(distance to the enclosing delimiter) and every `<` in a construct
// pays it, so a call whose arguments are all comparisons is quadratic in the argument count
// — `f(a1 < b1, …, aN < bN)` quadruples per doubling of N, against a `+` control that stays
// flat and unmeasurable. (A wall-clock figure is deliberately not quoted: the ratio is the
// property, and an absolute second belongs to one machine.) Both readings pay it — a `<` that
// opens no type-argument list reads to the construct's own close before it can say so. A memo keyed on the enclosing delimiter's span would collapse it; nothing
// real has the shape, so it has not been worth the state.
///
/// Operator disambiguation: `<=` and `>=` are comparison operators (not angle
/// brackets) and `=>` is an arrow operator (not a closing bracket).
///
/// The unbalanced `)` is the one stop the two readings split on. Under
/// [`TypeArgScan::Relex`] a `)` the region did not open is a **paren shell the printer
/// strips**, and the question is about the PRINTED bytes, where it is absent — so it does
/// not end the region and the scan reads on. Without that, the pair this reading exists to
/// justify would erase itself on the next pass: `(a < b) > c` re-read as source stops the
/// scan at its own `)` and prints back out as `a < b > c`. `]`, `}` and a top-level `;`
/// stop both readings — a bracket or brace is never a shell the printer strips, and a `;`
/// ends the statement in either spelling.
pub
/// Find the `)`/`]`/`}` closing the `(`/`[`/`{` at `open`, or `None` if a
/// different delimiter closes unbalanced first or the input ends. Strings,
/// templates, and comments are opaque, like `matching_angle_close` (and like it,
/// no regex skip — a `)` inside a regex parameter default could close early,
/// which only fails toward the shift/comparison reading).
pub
/// Whether the token starting at `pos` can begin an expression and therefore
/// turns a would-be type-argument `<…>` into a relational chain (acorn's
/// `tokenCanStartExpression` bail): identifier (covers keyword operands like
/// `typeof`), numeric literal (including `.5`), string, `[`, `{`, or a prefix
/// operator. The question is about the TOKEN, not its first byte: `!=`, `+=` and
/// `-=` share a byte with a prefix operator but start nothing. `(` (call) and
/// `` ` `` (tagged template) continue the instantiation instead and are
/// deliberately excluded; a `/`-led regex is excluded because acorn also rejects
/// `x < y > /a/` (a `/` there is the division operator to both parsers) — except one
/// opening `/=`, the head tsc's `isStartOfExpression` counts, since an assignment
/// operator never continues a list the way a binary one does.
pub
/// Whether the byte range contains an ECMAScript line terminator (LF, CR,
/// U+2028, U+2029 — the latter two as UTF-8 `e2 80 a8`/`a9`).
pub