Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
4.7 KB
Raw Blame
/**
 * pregate/playwright-spec — whether a JS/TS test file is a Playwright spec, the one predicate that routes a test to
 * `playwright test` in the lanes check and lets a policy's `local_skip` leave it out of the local lane (#4709).
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
import { readRepoFile } from './import-graph';

/**
 * Whether a JS/TS test imports a value from Playwright's runner in a statement that starts a line: `import … from '@playwright/test'`
 * (one line or several), a bare import, or `require`. A type-only import (`import type …`, or every specifier `type X`) and a string or
 * fixture body that mentions the import mid-line do not count.
 */
function importsPlaywright(text: string): boolean {
  const code = text.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, '');
  if (/^[\t ]*(?:import[\t ]*|(?:const|let|var)\b[^=\n]*=[\t ]*require\([\t ]*)['"]@playwright\/test['"]/m.test(code)) return true;
  for (const m of code.matchAll(/^[\t ]*import(?=[\s{*])([^'"`;]*?)\bfrom[\t ]*['"]@playwright\/test['"]/gm)) {
    const spec = m[1]!.trim();
    if (!/^type(?=[\s{*])/.test(spec) && spec.replace(/[{}]/g, '').split(',').some(s => s.trim() && !/^type\s/.test(s.trim()))) return true;
  }
  return false;
}
/** Where a file takes the value `test` from: a relative module or `@playwright/test`, by `import { test } from …` (aliased or not), `export { test } from …` or `export * from …`. */
function testSources(text: string): string[] {
  const code = text.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, '');
  return [...code.matchAll(/^[\t ]*(?:(?:import|export)[\t ]*\{([^}]*)\}|export[\t ]*\*)[\t ]*from[\t ]*['"](\.{1,2}\/[^'"]+|@playwright\/test)['"]/gm)].filter(m => m[1] === undefined || m[1].split(',').some(s => /^test(?:\s+as\s+[\w$]+)?$/.test(s.trim()))).map(m => m[2]!);
}
/**
 * Whether a JS/TS test is a Playwright spec: it imports a value from `@playwright/test`, or takes `test` from a repo module that does and doesn't take
 * `test` from a unit runner, directly or through further such modules, up to 8 deep (Playwright's fixtures pattern: `base.extend(…)`, `mergeTests(…)`).
 * Only the modules `test` comes from are followed, so a unit test whose helper or vitest fixture uses other Playwright APIs keeps its runner (#4347).
 */
export function playwrightSpec(repoRoot: string, file: string, owned = false): boolean {
  const exts = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
  const seen = new Set<string>();
  let level = [file];
  for (let depth = 0; level.length && depth <= 8; depth++) {
    const next: string[] = [];
    for (const mod of level.filter(m => !seen.has(m))) {
      seen.add(mod);
      const raw = readRepoFile(repoRoot, mod) ?? '', text = owned ? raw.replace(TEMPLATE_LITERAL, '') : raw, sources = testSources(text);
      if (depth === 0 ? (owned ? sources.includes('@playwright/test') : importsPlaywright(text)) : sources.includes('@playwright/test') || (importsPlaywright(text) && !/^[\t ]*import\b[^;]*\btest\b[^;]*\bfrom[\t ]*['"](?:vitest|@jest\/globals|bun:test|node:test)['"]/m.test(text))) return true;
      for (const base of sources.filter(s => s.startsWith('.')).map(spec => path.posix.normalize(path.posix.join(path.posix.dirname(mod), spec))).filter(b => b !== '..' && !b.startsWith('../'))) {
        const stem = base.replace(/\.[cm]?jsx?$/, '');
        const hit = [base, ...exts.map(e => stem + e), ...exts.map(e => `${base}/index${e}`)].find(c => readRepoFile(repoRoot, c) !== null);
        if (hit) next.push(hit);
      }
    }
    level = next;
  }
  return false;
}

const TEMPLATE_LITERAL = /`(?:\\[\s\S]|[^\\`])*`/g;
const UNIT_RUNNER = /^[\t ]*(?:import\b[^;'"`]*?\bfrom[\t ]*|import[\t ]*|(?:const|let|var)\b[^=\n]*=[\t ]*require\([\t ]*)['"](?:vitest|@jest\/globals|bun:test|node:test)['"]/m;
/**
 * Whether a policy's `local_skip` may leave a test out of the local lane (#4709): positive evidence that Playwright owns it, stricter than
 * playwrightSpec so an ambiguous file keeps running. A JS/TS file under a package.json whose own `test` comes from `@playwright/test`,
 * directly or through repo fixture modules, and that loads no unit runner; import-shaped text in a template literal or comment does not count.
 */
export function playwrightOwned(repoRoot: string, file: string): boolean {
  if (!/\.[cm]?[jt]sx?$/.test(file)) return false;
  const code = (readRepoFile(repoRoot, file) ?? '').replace(TEMPLATE_LITERAL, '').replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, '');
  if (UNIT_RUNNER.test(code)) return false;
  for (let d = path.posix.dirname(file); ; d = path.posix.dirname(d)) {
    if (fs.existsSync(path.join(repoRoot, d === '.' ? '' : d, 'package.json'))) return playwrightSpec(repoRoot, file, true);
    if (d === '.' || d === '/' || d === '') return false;
  }
}