Skip to content

Commit

Permalink
fix: Improve parsing of message details in VS Code problems matcher
Browse files Browse the repository at this point in the history
Printing message details in a separate line causes the VS Code "Problems"
integration to only display one of the lines, as both point to the same
line/column in the code.

In addition, replacing multiple spaces solves incorrect
message parsing with the $eslint-stylish matcher which expects a "code"
after two spaces. Currently no code is printed at all.
  • Loading branch information
matz3 committed Jun 3, 2024
1 parent 7084704 commit ea8e258
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/formatter/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import chalk from "chalk";
import path from "node:path";
import {LintMessageSeverity, LintResult, LintMessage} from "../linter/LinterContext.js";

const detailsHeader = chalk.white.bold("Details:");

function formatSeverity(severity: LintMessageSeverity) {
if (severity === LintMessageSeverity.Error) {
return chalk.red("error");
Expand All @@ -20,6 +22,17 @@ function formatLocation(line: LintMessage["line"], column: LintMessage["column"]
return chalk.dim(`${lineStr}:${columnStr}`);
}

function formatMessageDetails(msg: LintMessage, showDetails: boolean) {
if (!showDetails || !msg.messageDetails) {
return "";
}
// Ensure that details are not containing line breaks as every message should be just a single line.
// In addition, some integrations understand two whitespace chars (e.g. two spaces) as a separator
// for a message code (e.g. $eslint-stylish problems matcher in VS Code).
// Therefore, two or more whitespace chars are reduced to a single space.
return `. ${detailsHeader} ${chalk.italic(msg.messageDetails.replace(/\s\s+|\n/g, " "))}`;
}

export class Text {
#buffer = "";

Expand Down Expand Up @@ -72,16 +85,11 @@ export class Text {
const formattedLocation =
formatLocation(msg.line, msg.column, lineInfoLength, columnInfoLength);

const messageDetails = (showDetails && msg.messageDetails) ?
(`\n ${formattedLocation} ${chalk.white.bold("Details:")} ` +
`${chalk.italic(msg.messageDetails.replaceAll("\n", " "))}`) :
"";

this.#writeln(
` ${formattedLocation} ` +
`${formatSeverity(msg.severity)} ` +
`${msg.message}` +
`${messageDetails}`);
`${formatMessageDetails(msg, showDetails)}`);
});

this.#writeln("");
Expand Down

0 comments on commit ea8e258

Please sign in to comment.