Files
orca/config/scripts/ast-grep-gates.mjs
T
OrcaWinandOrcaWin 819d273e27 perf(lint): one ast-grep scan feeds both the lint gate and the flaky-test ratchet (#33903)
lint:ast-grep and check-flaky-test-pattern-ratchet each parsed the whole tree.
ast-grep-gates.mjs scans once; the lint gate re-runs ast-grep only on files with
warning-or-error findings (ast-grep's own report and exit code), and the flaky
ratchet counts test-* matches from the shared scan.

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-26 03:50:42 -07:00

67 lines
2.1 KiB
JavaScript

// One ast-grep scan for both gates: the lint gate (warning and up) and the flaky-test ratchet (`test-*` rules).
import { spawnSync } from 'node:child_process'
import process from 'node:process'
import { pathToFileURL } from 'node:url'
import {
AST_GREP_CONFIG,
AST_GREP_SCAN_PATHS,
resolveAstGrepBinary,
scanAstGrep
} from './ast-grep-scan.mjs'
import { main as checkFlakyTestPatterns } from './check-flaky-test-pattern-ratchet.mjs'
const LINT_SEVERITIES = new Set(['error', 'warning'])
// Windows caps a command line near 32K characters; past this, rescan the whole tree.
const MAX_PATH_ARGUMENT_CHARS = 24_000
export function lintFindingFiles(matches) {
return [
...new Set(
matches
.filter((match) => LINT_SEVERITIES.has(match.severity))
.map((match) => match.file.split('\\').join('/'))
)
].sort()
}
// Rules match file by file, so rescanning only the reported files gives ast-grep's own report and exit status.
function reportLintFindings(root, files, format) {
const paths =
files.reduce((total, file) => total + file.length + 1, 0) <= MAX_PATH_ARGUMENT_CHARS
? files
: AST_GREP_SCAN_PATHS
const result = spawnSync(
resolveAstGrepBinary(),
[
'scan',
'--config',
AST_GREP_CONFIG,
'--min-severity',
'warning',
...(format ? ['--format', format] : []),
'--',
...paths
],
{ cwd: root, stdio: 'inherit', windowsHide: true }
)
if (result.error) {
throw result.error
}
return result.status ?? 1
}
export function main(root = process.cwd(), argv = []) {
const format = argv
.find((argument) => argument.startsWith('--format='))
?.slice('--format='.length)
const matches = scanAstGrep(root)
const files = lintFindingFiles(matches)
const lint = files.length > 0 ? reportLintFindings(root, files, format) : 0
const flaky = checkFlakyTestPatterns(root, undefined, matches)
return Math.max(lint, flaky)
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
process.exitCode = main(process.cwd(), process.argv.slice(2))
}