Skip to content
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

ci(eslint): add rule to encourage generic naming convention as ^(T|T[A-Z][A-Za-z]+)$ #6684

Merged

Conversation

manudeli
Copy link
Contributor

@manudeli manudeli commented Jan 11, 2024

@TkDodo I saw your post in X and @juliusmarminge's solution by @typescript-eslint/naming-convention with regex
I want to add it in this repository

I've been using this rule in various repositories for quite some time, and I really liked the fact that when defining a new generic, it encouraged me not to just name it T. It make coworker and interface user feel better DX.

@juliusmarminge Thanks for sharing your cool solution too.

Copy link

vercel bot commented Jan 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
query ⬜️ Ignored (Inspect) Visit Preview Jan 28, 2024 5:05pm

Copy link

nx-cloud bot commented Jan 11, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 3be51b3. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

Copy link

codesandbox-ci bot commented Jan 11, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 3be51b3:

Sandbox Source
@tanstack/query-example-angular-basic Configuration
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

@manudeli manudeli marked this pull request as ready for review January 11, 2024 18:13
@manudeli manudeli changed the title ci(eslint): add rule to force generic naming convention ci(eslint): add rule to force generic naming convention as '^(T|\\$)[A-Z][a-zA-Z]+[0-9]*$' Jan 11, 2024
@manudeli manudeli changed the title ci(eslint): add rule to force generic naming convention as '^(T|\\$)[A-Z][a-zA-Z]+[0-9]*$' ci(eslint): add rule to force generic naming convention as ^(T|\\$)[A-Z][a-zA-Z]+[0-9]*$ Jan 11, 2024
@manudeli manudeli changed the title ci(eslint): add rule to force generic naming convention as ^(T|\\$)[A-Z][a-zA-Z]+[0-9]*$ ci(eslint): add rule to encourage generic naming convention as ^(T|\\$)[A-Z][a-zA-Z]+[0-9]*$ Jan 11, 2024
.eslintrc.cjs Outdated
Comment on lines 51 to 57
selector: 'typeParameter',
format: ['PascalCase'],
leadingUnderscore: 'allow',
custom: {
regex: '^(T|\\$)[A-Z][a-zA-Z]+[0-9]*$',
match: true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm why would we allow leading underscores on a type param? I've set this in other projects to:

{
    selector: 'typeParameter',
    format: ['PascalCase'],
    leadingUnderscore: 'forbid',
    trailingUnderscore: 'forbid',
    custom: {
        regex: '^(T|\\$)([A-Z][a-z]+)*$',
        match: true,
    },
},

Copy link
Contributor Author

@manudeli manudeli Jan 14, 2024

Choose a reason for hiding this comment

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

hmm why would we allow leading underscores on a type param?

Oh thanks, I didn't care these options(leadingUnderscore, trailingUnderscore) mistakely.
I banned underscores, and simplified regex

like below

{
  selector: 'typeParameter',
  format: ['PascalCase'],
  leadingUnderscore: 'forbid',
  trailingUnderscore: 'forbid',
  custom: {
    regex: '^([A-Z]|T[A-Z][A-Za-z]+)$'',
    match: true,
  },
}

@manudeli
Copy link
Contributor Author

could we try to error it and just rename the type parameters if necessary? This should be non breaking ...

@TkDodo I will resolve eslint errors soon. wait a minutes please.

@manudeli
Copy link
Contributor Author

manudeli commented Jan 14, 2024

could we try to error it and just rename the type parameters if necessary? This should be non breaking ...

@TkDodo I will resolve eslint errors soon. wait a minutes please.

I tried to resolve eslint errors, but there was too many cases that don't need this rule like below. 🥲
How about this rule as warning now? This rule with warning also will make us follow this rule too.

export type MaybeRef<T> = Ref<T> | T // isn't it T also good? I think warning is enough for this case

export type MaybeRefOrGetter<T> = MaybeRef<T> | (() => T)

...smillar cases

or this rule as error in each files with eslint-disable if need?

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 14, 2024

T alone should be valid according to the regex, no?

@manudeli
Copy link
Contributor Author

manudeli commented Jan 14, 2024

T alone should be valid according to the regex, no?

Ah~, I thought T should be banned before.
Then K, X, Y should be valid right too?

@codecov-commenter
Copy link

codecov-commenter commented Jan 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b32ad24) 41.78% compared to head (3be51b3) 41.78%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6684   +/-   ##
=======================================
  Coverage   41.78%   41.78%           
=======================================
  Files         178      178           
  Lines        7017     7017           
  Branches     1421     1421           
=======================================
  Hits         2932     2932           
  Misses       3722     3722           
  Partials      363      363           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@manudeli manudeli changed the title ci(eslint): add rule to encourage generic naming convention as ^(T|\\$)[A-Z][a-zA-Z]+[0-9]*$ ci(eslint): add rule to encourage generic naming convention as ^([A-Z]|T[A-Z][A-Za-z]+)$ Jan 14, 2024
@manudeli
Copy link
Contributor Author

manudeli commented Jan 14, 2024

@TkDodo sorry, My reply was late because I didn't know much about regex and was studying it. I update regex for this rule as ^([A-Z]|T[A-Z][A-Za-z]+)$

This rule will make contributors use only below.

okay

alone T, U, V, etc. is okay
TQueryKey, TQueryFnData is okay

not okay

KQueryData, Result, Depth, State, DefinedQueryObserver, Runner, Args, TaggedQueryKey, Type, Value, etc. is banned from now. these should be started with T

And I updated codes not following this rule

ps. When I test this rule, I used this tool(https://rex.antfu.me/) made by @antfu. so cool thanks anthony fu!
image

@manudeli manudeli requested a review from TkDodo January 14, 2024 17:40
@manudeli manudeli force-pushed the ci/eslint/naming-convention-TXXX-for-generic branch from 0f245e4 to 25177d2 Compare January 20, 2024 15:39
@manudeli
Copy link
Contributor Author

@TkDodo check this again please

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 27, 2024

@manudeli I would prefer it if T would be the only allowed single character type parameter. This would make sure that one function can have T as a generic, but as soon as you add a second, you can't make <T,U> - you need to give meaningful names.

Imo one-character generics are best if there is only one on a given type / function. With two or more, you always need to now what they stand for ...

@manudeli
Copy link
Contributor Author

manudeli commented Jan 27, 2024

@manudeli I would prefer it if T would be the only allowed single character type parameter. This would make sure that one function can have T as a generic, but as soon as you add a second, you can't make <T,U> - you need to give meaningful names.

Imo one-character generics are best if there is only one on a given type / function. With two or more, you always need to now what they stand for ...

Ahha, I understood your intention. and I agree your opinion. then I'll change it as following you want

@manudeli manudeli changed the title ci(eslint): add rule to encourage generic naming convention as ^([A-Z]|T[A-Z][A-Za-z]+)$ ci(eslint): add rule to encourage generic naming convention as ^(T|T[A-Z][A-Za-z]+)$ Jan 27, 2024
@manudeli
Copy link
Contributor Author

manudeli commented Jan 27, 2024

@TkDodo I update rule as ^(T|T[A-Z][A-Za-z]+)$ from ^([A-Z]|T[A-Z][A-Za-z]+)$ and I resolved error from this rule in some code. We can check change in 31140ac

image

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 28, 2024

can you please fix the conflicts ?

@manudeli
Copy link
Contributor Author

can you please fix the conflicts ?

@TkDodo Sure, I resolved conflict now

@TkDodo TkDodo merged commit c774772 into TanStack:main Jan 28, 2024
manudeli added a commit to toss/suspensive that referenced this pull request Feb 14, 2024
# Overview

<!--
    A clear and concise description of what this pr is about.
 -->

TanStack/query#6684

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md)
2. I added documents and tests.
manudeli added a commit to toss/suspensive that referenced this pull request Aug 3, 2024
# Overview

<!--
    A clear and concise description of what this pr is about.
 -->

TanStack/query#6684

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md)
2. I added documents and tests.
manudeli added a commit to toss/suspensive that referenced this pull request Aug 3, 2024
# Overview

<!--
    A clear and concise description of what this pr is about.
 -->

TanStack/query#6684

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md)
2. I added documents and tests.
manudeli added a commit to toss/suspensive that referenced this pull request Aug 3, 2024
# Overview

<!--
    A clear and concise description of what this pr is about.
 -->

TanStack/query#6684

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md)
2. I added documents and tests.
manudeli added a commit to toss/suspensive that referenced this pull request Aug 3, 2024
# Overview

<!--
    A clear and concise description of what this pr is about.
 -->

TanStack/query#6684

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md)
2. I added documents and tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants