-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: no-enum / ban-enum / no-use-enum #561
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
I think this also kind of useful, and perhaps it can be auto-fixed in some cases. 👍 |
we don't like to implement rules that just ban a language feature, because we've found that different people like different things, and unless it's preposterous that you'd use the inverse (like Happy for something like this to be implemented, but we may want to review it in conjunction with the other enum rules with PRs (#290, #315) |
True, I feel there are many similar rules flooded, and would like to have 1 entry point to regulate how We are using TS as just a pure type checker, so any non-type-system-related syntax/operators will out of our interests. |
Will the core |
I believe that would work @platinumazure {
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "TSEnumDeclaration",
"message": "Don't declare enums"
}
]
}
} |
Tested, the above is the one I am looking for. But I think this rule might be useful if ESLint-TypeScript would ever provide the recommended default setup for Babel users. 🤔 |
It may be worth noting that you can also disallow either const or non-const enums selectively with Disallow const enums:
Disallow non-const enums: {
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "TSEnumDeclaration:not([const=true])",
"message": "Don't declare non-const enums"
}
]
}
} |
Is there any way to selectively do this so that const enums are allowed for internal package use, but cannot be exported for external consumption? |
It's pretty hard / near impossible to detect this. A rule like that would have to understand the above, and be able to scan every export from the "external api", and crawl every single type to find any const enums. I.e. very hard to do, very niche, and outside of the scope of this plugin. |
After switching to TypeScript, our team's monorepo ended up with quite a few enums in various parts of the code. Initially, referring to enums felt better than exporting named constants as we did in the good old JavaScript times (
export const STATUS_READY = "ready"
). However, we noticed a few issues with enums afterwards and decided to switch to TypeScript discriminated unions instead. The most irritating issue with enums for us was that their imports could cause cyclic module dependencies, thus magically resultingundefined
in unexpected places, e.g. when running Jest tests.We have removed all enums from the codebase, but there is still a chance that someone can introduce a new enum via a PR and this will be overlooked. I'm wondering if
@typescript-eslint/eslint-plugin
could have a rule named likeno-enum
/ban-enum
/no-use-enum
to prevent this from happening.All the rule would have to do is searching for the
enum
token – that does not seem too complex. WDYT folks?Related: #280 (see comment by @leoyli with a similar request)
The text was updated successfully, but these errors were encountered: