From 2e408545f673017d67fd0186fdd89409ad9d1d59 Mon Sep 17 00:00:00 2001 From: luojiyin Date: Fri, 10 Jul 2026 17:47:29 +0800 Subject: [PATCH] fix(get-report-data): use real fixable counts from core (A1) --- __tests__/get-report-data.spec.ts | 107 ++++++++++++++++++++++++++++++ src/lint-md.ts | 7 +- src/types.ts | 2 + src/utils/get-report-data.ts | 4 +- src/utils/lint-worker.ts | 2 + 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 __tests__/get-report-data.spec.ts diff --git a/__tests__/get-report-data.spec.ts b/__tests__/get-report-data.spec.ts new file mode 100644 index 0000000..bd79300 --- /dev/null +++ b/__tests__/get-report-data.spec.ts @@ -0,0 +1,107 @@ +import type { BatchLintItem } from '../src/types'; +import type { LintReportItem } from '@lint-md/core'; +import { getReportData } from '../src/utils/get-report-data'; + +const makeReportItem = ( + severity: number, + overrides: Partial = {} +): LintReportItem => ({ + name: 'rule-x', + message: 'some problem', + content: 'x', + severity, + loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 2 } }, + ...overrides, +}); + +const makeItem = (overrides: Partial = {}): BatchLintItem => ({ + path: 'doc.md', + lintResult: [], + ...overrides, +}); + +describe('getReportData', () => { + test('counts errors (severity 2) and warnings (severity 1) separately', () => { + const result = getReportData([ + makeItem({ + lintResult: [ + makeReportItem(2), + makeReportItem(2), + makeReportItem(1), + ], + fixableErrorCount: 0, + fixableWarningCount: 0, + }), + ]); + + expect(result.errorCount).toBe(2); + expect(result.warningCount).toBe(1); + }); + + test('aggregates error/warning counts across multiple files', () => { + const result = getReportData([ + makeItem({ + path: 'a.md', + lintResult: [makeReportItem(2), makeReportItem(1)], + }), + makeItem({ + path: 'b.md', + lintResult: [makeReportItem(2), makeReportItem(2), makeReportItem(1)], + }), + ]); + + expect(result.errorCount).toBe(3); + expect(result.warningCount).toBe(2); + }); + + test('shows the fixable summary when fixable counts are present', () => { + const { consoleMessage } = getReportData([ + makeItem({ + lintResult: [makeReportItem(2), makeReportItem(1)], + fixableErrorCount: 2, + fixableWarningCount: 1, + }), + ]); + + expect(consoleMessage).toContain('2 errors and 1 warning potentially fixable with the `--fix` option.'); + }); + + test('omits the fixable summary when fixable counts are zero', () => { + const { consoleMessage } = getReportData([ + makeItem({ + lintResult: [makeReportItem(2), makeReportItem(1)], + fixableErrorCount: 0, + fixableWarningCount: 0, + }), + ]); + + expect(consoleMessage).not.toContain('potentially fixable with the `--fix` option.'); + }); + + test('propagates real fixable counts through aggregation', () => { + const { consoleMessage } = getReportData([ + makeItem({ + lintResult: [makeReportItem(2)], + fixableErrorCount: 3, + fixableWarningCount: 0, + }), + makeItem({ + lintResult: [makeReportItem(1)], + fixableErrorCount: 1, + fixableWarningCount: 4, + }), + ]); + + expect(consoleMessage).toContain('4 errors and 4 warnings potentially fixable with the `--fix` option.'); + }); + + test('drops files with no problems from the report', () => { + const { errorCount, warningCount, consoleMessage } = getReportData([ + makeItem({ lintResult: [], fixableErrorCount: 0, fixableWarningCount: 0 }), + ]); + + expect(errorCount).toBe(0); + expect(warningCount).toBe(0); + expect(consoleMessage).toBe(''); + }); +}); diff --git a/src/lint-md.ts b/src/lint-md.ts index b07bc67..125784f 100644 --- a/src/lint-md.ts +++ b/src/lint-md.ts @@ -103,7 +103,12 @@ program try { const result = lintMarkdown(content, rules, false); const { consoleMessage, errorCount, warningCount } - = getReportData([{ path: '(stdin)', lintResult: result.lintResult }]); + = getReportData([{ + path: '(stdin)', + lintResult: result.lintResult, + fixableErrorCount: result.fixableErrorCount, + fixableWarningCount: result.fixableWarningCount, + }]); console.log(consoleMessage); diff --git a/src/types.ts b/src/types.ts index 51387be..e650ea2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -44,4 +44,6 @@ export interface BatchLintItem { path: string lintResult: LintReportItem[] fixedResult?: FixedResult | null + fixableErrorCount?: number + fixableWarningCount?: number } diff --git a/src/utils/get-report-data.ts b/src/utils/get-report-data.ts index b11fc72..07de4ed 100644 --- a/src/utils/get-report-data.ts +++ b/src/utils/get-report-data.ts @@ -44,8 +44,8 @@ export const getReportData = (problemResult: BatchLintItem[]) => { return { errorCount, filePath: path, - fixableErrorCount: 0, - fixableWarningCount: 0, + fixableErrorCount: res.fixableErrorCount ?? 0, + fixableWarningCount: res.fixableWarningCount ?? 0, messages: lintResult.map((lintItem) => { const { loc, message, severity, name } = lintItem; return { diff --git a/src/utils/lint-worker.ts b/src/utils/lint-worker.ts index ce0516b..e94160b 100644 --- a/src/utils/lint-worker.ts +++ b/src/utils/lint-worker.ts @@ -26,6 +26,8 @@ const lintWorker = async (options: LintWorkerOptions) => { path: filePath, lintResult: result.lintResult, fixedResult: result.fixedResult, + fixableErrorCount: result.fixableErrorCount, + fixableWarningCount: result.fixableWarningCount, }; };