deno_doc icon indicating copy to clipboard operation
deno_doc copied to clipboard

ANSI colors included in the `returnType` field

Open nberlette opened this issue 11 months ago • 0 comments

I've been encountering some pretty strange behavior with Deno Doc where generated function doc nodes contain ANSI color sequences like \u001b[38;5;12m in their returnType fields. I've confirmed this to be occurring in both the WebAssembly build and the Deno CLI version in --json mode, as recently as version 0.172.0 / Deno v2.3.1+5044f2f.

import { doc } from "jsr:@deno/[email protected]";

const specifiers = [
  "jsr:@nick/[email protected]/both",
  "jsr:@nick/[email protected]/either",
  "jsr:@nick/[email protected]/constructor",
];

const result = await doc(specifiers);

console.log(result[specifiers[0]][1].functionDef.returnType);

When I run the above code I get the following result. Notice line 7.

{
  repr: "",
  kind: "fnOrConstructor",
  fnOrConstructor: {
    constructor: false,
    tsType: {
      repr: "it is \x1b[0m\x1b[38;5;12mL\x1b[0m & \x1b[0m\x1b[38;5;12mR\x1b[0m",
      kind: "typePredicate",
      typePredicate: {
        asserts: false,
        param: { type: "identifier", name: "it" },
        type: { repr: "", kind: "intersection", intersection: [Array] }
      }
    },
    params: [
      {
        kind: "identifier",
        name: "it",
        optional: false,
        tsType: { repr: "unknown", kind: "keyword", keyword: "unknown" }
      }
    ],
    typeParams: []
  }
}

At first I thought it was only occuring on the isBoth and isEither guards, but I have just confirmed it is happening with other ones as well. The /constructor module, third in the specifiers array in the above example snippet, generates this doc node structure:

{
  repr: "it is \x1b[0m\x1b[38;5;12mConstructor\x1b[0m<\x1b[0m\x1b[38;5;12mT\x1b[0m>",
  kind: "typePredicate",
  typePredicate: {
    asserts: false,
    param: { type: "identifier", name: "it" },
    type: {
      repr: "Constructor",
      kind: "typeRef",
      typeRef: { typeParams: [ [Object] ], typeName: "Constructor" }
    }
  }
}

edit: it's also occurring when the return type is a simple keyword type like string:

// ...
  repr: "it is \x1b[0m\x1b[36mstring\x1b[0m",
// ...

It appears as though this might be some sort of crossover between the CLI's colorization logic and the JSON output formatter...? I would've figured the logic for pretty-printing docs to the terminal would be omitted from the WebAssembly build, but I can't come up with any alternative hypotheses as to what could be going on here.

I'll poke around in the source code when I have some time and see if I can figure out the culprit. I'm not as familiar with this codebase as I am with e.g. deno_lint, so any guidance or pointing in the right direction would definitely be appreciated 😅

Also, if this issue would be better off in the main denoland/deno issue tracker, please let me know and I will transfer it over there.

Thanks for your time!

nberlette avatar May 13 '25 17:05 nberlette