-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[naming-convention] Allow quoted object keys in any format #1483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The base I don't think I agree with this entirely, as literal prop names could then slip through, accidentally circumventing the rule. /*
eslint '@typescript-eslint/naming-convention': [
'error',
{
selector: 'default',
format: [
'strictCamelCase'
],
leadingUnderscore: 'allow',
trailingUnderscore: 'allow'
}
}
*/
const x = {
'Skip This': 1, // sure, it seems obvious that this should be skipped
x_x: 1, // error - not in strictCamelCase
'y_y': 1, // this should error too, right?
} A surprising number of people don't use prettier, and the base I don't know the best solution here. Part of me thinks maybe this is just an eslint-disable case. Unsure, need to think some more. Maybe it should inspect the name to see if it requires quotes, and ignores it if it does? |
I would argue that's an explicit intent on using something else.
You could make it an option to ignore quote object literal keys or expose the type so I can manually ignore it myself.
It's quite a common use-case though. You would then force pretty much any server-related app to use lots of eslint-disable comments as you kinda have to do this when dealing with HTTP headers.
That would be the preferred solution, yes. |
This comment has been minimized.
This comment has been minimized.
Hello people. Thank you for the excellent work with this tool. I'm having the same problem. Is there any way to resolve it other than to disable it as a comment? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Digging into this - it's actually really, really, really hard to tell if a string is a valid identifier. The grammar for an identifier is very permissive. The only way to do this reliably would be to add an external library, and I don't hugely want to do that. Prettier uses And you can see the regexes However, I never thought about this - but this is entirely achievable from a configuration standpoint. You can use the {
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "property",
"format": ["strictCamelCase"],
"filter": {
// you can expand this regex to add more allowed names
"regex": "^(Property-Name-One|Property-Name-Two)$",
"match": false
}
}
]
} You can use the {
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "property",
"format": ["strictCamelCase"],
"filter": {
// you can expand this regex as you find more cases that require quoting that you want to allow
"regex": "[-]",
"match": false
}
}
]
} I have updated the documentation appropriately. |
Hello, thank you very much for responding to our problem. Sorry if I didn't understand the way you solved the problem, but for my case I had to do it a little differently. I had the following case: const headers = {
'Retry-After': retryAfter,
'X-RateLimit-Limit': limit,
'X-RateLimit-Remaining': remaining,
'X-RateLimit-Reset': resete
} And to resolve, based on your help I did so: {
"selector": "property",
"format": ["PascalCase"],
"filter": {
"regex": "[-]",
"match": true
}
} |
Instead, you're putting the burden on every user. Couldn't ESLint potentially expose an utility for this? |
only users that set headers via string keys. There aren't many other common use cases that I know of. I believe that this is a small fraction of users. Even then using a configuration option as above is better as you can thoroughly customise the allowed formats and names based on your use case. For example:
The configuration is flexible so you can apply the level of strictness you want (which is the entire point of merging all the naming rules together). Having an
ESLint exposes no utilities to help with writing rules. |
Neither of us have any proof on that. It's common when doing HTTP requests.
Flexibility is great, but I also feel that defaults should be good.
Yes, I wouldn't want such an option. Agreed.
It should though, but I realize this is not the place to discuss that. |
Just notifying anyone subscribed to this issue - #2813 adds the If you simply want to allow all property names that require quotes, you can use the New example from the docs for ignoring property names that require quotes (you can customise this to limit it if you see fit). {
"@typescript-eslint/naming-convention": [
"error",
{
"selector": [
"classProperty",
"objectLiteralProperty",
"typeProperty",
"classMethod",
"objectLiteralMethod",
"typeMethod",
"accessor",
"enumMember"
],
"format": null,
"modifiers": ["requiresQuotes"]
}
]
} |
Expected Result
I expected the rule to ignore quoted object keys or have a way to ignore them. I think the old camelcase rule ignored such case.
Actual Result
Additional Info
Versions
@typescript-eslint/eslint-plugin
2.17.0
@typescript-eslint/parser
2.17.0
TypeScript
3.7.5
ESLint
6.8.0
node
10.17.0
npm
6.13.6
The text was updated successfully, but these errors were encountered: