tslint-consistent-codestyle icon indicating copy to clipboard operation
tslint-consistent-codestyle copied to clipboard

naming-convention: banned names and require minimum length

Open AndreasGassmann opened this issue 6 years ago • 1 comments

I would like to disallow short variable names and certain keywords.

This should already be possible with a regex, but maybe that could still be added as an option?

Examples where this is useful:

// Short variable names
try {} catch (e) {}
[1, 2].map(n => n*2)
[1, 2].forEach(v => v)
window.addEventListener('error', (ev) => {})

// Banned words (specific names, such as `err`, `num` and `val`
try {} catch (err) {}
[1, 2].map(num => num*2)
[1, 2].forEach(val => val)

Maybe there should be a way to define exceptions, for example i:

for(let i = 0; i < 1; i++) {}

I also noticed that the rule doesn't seem to trigger for the variable used in the catch: try {} catch (e) {}. Is that intended behavior or a bug?

AndreasGassmann avatar Feb 28 '19 15:02 AndreasGassmann

I agree that this is a valid use case. The blacklist would be pretty hard to implement as regular expression. Combining that with a minimum length makes it even worse.

I'm accepting PRs to add new options for minLength, blacklist and maybe maxLength. We just need to come to a conclusion whether blacklist should be a string array or a regular expression.

Maybe there should be a way to define exceptions

That's already possible using the filter option.

ajafff avatar Mar 01 '19 20:03 ajafff