Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin):prefer-consistent-enums rule#3891

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

brianconnoly
Copy link

@brianconnolybrianconnoly commentedSep 16, 2021
edited
Loading

Added new rule —prefer-consistent-enums

Enum members can have members of different types
Enums with string initializers can be used as object to iterate over them usingObject.keys /Object.values

enumStatus{Open='open',Closed='closed',}Object.values(Status);// ['open','closed']

But if member with number initializer will be added (or without one) — results ofObject.keys /Object.values will have additional auto generated items

enumStatus{Pending,Open='open',Closed='closed',}Object.values(Status);//  ["Pending", 0, "open", "closed"]
enumStatus{Pending=123,Open='open',Closed='closed',}Object.values(Status);//  ["123", "Pending", "open", "closed"]

This can lead to bugs, so this rule can prevent them by requiring enums to have consistent member types

@typescript-eslint
Copy link
Contributor

Thanks for the PR,@brianconnoly!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitorsper day.

@nx-cloud
Copy link

nx-cloudbot commentedSep 16, 2021
edited
Loading

Nx Cloud Report

CI ran the following commands for commitee959b1. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch

StatusCommand
#000000nx test @typescript-eslint/eslint-plugin-tslint
#000000nx test @typescript-eslint/eslint-plugin
#000000nx test @typescript-eslint/eslint-plugin-tslint
#000000nx test @typescript-eslint/eslint-plugin
#000000nx test @typescript-eslint/eslint-plugin-internal
#000000nx test @typescript-eslint/eslint-plugin-tslint
#000000nx test @typescript-eslint/eslint-plugin
#000000nx run-many --target=typecheck --all --parallel
#000000nx test @typescript-eslint/parser
#000000nx test @typescript-eslint/experimental-utils
#000000nx test @typescript-eslint/parser
#000000nx test @typescript-eslint/scope-manager
#000000nx test @typescript-eslint/experimental-utils
#000000nx test @typescript-eslint/visitor-keys
#000000nx test @typescript-eslint/scope-manager
#000000nx test @typescript-eslint/typescript-estree
#000000nx test @typescript-eslint/visitor-keys
#000000nx run-many --target=build --all --parallel
#000000nx test @typescript-eslint/typescript-estree
#000000nx run-many --target=build --all --parallel

Sent with 💌 fromNxCloud.

@codecov
Copy link

codecovbot commentedSep 16, 2021
edited
Loading

Codecov Report

Merging#3891 (ee959b1) intomaster (b1df817) willincrease coverage by0.81%.
The diff coverage is90.47%.

@@            Coverage Diff             @@##           master    #3891      +/-   ##==========================================+ Coverage   92.70%   93.51%   +0.81%==========================================  Files         329      150     -179       Lines       11534     8085    -3449       Branches     3257     2562     -695     ==========================================- Hits        10693     7561    -3132+ Misses        368      165     -203+ Partials      473      359     -114
FlagCoverage Δ
unittest93.51% <90.47%> (+0.81%)⬆️

Flags with carried forward coverage won't be shown.Click here to find out more.

Impacted FilesCoverage Δ
packages/eslint-plugin/src/configs/all.ts100.00% <ø> (ø)
...eslint-plugin/src/rules/prefer-consistent-enums.ts90.47% <90.47%> (ø)
...rimental-utils/src/ts-eslint-scope/ScopeManager.ts
...es/experimental-utils/src/ts-eslint-scope/Scope.ts
...e-manager/src/definition/FunctionNameDefinition.ts
packages/eslint-plugin-tslint/src/rules/config.ts
packages/scope-manager/src/scope/TSModuleScope.ts
...cope-manager/src/definition/ParameterDefinition.ts
...ackages/scope-manager/src/variable/VariableBase.ts
packages/scope-manager/src/lib/es2017.string.ts
... and173 more

@bradzacherbradzacher added the enhancement: new plugin ruleNew rule request for eslint-plugin labelSep 16, 2021
@JoshuaKGoldberg
Copy link
Member

LGTM in general, thanks for sending! Requesting changes on the error message and auto-fixer.

requiresTypeChecking: false,
},
messages: {
nonConsistentEnum: `All enum members of {{ name }} must be same type (string, number, boolean, etc).`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

boolean

I'm under the impression TypeScript enums can't haveboolean typed values: onlystring ornumber.


if (!memberType) {
return;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Indeed, this check isn't covered in tests. Perhaps it'd be good to have a test that includes an invalid enum value such as a boolean or array?

Comment on lines +76 to +79
/**
* If initializers types dont match — suggest change
*/
if (enumType !== memberType) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Teeny nit: consider making this also return early, to reduce nesting later on? This function is pretty nested as-is.

Suggested change
/**
*Ifinitializerstypesdontmatchsuggestchange
*/
if(enumType!==memberType){
/**
*Ifinitializerstypesmatchdon't suggest change
*/
if(enumType===memberType){
return;
}

(not a blocker IMO)

node: member,
messageId: 'nonConsistentEnum',
data: { name: enumName },
suggest: [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

🤔 I'm a little -1 on suggesting such an unsafe change. In theory folks shouldn't rely on enum values but in practice it happens.eslint/eslint#7873

I'll defer to another reviewer's opinion if they feel strongly on it.

create(context) {
const sourceCode = context.getSourceCode();

function TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nit: out of curiosity, is there a reason to have this as a standalone function instead of an inline member of the returned object?

return {  TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void {    // ...  }}

@JoshuaKGoldberg
Copy link
Member

Btw@brianconnoly we generally ask that PRs address an existing issue, to give folks a chance to discuss ideas & verify they'd be accepted before putting a lot of work into a PR. This one seems reasonable to me as a non-required rule so just maybe a heads up for next time? I'll defer to@bradzacher on whether this is a common enough use case to justify being in typescript-eslint core.

@JoshuaKGoldbergJoshuaKGoldberg added the awaiting responseIssues waiting for a reply from the OP or another party labelOct 16, 2021
@bradzacherbradzacher removed the awaiting responseIssues waiting for a reply from the OP or another party labelOct 18, 2021
@JoshuaKGoldberg
Copy link
Member

Oh, this actually resolves#2694, I think!

@JoshuaKGoldbergJoshuaKGoldberg added the awaiting responseIssues waiting for a reply from the OP or another party labelOct 25, 2021
@osdiab
Copy link

Should this be merged?

@JoshuaKGoldberg
Copy link
Member

@osdiab I'd requested changes that haven't yet been addressed.@brianconnoly do you still have the time to work on this?

@bradzacher
Copy link
Member

At this point the PR is now 6 months stale. Closing for housekeeping.
This is not a rejection - anyone is free to re-raise the PR to continue this work!

@bradzacherbradzacher added stalePRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period awaiting responseIssues waiting for a reply from the OP or another party and removed awaiting responseIssues waiting for a reply from the OP or another party labelsMar 4, 2022
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsMay 25, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@JoshuaKGoldbergJoshuaKGoldbergJoshuaKGoldberg left review comments

Assignees
No one assigned
Labels
awaiting responseIssues waiting for a reply from the OP or another partyenhancement: new plugin ruleNew rule request for eslint-pluginstalePRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@brianconnoly@JoshuaKGoldberg@osdiab@bradzacher

[8]ページ先頭

©2009-2025 Movatter.jp