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