|
| 1 | +import type { Expect, Equal } from "type-testing"; |
| 2 | + |
| 3 | +type AnalyzeScope<T> = T extends `${" " | "\n" | "\t" | "\r"}${infer Tail}` |
| 4 | + ? AnalyzeScope<Tail> |
| 5 | + : T extends `${"let" | "const" | "var"} ${infer Variable} = "${string}";${infer Tail}` |
| 6 | + ? { |
| 7 | + declared: [Variable, ...AnalyzeScope<Tail>["declared"]]; |
| 8 | + used: AnalyzeScope<Tail>["used"]; |
| 9 | + } |
| 10 | + : T extends `${string}(${infer Param});${infer Tail}` |
| 11 | + ? { |
| 12 | + declared: AnalyzeScope<Tail>["declared"]; |
| 13 | + used: [Param, ...AnalyzeScope<Tail>["used"]]; |
| 14 | + } |
| 15 | + : { declared: []; used: [] }; |
| 16 | + |
| 17 | +// ------------------- Test section --------------------- |
| 18 | + |
| 19 | +type t0_actual = AnalyzeScope<` |
| 20 | +let teddyBear = "standard_model"; |
| 21 | +`>; |
| 22 | +interface t0_expected { |
| 23 | + declared: ["teddyBear"]; |
| 24 | + used: []; |
| 25 | +} |
| 26 | +type t0 = Expect<Equal<t0_actual, t0_expected>>; |
| 27 | + |
| 28 | +type t1_actual = AnalyzeScope<` |
| 29 | +buildToy(teddyBear); |
| 30 | +`>; |
| 31 | +interface t1_expected { |
| 32 | + declared: []; |
| 33 | + used: ["teddyBear"]; |
| 34 | +} |
| 35 | +type t1 = Expect<Equal<t1_actual, t1_expected>>; |
| 36 | + |
| 37 | +type t2_actual = AnalyzeScope<` |
| 38 | +let robotDog = "deluxe_model"; |
| 39 | +assembleToy(robotDog); |
| 40 | +`>; |
| 41 | +interface t2_expected { |
| 42 | + declared: ["robotDog"]; |
| 43 | + used: ["robotDog"]; |
| 44 | +} |
| 45 | +type t2 = Expect<Equal<t2_actual, t2_expected>>; |
| 46 | + |
| 47 | +type t3_actual = AnalyzeScope<` |
| 48 | + let robotDog = "standard_model"; |
| 49 | + const giftBox = "premium_wrap"; |
| 50 | + var ribbon123 = "silk"; |
| 51 | + |
| 52 | + \t |
| 53 | + wrapGift(giftBox); |
| 54 | + \r\n |
| 55 | + addRibbon(ribbon123); |
| 56 | +`>; |
| 57 | +interface t3_expected { |
| 58 | + declared: ["robotDog", "giftBox", "ribbon123"]; |
| 59 | + used: ["giftBox", "ribbon123"]; |
| 60 | +} |
| 61 | +type t3 = Expect<Equal<t3_actual, t3_expected>>; |
| 62 | + |
| 63 | +type t4_input = "\n\t\r \t\r "; |
| 64 | +type t4_actual = AnalyzeScope<t4_input>; |
| 65 | +interface t4_expected { |
| 66 | + declared: []; |
| 67 | + used: []; |
| 68 | +} |
| 69 | +type t4 = Expect<Equal<t4_actual, t4_expected>>; |
0 commit comments