Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { join } from 'path';
import type {
AllViolationsReportByFile,
AllViolationsReport,
ComponentViolationReport,
GroupViolationsOptions,
GroupViolationsReport,
GroupViolationsResult,
Expand All @@ -14,6 +15,7 @@ import { groupViolationsSchema } from './models/schema.js';
import {
detectReportFormat,
convertComponentToFileFormat,
convertSingleComponentToComponentFormat,
enrichFiles,
groupByDirectory,
determineOptimalGroups,
Expand Down Expand Up @@ -55,13 +57,18 @@ export const groupViolationsHandler = createHandler<

if (format === 'unknown') {
throw new Error(
'Invalid violations report format. Expected either { files: [...] } or { components: [...] }',
'Invalid violations report format. Expected { files: [...] }, { components: [...] }, or { component: "...", violations: [...] }',
);
}

let violationsData: AllViolationsReportByFile;

if (format === 'component') {
if (format === 'single-component') {
const asComponentFormat = convertSingleComponentToComponentFormat(
rawData as ComponentViolationReport,
);
violationsData = convertComponentToFileFormat(asComponentFormat);
} else if (format === 'component') {
violationsData = convertComponentToFileFormat(
rawData as AllViolationsReport,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AllViolationsReport,
AllViolationsReportByFile,
ComponentViolationReport,
FileViolationReport,
} from '../models/types.js';

Expand All @@ -9,16 +10,39 @@ import type {
*/
export function detectReportFormat(
data: any,
): 'file' | 'component' | 'unknown' {
): 'file' | 'component' | 'single-component' | 'unknown' {
if (data.files && Array.isArray(data.files)) {
return 'file';
}
if (data.components && Array.isArray(data.components)) {
return 'component';
}
if (data.component && data.violations && Array.isArray(data.violations)) {
return 'single-component';
}
return 'unknown';
}

/**
* Convert single-component report to multi-component format
*/
export function convertSingleComponentToComponentFormat(
report: ComponentViolationReport,
): AllViolationsReport {
return {
components: [
{
component: report.component,
violations: report.violations.map((v) => ({
...v,
replacement: report.component,
})),
},
],
rootPath: report.rootPath,
};
}

/**
* Convert component-grouped report to file-grouped format
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
export {
detectReportFormat,
convertComponentToFileFormat,
convertSingleComponentToComponentFormat,
} from './format-converter.utils.js';
export { calculateViolations, enrichFiles } from './file-enrichment.utils.js';
export {
Expand Down
Loading