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

Docs: Add note regarding PascalCase function components in naming-convention rule #7108

Closed
2 tasks done
sharky98 opened this issue Jun 16, 2023 · 3 comments · Fixed by #9196
Closed
2 tasks done

Docs: Add note regarding PascalCase function components in naming-convention rule #7108

sharky98 opened this issue Jun 16, 2023 · 3 comments · Fixed by #9196
Labels
accepting prs Go ahead, send a pull request that resolves this issue documentation Documentation ("docs") that needs adding/updating locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.

Comments

@sharky98
Copy link

Before You File a Documentation Request Please Confirm You Have Done The Following...

Suggested Changes

Background

I was looking to use naming-convention for my React and non-React packages. It is known that React convention is to use PascalCase component, while the rest is camelCase. Turns out, it is not possible to define a rule to cover this case, since there is nothing to distinguish a React component function from a non-component function (neither function arguments or return type are fool proof).

It has been discussed in #2607 and #2692.

Requested change

Since the PascalCase pattern for component is quite common, I would like to see a note regarding this in the documentation of the naming convention rule. The best place could be in the first example, which shows how to enforce function to camelCase.

### Enforce that all variables, functions and properties follow are camelCase

```json
{
  "@typescript-eslint/naming-convention": [
    "error",
    { "selector": "variableLike", "format": ["camelCase"] }
  ]
}
```

:::caution
Using this rule will trigger errors on functional components using `PascalCase` convention, as used in React. See [issue #2607](https://github.com/typescript-eslint/typescript-eslint/issues/2607) for more information and [eslint-plugin-react/jsx-pascal-case rule](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md) rule.
:::

Affected URL(s)

https://typescript-eslint.io/rules/naming-convention/

@sharky98 sharky98 added documentation Documentation ("docs") that needs adding/updating triage Waiting for team members to take a look labels Jun 16, 2023
@sharky98 sharky98 changed the title Docs: <a short description of my proposal> Docs: Add note regarding PascalCase functional components in naming-convention rule Jun 16, 2023
@bradzacher
Copy link
Member

since there is nothing to distinguish a React component function from a non-component function (neither function arguments or return type are fool proof).

Yeah it's a major problem for static analysis of function components. You should see the crazy logic eslint-plugin-react has to try and detect them!

It's also a sticking point for various research exploration projects within the react team.

For a react codebase I'd recommend specifying functions separately to specifically allow both camelCase AND PascalCase just for them.

But also some codebases use arrow functions for components... so you'd also need to allow both styles for variables as well!

@sharky98
Copy link
Author

For a react codebase I'd recommend specifying functions separately to specifically allow both camelCase AND PascalCase just for them.

This is why I would suggest adding the caution note in that first example. I think React functional components are common enough (or will be sooner or later), that a caution admonition in the docs would help user that may be less knowledgeable in the inner working of all those terms.

Maybe could be worded a bit differently.

:::caution
Using this rule will trigger errors on functional components using `PascalCase` convention, as used in React. You may want to use `"format": ["camelCase", "PascalCase"]` to allow both convention.

See [issue #2607](https://github.com/typescript-eslint/typescript-eslint/issues/2607) for more information and [eslint-plugin-react/jsx-pascal-case rule](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md) rule.
:::

But also some codebases use arrow functions for components... so you'd also need to allow both styles for variables as well!

I am guilty of that! 🤣 However, I did limit the PascalCase only on variables that are types of function ({ selector: "variable", types: ["function"], modifiers: ["const"], format: ["PascalCase", "camelCase"] }). So far so good in my own codebase.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Docs: Add note regarding PascalCase functional components in naming-convention rule Docs: Add note regarding PascalCase function components in naming-convention rule Aug 17, 2023
@JoshuaKGoldberg
Copy link
Member

Accepting a PR to add an explicit note about UI library function components. A couple thoughts:

  • It's function components, not functional. This is nitpicky 😄 but you can have function components with non-functional side effects because of useEffect & co.
  • It's good to mention React for searchability, but as with this issue's description, it should be clear that it works with other UI libraries. I'd suggest naming at least Solid and Preact.

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Aug 17, 2023
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Jun 10, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue documentation Documentation ("docs") that needs adding/updating locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants