infctr / eslint-plugin-typescript-sort-keys

A linter plugin to require sorting interface and string enum keys
ISC License
144 stars 28 forks source link

crash in typescript-sort-keys/interface on constructor declarations #9

Closed arild-haugstad closed 4 years ago

arild-haugstad commented 4 years ago

As a minimal example; if we extend the ClockConstructor interface from the TypeScript documentation with an overload, so that there is something to sort, we get a crash when sorting uses getPropertyName(node) which attempts to read node.key.name, which fails on TSConstructSignatureDeclaration nodes.

Example code to lint:

interface ClockConstructor {
    new (hour: number, minute: number): ClockInterface;
    new (hour: number): ClockInterface;
}
interface ClockInterface {
    tick(): void;
}

Depending on the preferred order wrt. constructor declarations, perhaps replacing the line

return module.exports.getStaticPropertyName(node) || node.key.name || null;

in getPropertyName with

return module.exports.getStaticPropertyName(node) || (node.key && node.key.name) || null;

would be a reasonable solution?

infctr commented 4 years ago

@bfmiv Would you like to investigate?

bfmiv commented 4 years ago

@infctr sorry just saw this, yes I will take a look

infctr commented 4 years ago

@bfmiv Thank you