|
| 1 | +import { createHandler, BaseHandlerOptions } from '../shared/utils/handler-helpers.js'; |
| 2 | +import { |
| 3 | + createProjectAnalysisSchema, |
| 4 | + COMMON_ANNOTATIONS, |
| 5 | +} from '../shared/models/schema-helpers.js'; |
| 6 | +import { |
| 7 | + analyzeProjectCoverage, |
| 8 | +} from '../shared/violation-analysis/coverage-analyzer.js'; |
| 9 | +import { formatViolations } from '../shared/violation-analysis/formatters.js'; |
| 10 | +import { loadAndValidateDsComponentsFile } from '../../../validation/ds-components-file-loader.validation.js'; |
| 11 | + |
| 12 | +interface ReportAllViolationsOptions extends BaseHandlerOptions { |
| 13 | + directory: string; |
| 14 | + groupBy?: 'file' | 'folder'; |
| 15 | +} |
| 16 | + |
| 17 | +export const reportAllViolationsSchema = { |
| 18 | + name: 'report-all-violations', |
| 19 | + description: |
| 20 | + 'Report all deprecated DS CSS usage for every component defined in the config file within a directory.', |
| 21 | + inputSchema: createProjectAnalysisSchema({ |
| 22 | + groupBy: { |
| 23 | + type: 'string', |
| 24 | + enum: ['file', 'folder'], |
| 25 | + description: 'How to group the results', |
| 26 | + default: 'file', |
| 27 | + }, |
| 28 | + }), |
| 29 | + annotations: { |
| 30 | + title: 'Report All Violations', |
| 31 | + ...COMMON_ANNOTATIONS.readOnly, |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | + |
| 36 | +// Reuse centralized loader+validator for DS components config |
| 37 | + |
| 38 | +export const reportAllViolationsHandler = createHandler< |
| 39 | + ReportAllViolationsOptions, |
| 40 | + string[] |
| 41 | +>( |
| 42 | + reportAllViolationsSchema.name, |
| 43 | + async (params, { cwd, deprecatedCssClassesPath }) => { |
| 44 | + const groupBy = params.groupBy || 'file'; |
| 45 | + const dsComponents = await loadAndValidateDsComponentsFile( |
| 46 | + cwd, |
| 47 | + deprecatedCssClassesPath || '', |
| 48 | + ); |
| 49 | + |
| 50 | + const coverageResult = await analyzeProjectCoverage({ |
| 51 | + cwd, |
| 52 | + returnRawData: true, |
| 53 | + directory: params.directory, |
| 54 | + dsComponents, |
| 55 | + }); |
| 56 | + |
| 57 | + const raw = coverageResult.rawData?.rawPluginResult; |
| 58 | + if (!raw) return []; |
| 59 | + |
| 60 | + // Reuse the existing minimal formatter so output format matches report-violations |
| 61 | + // and respect groupBy option. |
| 62 | + const formattedContent = formatViolations(raw, params.directory, { |
| 63 | + groupBy: groupBy === 'file' ? 'file' : 'folder', |
| 64 | + }); |
| 65 | + |
| 66 | + // Extract text items to string[] |
| 67 | + const lines = formattedContent.map((item: { type?: string; text?: string } | string) => { |
| 68 | + if (typeof item === 'string') return item; |
| 69 | + if (item && typeof item.text === 'string') return item.text; |
| 70 | + return String(item); |
| 71 | + }); |
| 72 | + |
| 73 | + return lines; |
| 74 | + }, |
| 75 | + (result) => result, |
| 76 | +); |
| 77 | + |
| 78 | +export const reportAllViolationsTools = [ |
| 79 | + { |
| 80 | + schema: reportAllViolationsSchema, |
| 81 | + handler: reportAllViolationsHandler, |
| 82 | + }, |
| 83 | +]; |
0 commit comments