Skip to content

Avoid de-denting comments that align #410

Open
@jakebailey

Description

@jakebailey

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions