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): [no-unsafe-enum-assignment] add rule#6091

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

JoshuaKGoldberg
Copy link
Member

@JoshuaKGoldbergJoshuaKGoldberg commentedNov 25, 2022
edited by Josh-Cena
Loading

PR Checklist

Overview

Adds a newno-unsafe-enum-assignment rules that prevents assigning literals to enum values.

no-unsafe-enum-comparison was split out to#6107.

Co-authored-by:@Zamiell

HolgerJeromin and Zamiell reacted with hooray emoji
also fixes for codebase
@bradzacherbradzacher added the awaiting responseIssues waiting for a reply from the OP or another party labelDec 16, 2022
@JoshuaKGoldbergJoshuaKGoldberg requested review frombradzacher and removed request forZamiellDecember 16, 2022 17:57
@JoshuaKGoldbergJoshuaKGoldberg removed the awaiting responseIssues waiting for a reply from the OP or another party labelJan 10, 2023
@bradzacher
Copy link
Member

bradzacher commentedJan 30, 2023
edited
Loading

Additional cases that this rule should handle:

  • class properties
    classF{prop:Fruit=1;}
    • There's also cases like this:
      interfaceF{prop:Fruit;}classFooimplementsF{prop=1;}
  • object properties
    constx={prop:1}as{prop:Fruit))
  • assertions in general?
    constx=1asFruit;consty=<Fruit>1;
  • JSX attributes
    typeProps={prop:Fruit};declarefunctionComponent(props:Props):JSX.Element;(<Componentprop={1}/>);
  • return statements
    functionx():Fruit{return1;}
  • computed member expression properties
    declareconstfoo:{[kinFruit]:string};foo[0];foo?.[0];declareconstbar:{[Fruit.Apple]:string};bar[0];bar?.[0];

Edge cases to think about:

declarefunctionfn(arg:Fruit):void;fn(...[1]);constx=[1,Fruit.Apple,2]asconst;consty:readonlyFruit[]=x;interfaceFoo<TextendsFruit>{prop:T;}exportconstclazz:Foo<Fruit>=classWTF{staticprop=1;}

@bradzacherbradzacher added the awaiting responseIssues waiting for a reply from the OP or another party labelJan 30, 2023
@JoshuaKGoldberg
Copy link
MemberAuthor

Yeah, there are alot of cases to handle here. Basically any identifier and literal has to be checked - including array literals and parameters to generic functions!

I don't have the time to work on this rule given other, more important features to work on. So I'm going to close this PR. If anybody else wants to pick it up, please do!

Note that the code will likely need to be refactored a good bit to support all the new cases.

@JoshuaKGoldbergJoshuaKGoldberg deleted the strict-enums branchFebruary 4, 2023 21:23
@JoshuaKGoldbergJoshuaKGoldberg restored the strict-enums branchFebruary 4, 2023 21:23
@bradzacher
Copy link
Member

I wonder@JoshuaKGoldberg - do you think this should be built directly into ourno-unsafe-* rules, rather than being their own rule.

The reason I originally split up the rules into a set was to isolate the logic for checking assignments in each case to reduce the overall maintenance complexity.
So those rules already contain much of the logic required by this rule in terms of getting contextual types and checking generics.

They don't currently do some of the checks I've listed (like checking when{prop: any} is assigned to{prop: string}), but at least there would be parity.

WDYT? Do we refactor to reuse logic or build into those rules?

@JoshuaKGoldberg
Copy link
MemberAuthor

Hmmmmm, my gut reaction is that I'd rather extract those things out into shared utilities. +1 to reusing the logic for sure. Enums really aren't the same use case asany safety. I also had to reimplement a bunch of them inTypeStat and that was a huge pain.

@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsFeb 13, 2023
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@HolgerJerominHolgerJerominHolgerJeromin left review comments

@ZamiellZamiellZamiell left review comments

@bradzacherbradzacherAwaiting requested review from bradzacher

Assignees
No one assigned
Labels
awaiting responseIssues waiting for a reply from the OP or another partyenhancement: new plugin ruleNew rule request for eslint-plugin
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@JoshuaKGoldberg@Zamiell@bradzacher@HolgerJeromin

[8]ページ先頭

©2009-2025 Movatter.jp