55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
|
|
const file = process.argv[2];
|
|
const lineNum = parseInt(process.argv[3], 10);
|
|
|
|
const content = readFileSync(file, "utf-8");
|
|
const lines = content.split("\n");
|
|
|
|
const MULTILINE_LOOKAHEAD = 5;
|
|
|
|
const escapeRegex = (raw: string) => raw.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
const CHALK_PATTERN =
|
|
/(?:describe|test(?:\.concurrent)?)\s*\(\s*`\$\{chalk\.\w+\(["'](.*?)["']\)\}`/;
|
|
const BLOCK_NAME = String.raw`(?:describe|test(?:\.concurrent)?|Eval(?:<[^>]+>)?)`;
|
|
const SIMPLE_PATTERN = new RegExp(`${BLOCK_NAME}\\s*\\(\\s*["'\`](.*?)["'\`]`);
|
|
const OPEN_PATTERN = new RegExp(`${BLOCK_NAME}\\s*\\(\\s*$`);
|
|
|
|
// Walk backwards from cursor to find enclosing describe, test, or test.concurrent.
|
|
// Each candidate also gets a multi-line lookahead so name args wrapped onto the
|
|
// next line(s) — `test.concurrent(\n\t\`${chalk.yellowBright("name")}\`,` — are matched.
|
|
for (let i = lineNum - 1; i >= 0; i--) {
|
|
const line = lines[i];
|
|
|
|
const chalkMatch = line.match(CHALK_PATTERN);
|
|
if (chalkMatch) {
|
|
console.log(escapeRegex(chalkMatch[1]));
|
|
process.exit(0);
|
|
}
|
|
|
|
const simpleMatch = line.match(SIMPLE_PATTERN);
|
|
if (simpleMatch) {
|
|
console.log(escapeRegex(simpleMatch[1]));
|
|
process.exit(0);
|
|
}
|
|
|
|
if (OPEN_PATTERN.test(line)) {
|
|
const joined = lines
|
|
.slice(i, Math.min(lines.length, i + 1 + MULTILINE_LOOKAHEAD))
|
|
.join("\n");
|
|
const chalkMulti = joined.match(CHALK_PATTERN);
|
|
if (chalkMulti) {
|
|
console.log(escapeRegex(chalkMulti[1]));
|
|
process.exit(0);
|
|
}
|
|
const simpleMulti = joined.match(SIMPLE_PATTERN);
|
|
if (simpleMulti) {
|
|
console.log(escapeRegex(simpleMulti[1]));
|
|
process.exit(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(".*"); // fallback: run all
|