Pārlūkot izejas kodu

perf(scripts): parse only the files the dispatcher gate can find a violation in

`agentBindings` now walks the whole tree to reach a dynamic `import('undici')`,
which pushed the repository-wide scan past the 5s default under the coverage
lane's instrumentation.

Both violations name one of two words in source: an agent construction needs a
binding from the undici module, and the option is a property called
`dispatcher`. Skipping a file that mentions neither leaves 21 of 1597 files to
parse, so the scan runs in ~60ms instead of ~500ms — well clear of the timeout
even instrumented.
Yichen Jiang 1 mēnesi atpakaļ
vecāks
revīzija
4623c68e70

+ 11 - 0
scripts/verify-no-bare-dispatcher.spec.ts

@@ -126,6 +126,17 @@ describe('bare dispatcher check', () => {
     expect(reasons("import { Agent } from 'undici'\nconst agent = new Agent({})", DISPATCHER_OWNER.replaceAll('/', '\\') + 'src\\install.ts')).toEqual([])
   })
 
+  it('parses only a file naming undici or the dispatcher option', () => {
+    // The pre-filter that keeps this gate from parsing 1576 of 1597 repository files excludes a
+    // file mentioning neither word. Both violations require one of them in source, so nothing
+    // detectable is excluded — the second case proves a violating shape survives the filter.
+    expect(reasons('      const agent = new Agent({ keepAlive: true })')).toEqual([])
+    expect(reasons(`
+      import { Agent } from 'undici'
+      const agent = new Agent({})
+    `)).toEqual(['constructs an undici agent'])
+  })
+
   it('passes on the current tree', () => {
     expect(scanRepository()).toEqual([])
   })

+ 4 - 0
scripts/verify-no-bare-dispatcher.ts

@@ -160,6 +160,10 @@ function suppliesDispatcher(member: ts.ObjectLiteralElementLike): boolean {
 export function findDispatcherViolations(file: string, sourceText: string): DispatcherViolation[] {
   const posix = file.replaceAll('\\', '/')
   if (posix.startsWith(DISPATCHER_OWNER)) return []
+  // Both violations name one of these two words in source: an agent construction needs a binding
+  // from the undici module, and the option is a property called `dispatcher`. Parsing the rest of
+  // the repository anyway made this the slowest gate — 21 of 1597 files survive the filter.
+  if (!sourceText.includes(AGENT_MODULE) && !sourceText.includes(DISPATCHER_PROPERTY)) return []
   const source = ts.createSourceFile(posix, sourceText, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
   const bound = agentBindings(source)
   const lines = sourceText.split('\n')