Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

no-duplicate-string ignore Key-Names #852

Open
marvinside opened this issue Apr 24, 2019 · 3 comments
Open

no-duplicate-string ignore Key-Names #852

marvinside opened this issue Apr 24, 2019 · 3 comments

Comments

@marvinside
Copy link

I want to request a feature.

I have all the HTTP Status Codes in an Enum. This maces it easier to reference them and also immediately see what this code means.
The Enum Looks like this:

enum HTTPStatusCode {
	/// <ommited>

	'Bad Request' = 400,
	Unauthorized = 401,
	'Payment Required' = 402,
	Forbidden = 403,
	'Not Found' = 404,
	'Method Not Allowed' = 405,
	'Not Acceptable' = 406,
	/// <ommited>
}

As you see, there are spaces in the Keys.
So if i reference them, it looks like this: HTTPStatusCode['Bad Request']
If i use the same Code to often, i'll get the no-duplicate-string warning

Suggested rule-extension:

"no-duplicate-string": true, // Keep current behaivor
"no-duplicate-string": [true, "ignore-keynames"], // Ignore strings which are keynames
@vilchik-elena
Copy link
Contributor

Indeed, when there is typo in such string (enum element) TypeScript compiler will complain (not very explicitly but still). Even refactoring (renaming) will work for such strings (tested in VSCode).

Still in order to introduce this exception to the rule we will need to access type information, to know that object on which string index is accessed is a enum. May be we can simply ignore all strings inside [].

@marvinside
Copy link
Author

If you ignore everything inside [ ], you will also ignore Array declarations.
But you could check for something = ["Test1", "Test2"] and something["key-name"]

@vilchik-elena
Copy link
Contributor

@marvinside sorry I was not clear, by inside [] I meant array access, not array creation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants