dprint-plugin-typescript icon indicating copy to clipboard operation
dprint-plugin-typescript copied to clipboard

Avoid de-denting comments that align

Open jakebailey opened this issue 3 years ago • 0 comments

In TS we have enums with comments aligned like so:

export const enum TypeFacts {
    None = 0,
    TypeofEQString = 1 << 0,      // typeof x === "string"
    TypeofEQNumber = 1 << 1,      // typeof x === "number"
    TypeofEQBigInt = 1 << 2,      // typeof x === "bigint"
    TypeofEQBoolean = 1 << 3,     // typeof x === "boolean"
    TypeofEQSymbol = 1 << 4,      // typeof x === "symbol"
    TypeofEQObject = 1 << 5,      // typeof x === "object"
    TypeofEQFunction = 1 << 6,    // typeof x === "function"
    TypeofEQHostObject = 1 << 7,  // typeof x === "xxx"
    TypeofNEString = 1 << 8,      // typeof x !== "string"
    TypeofNENumber = 1 << 9,      // typeof x !== "number"
    TypeofNEBigInt = 1 << 10,     // typeof x !== "bigint"
    TypeofNEBoolean = 1 << 11,    // typeof x !== "boolean"
    TypeofNESymbol = 1 << 12,     // typeof x !== "symbol"
    TypeofNEObject = 1 << 13,     // typeof x !== "object"
    TypeofNEFunction = 1 << 14,   // typeof x !== "function"
    TypeofNEHostObject = 1 << 15, // typeof x !== "xxx"
    EQUndefined = 1 << 16,        // x === undefined
    EQNull = 1 << 17,             // x === null
    EQUndefinedOrNull = 1 << 18,  // x === undefined / x === null
}

But, dprint reformats this like:

export const enum TypeFacts {
    None = 0,
    TypeofEQString = 1 << 0, // typeof x === "string"
    TypeofEQNumber = 1 << 1, // typeof x === "number"
    TypeofEQBigInt = 1 << 2, // typeof x === "bigint"
    TypeofEQBoolean = 1 << 3, // typeof x === "boolean"
    TypeofEQSymbol = 1 << 4, // typeof x === "symbol"
    TypeofEQObject = 1 << 5, // typeof x === "object"
    TypeofEQFunction = 1 << 6, // typeof x === "function"
    TypeofEQHostObject = 1 << 7, // typeof x === "xxx"
    TypeofNEString = 1 << 8, // typeof x !== "string"
    TypeofNENumber = 1 << 9, // typeof x !== "number"
    TypeofNEBigInt = 1 << 10, // typeof x !== "bigint"
    TypeofNEBoolean = 1 << 11, // typeof x !== "boolean"
    TypeofNESymbol = 1 << 12, // typeof x !== "symbol"
    TypeofNEObject = 1 << 13, // typeof x !== "object"
    TypeofNEFunction = 1 << 14, // typeof x !== "function"
    TypeofNEHostObject = 1 << 15, // typeof x !== "xxx"
    EQUndefined = 1 << 16, // x === undefined
    EQNull = 1 << 17, // x === null
    EQUndefinedOrNull = 1 << 18, // x === undefined / x === null
}

Maybe it makes more sense for these comments to be JSDoc or something, but I think a lot of these comments are intended to be internal and not visible in d.ts files. I'd have to find more examples where it matters, though.

Likely, an ignore comment could be used when it matters to keep formatting (we have some cases where it matters), but this is visually pleasing. As a comparison, go fmt will format aligned comments in situations similar to the above, so it's not unprecedented overall, but prettier does format things like dprint currently does.

jakebailey avatar Sep 04 '22 21:09 jakebailey