|
| 1 | +import { readFileSync } from "fs"; |
| 2 | +import * as parser from "@babel/parser"; |
| 3 | +import type { NodePath } from "@babel/traverse"; |
| 4 | +import traverse from "@babel/traverse"; |
| 5 | +import type * as t from "@babel/types"; |
| 6 | +import * as babelTypes from "@babel/types"; |
| 7 | +import { z } from "zod"; |
| 8 | +import { getLogger } from "@/log"; |
| 9 | + |
| 10 | +export const EXPRESSION_PLACEHOLDER = "${...}"; |
| 11 | + |
| 12 | +export const TestLocationSchema = z.object({ |
| 13 | + testName: z.string(), |
| 14 | + startLine: z.number().int().positive(), |
| 15 | + endLine: z.number().int().positive(), |
| 16 | +}); |
| 17 | +export type TestLocation = z.infer<typeof TestLocationSchema>; |
| 18 | + |
| 19 | +const TestLocationsSchema = z.array(TestLocationSchema); |
| 20 | + |
| 21 | +export const parseShortestTestFile = (filePath: string): TestLocation[] => { |
| 22 | + const log = getLogger(); |
| 23 | + try { |
| 24 | + log.setGroup("File Parser"); |
| 25 | + |
| 26 | + const TemplateElementSchema = z.object({ |
| 27 | + value: z.object({ |
| 28 | + cooked: z.string().optional(), |
| 29 | + raw: z.string().optional(), |
| 30 | + }), |
| 31 | + }); |
| 32 | + type TemplateElement = z.infer<typeof TemplateElementSchema>; |
| 33 | + |
| 34 | + const StringLiteralSchema = z.object({ |
| 35 | + type: z.literal("StringLiteral"), |
| 36 | + value: z.string(), |
| 37 | + }); |
| 38 | + |
| 39 | + const TemplateLiteralSchema = z.object({ |
| 40 | + type: z.literal("TemplateLiteral"), |
| 41 | + quasis: z.array(TemplateElementSchema), |
| 42 | + }); |
| 43 | + |
| 44 | + const fileContent = readFileSync(filePath, "utf8"); |
| 45 | + const ast = parser.parse(fileContent, { |
| 46 | + sourceType: "module", |
| 47 | + plugins: [ |
| 48 | + "typescript", |
| 49 | + "objectRestSpread", |
| 50 | + "optionalChaining", |
| 51 | + "nullishCoalescingOperator", |
| 52 | + ], |
| 53 | + }); |
| 54 | + |
| 55 | + const testLocations: TestLocation[] = []; |
| 56 | + |
| 57 | + const testCallsByLine = new Map< |
| 58 | + number, |
| 59 | + { name: string; node: NodePath<t.CallExpression> } |
| 60 | + >(); |
| 61 | + |
| 62 | + traverse(ast, { |
| 63 | + CallExpression(path: NodePath<t.CallExpression>) { |
| 64 | + const node = path.node; |
| 65 | + |
| 66 | + if ( |
| 67 | + !node.type || |
| 68 | + node.type !== "CallExpression" || |
| 69 | + !node.callee || |
| 70 | + node.callee.type !== "Identifier" || |
| 71 | + node.callee.name !== "shortest" |
| 72 | + ) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const args = node.arguments || []; |
| 77 | + if (args.length === 0) return; |
| 78 | + |
| 79 | + const firstArg = args[0]; |
| 80 | + let testName = ""; |
| 81 | + |
| 82 | + if (babelTypes.isStringLiteral(firstArg)) { |
| 83 | + const parsed = StringLiteralSchema.parse(firstArg); |
| 84 | + testName = parsed.value; |
| 85 | + } else if (babelTypes.isTemplateLiteral(firstArg)) { |
| 86 | + const parsed = TemplateLiteralSchema.parse(firstArg); |
| 87 | + testName = parsed.quasis |
| 88 | + .map( |
| 89 | + (quasi: TemplateElement, i: number, arr: TemplateElement[]) => { |
| 90 | + const str = quasi.value.cooked || quasi.value.raw || ""; |
| 91 | + return i < arr.length - 1 ? str + EXPRESSION_PLACEHOLDER : str; |
| 92 | + }, |
| 93 | + ) |
| 94 | + .join("") |
| 95 | + .replace(/\s+/g, " ") |
| 96 | + .trim(); |
| 97 | + } else { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const startLine = node.loc?.start?.line || 0; |
| 102 | + testCallsByLine.set(startLine, { |
| 103 | + name: testName, |
| 104 | + node: path, |
| 105 | + }); |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + const sortedStartLines = Array.from(testCallsByLine.keys()).sort( |
| 110 | + (a, b) => a - b, |
| 111 | + ); |
| 112 | + |
| 113 | + for (let i = 0; i < sortedStartLines.length; i++) { |
| 114 | + const currentLine = sortedStartLines[i]; |
| 115 | + const nextLine = sortedStartLines[i + 1] || Number.MAX_SAFE_INTEGER; |
| 116 | + const { name, node } = testCallsByLine.get(currentLine)!; |
| 117 | + |
| 118 | + let path = node; |
| 119 | + let endLine = path.node.loc?.end?.line || 0; |
| 120 | + |
| 121 | + let currentPath: NodePath<t.Node> = path; |
| 122 | + while ( |
| 123 | + currentPath.parentPath && |
| 124 | + (currentPath.parentPath.node.loc?.end?.line ?? 0) < nextLine |
| 125 | + ) { |
| 126 | + const parentType = currentPath.parentPath.node.type; |
| 127 | + |
| 128 | + if (parentType === "ExpressionStatement") { |
| 129 | + endLine = currentPath.parentPath.node.loc?.end?.line || endLine; |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + if ( |
| 134 | + parentType === "CallExpression" || |
| 135 | + parentType === "MemberExpression" |
| 136 | + ) { |
| 137 | + currentPath = currentPath.parentPath; |
| 138 | + endLine = Math.max(endLine, currentPath.node.loc?.end?.line || 0); |
| 139 | + } else { |
| 140 | + endLine = Math.max( |
| 141 | + endLine, |
| 142 | + currentPath.parentPath.node.loc?.end?.line || endLine, |
| 143 | + ); |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + endLine = Math.min(endLine, nextLine - 1); |
| 148 | + |
| 149 | + const testLocation = TestLocationSchema.parse({ |
| 150 | + testName: name, |
| 151 | + startLine: currentLine, |
| 152 | + endLine, |
| 153 | + }); |
| 154 | + testLocations.push(testLocation); |
| 155 | + } |
| 156 | + |
| 157 | + log.trace("Test locations", { filePath, testLocations }); |
| 158 | + |
| 159 | + return TestLocationsSchema.parse(testLocations); |
| 160 | + } finally { |
| 161 | + log.resetGroup(); |
| 162 | + } |
| 163 | +}; |
0 commit comments