Skip to content

ANSI colors in JSON report #787

Open
@rarila

Description

@rarila

If I’m right, ANSI color codes get removed here, if you e.g. supply the switch --no-colors:

$generatedReport = Common::stripColors($generatedReport);

The problem:

When you run with --report=json --no-colors the message is first JSON encoded

$messages .= json_encode($messagesObject).",";

so, that afterwards the color code removal fails, as the initial message is now encoded.

For example:

Before:   ... "\033[30;1m·\033[0m" ...
After:    ... "\u001b[30;1m\u00b7\u001b[0m\u001b[30;1m\u00b7\u001b[0m" ...

To reproduce:

It happens for example with LanguageConstructSpacingSniff (add multiple spaces somewhere) that calls

public static function prepareForOutput($content, $exclude=[])

and adds colors.

Possible solutions:

Some ideas, but I'm sure someone can come up with a better one…

  • Probably easiest: Colors could be removed before JSON encoding independant of the color setting.
    Is there any need for colors in a JSON report?!
  • Don’t even add colors in the first place when no colors are demanded
  • Remove encoded colors in JSON report (probably the worst solution)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions