import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; import { runAnalyze } from '../commands/analyze'; import { runLint } from '../commands/lint'; import { DEFAULT_EXCLUDE } from '../commands/shared'; import { toAnalysisDocument, toLintDocument } from '../serializer'; import type { AnalysisDocument, FunctionAnalysis, LintDocument } from '../types'; import { captureJson } from './helpers/output'; function allFindings(doc: AnalysisDocument) { return doc.findings; } describe('JSON output capture', () => { it('isolates diagnostics when the test console routes errors through stdout', async () => { const reporter = jest.spyOn(console, 'error').mockImplementation((...args: unknown[]) => { process.stdout.write(`Jest diagnostic: ${args.join(' ')}\n`); }); try { const result = await captureJson<{ ok: boolean }>(async () => { console.error('compiler diagnostic'); process.stdout.write('{"ok":true}\n'); return 0; }); expect(result).toEqual({ code: 0, doc: { ok: true }, stderr: ['compiler diagnostic'] }); expect(reporter).not.toHaveBeenCalled(); expect(console.error).toBe(reporter); } finally { reporter.mockRestore(); } }); it('restores stdout and the diagnostic sink when the command rejects', async () => { const originalWrite = process.stdout.write; const originalError = console.error; await expect( captureJson(async () => { throw new Error('command failed'); }), ).rejects.toThrow('command failed'); expect(process.stdout.write).toBe(originalWrite); expect(console.error).toBe(originalError); }); }); describe('--format json', () => { let tempDir: string; beforeEach(() => { tempDir = mkdtempSync(join(tmpdir(), 'json-format-')); mkdirSync(join(tempDir, 'src'), { recursive: true }); writeFileSync(join(tempDir, 'package.json'), JSON.stringify({ name: 'json-pkg' })); }); function argv(overrides: Record = {}) { return { paths: [tempDir], verbose: false, concurrency: 2, exclude: DEFAULT_EXCLUDE, mode: 'infer' as const, format: 'json' as const, 'strict-paths': false, 'parser-plugin': [], annotate: undefined, riskConfig: undefined, fix: false, ...overrides, }; } describe('analyze', () => { beforeEach(() => { writeFileSync( join(tempDir, 'src', 'Risky.tsx'), `import { useState } from 'react'; declare function getAppStore(): { currentId: string }; export function Risky({ label }: { label: string }) { const [n, setN] = useState(0); const id = getAppStore().currentId; return
setN(c => c + 1)}>{label}{id}{n}
; } `, ); }); it.each([ ['label.tsx', 'Label'], ['useLabel.ts', 'useLabel'], ])('reports compiled functions in %s alongside a declared class field', async (fileName, functionName) => { const filePath = join(__dirname, '__fixtures__', 'declare-fields', fileName); const { doc, code, stderr } = await captureJson(() => runAnalyze(argv({ paths: [filePath] }) as never), ); expect(code).toBe(0); expect(stderr).toEqual([]); expect(doc.summary).toMatchObject({ functions: 1, compiled: 1, reportedMemoCachePositive: 1, errors: 0, unparseableFiles: 0, }); expect(doc.functions).toEqual([ expect.objectContaining({ function: functionName, status: 'compiled', compilerEvent: 'CompileSuccess' }), ]); expect(doc.unparseable).toEqual([]); }); it.each(['reader.ts', 'reader.tsx'])( 'reports indirect risks through declared class fields in %s', async fileName => { writeFileSync( join(tempDir, 'src', fileName), readFileSync(join(__dirname, '__fixtures__', 'declare-fields', fileName), 'utf-8'), ); const filePath = join(tempDir, 'src', 'LabelViaWrapper.tsx'); writeFileSync( filePath, "import { readValue } from './reader'; export function LabelViaWrapper() { return {readValue()}; }", ); const { doc, code, stderr } = await captureJson(() => runAnalyze( argv({ paths: [filePath], riskConfig: { detectGetStateReads: true, resolveWrappers: true } }) as never, ), ); expect(code).toBe(0); expect(stderr).toEqual([expect.stringContaining('1 import(s) resolved'), expect.stringContaining('baseUrl:')]); expect(doc.summary).toMatchObject({ functions: 1, compiled: 1, errors: 0, findings: 1, findingsOnCompiled: 1, unparseableFiles: 0, }); expect(doc.findings).toEqual([ expect.objectContaining({ function: 'LabelViaWrapper', ruleId: 'nonreactive-store-read', symbol: 'store.getState', message: expect.stringContaining('reached via `readValue`'), compiled: true, }), ]); expect(doc.unparseable).toEqual([]); }, ); it('emits a versioned envelope on stdout', async () => { const { doc, code } = await captureJson(() => runAnalyze(argv({ riskConfig: { storeAccessorPattern: 'Store$' } }) as never), ); expect(code).toBe(0); expect(doc.schemaVersion).toBe(3); expect(doc.tool).toBe('react-compiler-analyzer'); expect(doc.command).toBe('analyze'); expect(doc.mode).toBe('infer'); }); it('emits findings with the requested field shape', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { storeAccessorPattern: 'Store$' } }) as never), ); const findings = allFindings(doc); expect(findings).toHaveLength(1); expect(findings[0]).toMatchObject({ file: expect.stringContaining('Risky.tsx'), line: expect.any(Number), ruleId: 'nonreactive-store-read', severity: 'medium', compiled: true, }); }); it('uses workspace-relative POSIX paths', async () => { const { doc } = await captureJson(() => runAnalyze(argv() as never)); for (const fn of doc.functions) { expect(fn.file.startsWith('/')).toBe(false); expect(fn.file).not.toContain('\\'); } }); describe('findings suppressed by a directive', () => { beforeEach(() => { writeFileSync( join(tempDir, 'src', 'Suppressed.tsx'), `declare function getAppStore(): { currentId: string }; export function Guarded() { 'use no memo'; const id = getAppStore().currentId; return
{id}
; } `, ); }); /** A load-bearing opt-out must be distinguishable from a function that has no risk at all. */ it('marks a finding whose function opted out with `use no memo`', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { storeAccessorPattern: 'Store$' } }) as never), ); const suppressed = allFindings(doc).filter(finding => finding.file.includes('Suppressed.tsx')); expect(suppressed).toHaveLength(1); expect(suppressed[0]).toMatchObject({ suppressed: true }); }); it('omits the suppressed key for a live finding', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { storeAccessorPattern: 'Store$' } }) as never), ); const live = allFindings(doc).find(finding => finding.file.includes('Risky.tsx')); expect(live).toMatchObject({ compiled: true }); expect(live?.suppressed).toBeUndefined(); }); it('counts suppressed findings separately in the summary', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { storeAccessorPattern: 'Store$' } }) as never), ); expect(doc.summary.findings).toBe(2); expect(doc.summary.findingsOnCompiled).toBe(1); expect(doc.summary.findingsSuppressed).toBe(1); }); /** A compile failure is also `compiled: false`, so the two must not be conflated. */ it('does not mark a finding that is merely uncompiled as suppressed', async () => { writeFileSync( join(tempDir, 'src', 'Bail.tsx'), `declare function getAppStore(): { currentId: string }; export function Bail() { const id = getAppStore().currentId; try { return
{id}
; } finally { console.log('x'); } } `, ); const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { storeAccessorPattern: 'Store$' } }) as never), ); const bail = allFindings(doc).find(finding => finding.file.includes('Bail.tsx')); expect(bail).toBeDefined(); expect(bail).toMatchObject({ compiled: false }); expect(bail?.suppressed).toBeUndefined(); }); /** * The cross-file resolver keys findings by function declaration while a CompileSkip locates * the function at its body, so an opted-out wrapper risk must still reach the report. */ describe('reached through the cross-file wrapper resolver', () => { beforeEach(() => { writeFileSync( join(tempDir, 'src', 'logStore.ts'), `declare function getStore(): { getState(): { logs: string[] } }; export function getAllLogs() { return getStore().getState().logs; } `, ); writeFileSync( join(tempDir, 'src', 'Wrapped.tsx'), `import { getAllLogs } from './logStore'; export function GuardedWrapper() { 'use no memo'; const logs = getAllLogs(); return
{logs.length}
; } export function LiveWrapper() { const logs = getAllLogs(); return
{logs.length}
; } `, ); }); it('reports a wrapper-resolved risk inside an opted-out function', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { detectGetStateReads: true, resolveWrappers: true } }) as never), ); const guarded = allFindings(doc).filter(f => f.file.includes('Wrapped.tsx') && f.suppressed); expect(guarded).toHaveLength(1); expect(guarded[0].message).toContain('reached via'); }); it('still reports the equivalent risk when no directive is present', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { detectGetStateReads: true, resolveWrappers: true } }) as never), ); const live = allFindings(doc).filter(f => f.file.includes('Wrapped.tsx') && f.compiled && !f.suppressed); expect(live).toHaveLength(1); }); it('counts the suppressed wrapper finding in the summary', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ riskConfig: { detectGetStateReads: true, resolveWrappers: true } }) as never), ); expect(doc.summary.findingsSuppressed).toBe(1); }); }); }); it('produces byte-identical output across runs', async () => { const first = await captureJson(() => runAnalyze(argv() as never)); const second = await captureJson(() => runAnalyze(argv() as never)); expect(JSON.stringify(first.doc)).toBe(JSON.stringify(second.doc)); }); it('produces the same document at concurrency 1 and 8', async () => { const serial = await captureJson(() => runAnalyze(argv({ concurrency: 1 }) as never)); const parallel = await captureJson(() => runAnalyze(argv({ concurrency: 8 }) as never)); expect(parallel.doc).toEqual(serial.doc); }); it('routes scan diagnostics to stderr so stdout stays parseable', async () => { const { stderr } = await captureJson(() => runAnalyze(argv({ verbose: true }) as never)); // Parsing in captureJson already proves stdout was pure JSON. expect(stderr.some(line => line.includes('Analyzing:'))).toBe(true); }); it('reports files the parser rejected instead of dropping them silently', async () => { writeFileSync(join(tempDir, 'src', 'Broken.tsx'), 'export function Nope( { return
;\n'); const { doc } = await captureJson(() => runAnalyze(argv() as never)); expect(doc.summary.unparseableFiles).toBe(1); expect(doc.unparseable[0].file).toContain('Broken.tsx'); expect(doc.unparseable[0].error).toBeTruthy(); }); describe('with --annotate', () => { it('still writes directives to disk', async () => { const before = readFileSync(join(tempDir, 'src', 'Risky.tsx'), 'utf-8'); expect(before).not.toContain('use memo'); await captureJson(() => runAnalyze(argv({ annotate: 'all' }) as never)); expect(readFileSync(join(tempDir, 'src', 'Risky.tsx'), 'utf-8')).toContain("'use memo'"); }); it('reports the annotation outcome in the document', async () => { const { doc } = await captureJson(() => runAnalyze(argv({ annotate: 'all' }) as never)); expect(doc.annotate).toMatchObject({ mode: 'all', functionsAnnotated: expect.any(Number), filesModified: expect.any(Number), }); expect(doc.annotate!.functionsAnnotated).toBeGreaterThan(0); }); it('omits the annotate key when --annotate was not requested', async () => { const { doc } = await captureJson(() => runAnalyze(argv() as never)); expect(doc.annotate).toBeUndefined(); }); }); }); describe('lint', () => { it('emits directives and keeps the failing exit code', async () => { writeFileSync( join(tempDir, 'src', 'Redundant.tsx'), `import { useRef } from 'react'; export function useUncompilable() { 'use no memo'; const ref = useRef(null); ref.current = 42; return ref; } `, ); const { doc, code } = await captureJson(() => runLint(argv() as never)); expect(code).toBe(1); expect(doc.command).toBe('lint'); expect(doc.summary.redundant).toBe(1); expect(doc.directives[0]).toMatchObject({ directive: 'use-no-memo', status: 'redundant' }); }); it('applies --fix before emitting JSON', async () => { const filePath = join(tempDir, 'src', 'Fixable.tsx'); writeFileSync( filePath, `import { useRef } from 'react'; export function useUncompilable() { 'use no memo'; const ref = useRef(null); ref.current = 42; return ref; } `, ); const { doc, code } = await captureJson(() => runLint(argv({ fix: true }) as never)); expect(readFileSync(filePath, 'utf-8')).not.toContain('use no memo'); expect(code).toBe(0); expect(doc.summary).toMatchObject({ directives: 0, redundant: 0, unparseableFiles: 0 }); expect(doc.directives).toEqual([]); expect(doc.unparseable).toEqual([]); }); it('reports failures that remain after --fix, not the original conflicts', async () => { writeFileSync( join(tempDir, 'src', 'Conflicting.tsx'), `import { useRef } from 'react'; export function useUncompilable() { 'use memo'; 'use no memo'; const ref = useRef(null); ref.current = 42; return ref; } `, ); const { doc, code } = await captureJson(() => runLint(argv({ fix: true }) as never)); expect(code).toBe(1); expect(doc.summary).toMatchObject({ directives: 1, conflicting: 0, broken: 1 }); expect(doc.directives).toMatchObject([{ directive: 'use-memo', status: 'broken' }]); }); it.each([false, true])('reports a selected file with a parse failure and fails (fix: %s)', async fix => { const filePath = join(tempDir, 'src', 'Unparseable.tsx'); writeFileSync(filePath, "export function Broken() {\n 'use no memo';\n const = ;\n}\n"); const { doc, code } = await captureJson(() => runLint(argv({ fix }) as never)); expect(code).toBe(1); expect(doc.summary).toMatchObject({ directives: 0, unparseableFiles: 1 }); expect(doc.unparseable).toEqual([ { file: expect.stringContaining('Unparseable.tsx'), error: expect.any(String) }, ]); expect(doc.unparseable[0].error).toBeTruthy(); expect(readFileSync(filePath, 'utf-8')).toContain("'use no memo'"); }); }); }); describe('serializers', () => { const base: FunctionAnalysis = { filePath: '/ws/src/A.tsx', packageName: 'pkg', line: 3, column: 0, functionName: 'A', status: 'compiled', compilerEvent: 'CompileSuccess', }; it('marks findings on non-compiled functions as latent', () => { const results: FunctionAnalysis[] = [ { ...base, status: 'error', compilerEvent: 'CompileError', risks: [ { ruleId: 'nonreactive-store-read', severity: 'high', line: 4, column: 2, symbol: 'getAppStore', message: 'x', }, ], }, ]; const doc = toAnalysisDocument(results, { mode: 'infer', workspaceRoot: '/ws' }); expect(allFindings(doc)[0].compiled).toBe(false); expect(doc.summary.findings).toBe(1); expect(doc.summary.findingsOnCompiled).toBe(0); }); it('uses explicit nulls for unknown analysis fields', () => { const doc = toAnalysisDocument([{ ...base, memoStats: null }], { mode: 'infer', workspaceRoot: '/ws', }); expect(doc.functions[0].memoStats).toBeNull(); expect(allFindings(doc)).toHaveLength(0); }); it('summarizes accepted functions with positive reported memo statistics', () => { const doc = toAnalysisDocument( [ { ...base, functionName: 'Cached', memoStats: { memoSlots: 2, memoBlocks: 1, memoValues: 1, prunedMemoBlocks: 0, prunedMemoValues: 0 }, }, { ...base, functionName: 'Pruned', memoStats: { memoSlots: 0, memoBlocks: 0, memoValues: 0, prunedMemoBlocks: 1, prunedMemoValues: 1 }, }, { ...base, functionName: 'Unknown', memoStats: null, }, { ...base, functionName: 'ErroredWithStats', status: 'error', compilerEvent: 'CompileError', memoStats: { memoSlots: 3, memoBlocks: 1, memoValues: 2, prunedMemoBlocks: 0, prunedMemoValues: 0 }, }, ], { mode: 'infer', workspaceRoot: '/ws' }, ); expect(doc.summary.compiled).toBe(3); expect(doc.summary.reportedMemoCachePositive).toBe(1); }); it.each(['/ws/src/Broken.tsx', 'C:\\repo\\src\\Broken.tsx'])('removes the known diagnostic filename: %s', file => { const error = 'Expected closing tag for
. (1:34)'; const meta = { mode: 'infer' as const, workspaceRoot: '/ws', unparseable: [{ file, error: `${file}: ${error}` }] }; expect(toAnalysisDocument([], meta).unparseable[0].error).toBe(error); expect(toLintDocument([], meta).unparseable[0].error).toBe(error); const otherError = `Unexpected token while reading ${file}: nested diagnostic`; const otherMeta = { ...meta, unparseable: [{ file, error: otherError }] }; expect(toAnalysisDocument([], otherMeta).unparseable[0].error).toBe(otherError); expect(toLintDocument([], otherMeta).unparseable[0].error).toBe(otherError); }); it('summarizes lint directives by status', () => { const doc: LintDocument = toLintDocument( [ { filePath: '/ws/src/A.tsx', packageName: 'pkg', line: 2, functionName: 'A', status: 'broken', compilerEvent: 'CompileError', directiveType: 'use-memo', }, ], { mode: 'annotation', workspaceRoot: '/ws' }, ); expect(doc.mode).toBe('annotation'); expect(doc.summary).toMatchObject({ directives: 1, broken: 1, redundant: 0 }); expect(doc.directives[0].file).toBe('src/A.tsx'); }); });