eslint-config-xo-typescript icon indicating copy to clipboard operation
eslint-config-xo-typescript copied to clipboard

Class constants naming

Open socram8888 opened this issue 3 years ago • 4 comments

Currently, the ESlint configuration will mark constant properties on classes as errors. For example:

class FNV {
    private static readonly PRIME = 16777619;
    private static readonly OFFSET = 2166136261;

    // ...
}

Will be considered an error, as they are expected to follow the camel case convention used for other mutable or instance-specific values, despite being an immutable constant.

Unless this is intentional, maybe it could make sense to add a rule to force attributes tagged with static readonly to follow the same UPPER_CASE format as the rest of the constants.

socram8888 avatar Nov 24 '21 12:11 socram8888

#42 is probably relevant here. I tried getting it to work for my interest which didnt seem to work for me. (unclear why) Would be nice to see improvements there!

EdJoPaTo avatar Nov 24 '21 13:11 EdJoPaTo

Adding:

{
	selector: 'classProperty',
	modifiers: ['static', 'readonly'],
	format: ['UPPER_CASE'],
},

Seems to do the job. Again, not sure if this should be added because I am not sure if the XO guideline considers this a constant or a class property.

socram8888 avatar Nov 24 '21 13:11 socram8888

I'm fine with allowing upper case static properties, but it should also camel case.

sindresorhus avatar Nov 24 '21 16:11 sindresorhus

I’m not sure why this would be an exception to the current naming rule. The same request but for top-level constants was rejected, if I remember correctly. Either both should be allowed or neither.

(I’d prefer both, but…)

fregante avatar Aug 06 '22 15:08 fregante