Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/lit-analyzer/src/lib/cli/analyze-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CodeDiagnosticFormatter } from "./format/code-diagnostic-formatter.js";
import { AnalysisStats, DiagnosticFormatter } from "./format/diagnostic-formatter.js";
import { ListDiagnosticFormatter } from "./format/list-diagnostic-formatter.js";
import { MarkdownDiagnosticFormatter } from "./format/markdown-formatter.js";
import { JsonDiagnosticFormatter } from "./format/json-formatter.js";
import { FormatterFormat, LitAnalyzerCliConfig } from "./lit-analyzer-cli-config.js";

function printText(text: string, config: LitAnalyzerCliConfig) {
Expand Down Expand Up @@ -153,6 +154,8 @@ function getFormatter(format: FormatterFormat): DiagnosticFormatter {
return new CodeDiagnosticFormatter();
case "markdown":
return new MarkdownDiagnosticFormatter();
case "json":
return new JsonDiagnosticFormatter();
default:
throw new Error(`Unknown format: '${format}'`);
}
Expand Down
1 change: 1 addition & 0 deletions packages/lit-analyzer/src/lib/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export async function cli(): Promise<void> {
o code Highlight problems in the code (default)
o list Short and precise list of problems
o markdown Markdown format
o json Json format (eslint-compatible)
--noColor Print results without color
--outFile FILE Emit all output to a single file
--maxWarnings NUMBER Fail only when the number of warnings is larger than this number
Expand Down
49 changes: 49 additions & 0 deletions packages/lit-analyzer/src/lib/cli/format/json-formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { SourceFile } from "typescript";
import { LitDiagnostic } from "../../analyze/types/lit-diagnostic.js";
import { AnalysisStats, DiagnosticFormatter } from "./diagnostic-formatter.js";

export class JsonDiagnosticFormatter implements DiagnosticFormatter {
protected diagnostics: { file: SourceFile; diagnostics: LitDiagnostic[] }[] = [];

report(_stats: AnalysisStats): string | undefined {
return JSON.stringify(
this.diagnostics.map(({ file, diagnostics }) => ({
filePath: file.fileName,
messages: diagnostics.map(diag => ({
ruleId: `lit-analyzer/${diag.source}`,
severity: diag.severity === "error" ? 2 : 1,
message: diag.message,
messageId: diag.code,
...this.diagnosticsFileLocation(diag)
})),
errorCount: diagnostics.filter(d => d.severity === "error").length,
// "fatalErrorCount": 0,
// "fixableErrorCount": 0,
warningCount: diagnostics.filter(d => d.severity === "warning").length,
// "fixableWarningCount": 0,
// "suppressedMessages": [],
// "usedDeprecatedRules": []
source: file.text
}))
);
}

diagnosticTextForFile(file: SourceFile, diagnostics: LitDiagnostic[]): string | undefined {
if (diagnostics.length === 0) return undefined;
this.diagnostics.push({ file, diagnostics });
return undefined;
}

protected diagnosticsFileLocation(diagnostic: LitDiagnostic) {
const fileToStart = diagnostic.file.text.slice(0, diagnostic.location.start).split("\n");
const startLine = fileToStart[fileToStart.length - 1];
const fileToEnd = diagnostic.file.text.slice(0, diagnostic.location.end).split("\n");
const endLine = fileToEnd[fileToEnd.length - 1];
return {
line: fileToStart.length,
column: startLine ? startLine.length + 1 : undefined,
endLine: fileToEnd.length,
endColumn: endLine ? endLine.length + 1 : undefined
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LitAnalyzerRules } from "../analyze/lit-analyzer-config.js";

export type FormatterFormat = "code" | "list" | "markdown";
export type FormatterFormat = "code" | "list" | "markdown" | "json";

export interface LitAnalyzerCliConfig {
debug?: boolean;
Expand Down