Files
orca/config/scripts/ast-grep-scan.mjs
T
Neil 5447a26fd1 perf(lint): keep JS plugins out of default oxlint and adopt ast-grep (#33430)
* perf(lint): keep JS plugins out of default oxlint and adopt ast-grep

Any jsPlugin makes Oxlint hand every file's AST to Node, so the seven custom
plugins cost the default `oxlint` run several times its native time.

- Move app-store-performance, quadratic-buffer-concat, renderer-scrollbar-style
  and ime-enter-guard (scope- or dataflow-aware) to config/oxlint-js-plugins.json,
  run by `pnpm lint:js-plugins`, CI, lint-staged and the changed-lines gate.
- Port sort-comparator-performance, mobile-pairing and native-chat-design-tokens
  to ast-grep rules with rule tests, and delete the plugins.
- Add @ast-grep/cli, config/sgconfig.yml, `pnpm lint:ast-grep` / `test:ast-grep`,
  a CI step, and an ast-grep pass in check-changed-code-quality.
- Add flaky-test rules (wall-clock reads, real-time waits, polling loops,
  unquarantined skips, unexplained timeout bumps), baselined by a count ratchet.

* Enforce flaky-test ratchet on changed files; refresh baseline

* docs(agents): keep custom-rule details out of AGENTS.md

* Resolve ast-grep from the repo so gate scans of other roots work
2026-10-25 00:23:38 -07:00

51 lines
1.9 KiB
JavaScript

import { spawnSync } from 'node:child_process'
import { createRequire } from 'node:module'
import process from 'node:process'
export const AST_GREP_CONFIG = 'config/sgconfig.yml'
export const AST_GREP_SCAN_PATHS = ['src', 'config', 'tests', 'mobile']
// Why the platform binary: the package's `ast-grep` bin is either a JS launcher or (after its
// postinstall) the native binary, and on Windows a .cmd shim Node refuses to spawn without a shell.
// Resolved from this script, not the scanned root, so scans of temp or nested roots still find it.
export function resolveAstGrepBinary() {
const binary = createRequire(import.meta.url)('@ast-grep/cli/postinstall.js').resolveBinaryPath()
if (!binary) {
throw new Error('No @ast-grep/cli binary for this platform; run pnpm install.')
}
return binary
}
/** Runs `ast-grep scan` and returns its JSON matches; exit status 1 only means error findings. */
export function scanAstGrep(root, { filter, minSeverity, paths = AST_GREP_SCAN_PATHS } = {}) {
const args = ['scan', '--config', AST_GREP_CONFIG, '--json=stream', '--color', 'never']
if (filter) {
args.push('--filter', filter)
}
if (minSeverity) {
args.push('--min-severity', minSeverity)
}
const result = spawnSync(resolveAstGrepBinary(), [...args, '--', ...paths], {
cwd: root,
encoding: 'utf8',
maxBuffer: 256 * 1024 * 1024,
windowsHide: true
})
if (result.error) {
throw result.error
}
if (result.status !== 0 && result.status !== 1) {
process.stderr.write(result.stderr)
throw new Error(`ast-grep scan failed with exit code ${result.status}.`)
}
return result.stdout
.split('\n')
.filter((line) => line.trim())
.map((line) => JSON.parse(line))
}
/** Per-language twins share one rule id with a `-tsx` suffix; report them as one rule. */
export function baseRuleId(ruleId) {
return ruleId.replace(/-tsx$/, '')
}