Skip to content

Commit 74ac91d

Browse files
Rel1cxCopilot
andauthored
feat: update naming convention rules default excepts (#1216)
Signed-off-by: REL1CX <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 36dc088 commit 74ac91d

File tree

9 files changed

+132
-158
lines changed

9 files changed

+132
-158
lines changed

apps/website/content/docs/rules/meta.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
"dom-no-unsafe-target-blank",
7979
"dom-no-use-form-state",
8080
"dom-no-void-elements-with-children",
81-
"dom-prefer-namespace-import",
8281
"---Web API Rules---",
8382
"web-api-no-leaked-event-listener",
8483
"web-api-no-leaked-interval",

packages/plugins/eslint-plugin-react-dom/src/rules/prefer-namespace-import.mdx

Lines changed: 0 additions & 45 deletions
This file was deleted.

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-layout-effect.mdx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
title: no-direct-set-state-in-use-layout-effect
33
---
44

5-
<Callout type="warning">This rule is experimental and may change in the future or be removed. It is not recommended to use it in production code at this time.</Callout>
6-
75
**Full Name in `eslint-plugin-react-hooks-extra`**
86

97
```sh copy
@@ -20,12 +18,6 @@ react-hooks-extra/no-direct-set-state-in-use-layout-effect
2018

2119
`🧪`
2220

23-
**Presets**
24-
25-
- `recommended`
26-
- `recommended-typescript`
27-
- `recommended-type-checked`
28-
2921
## Description
3022

3123
Disallow **direct** calls to the [`set` function](https://react.dev/reference/react/useState#setstate) of `useState` in `useLayoutEffect`.
@@ -149,7 +141,11 @@ export default function RemoteContent() {
149141
}
150142
```
151143

152-
The following examples are derived from the [React documentation](https://react.dev/learn/you-might-not-need-an-effect):
144+
<Callout title="TIP">
145+
If you need to fetch remote data within the component, consider using libraries like [TanStack Query](https://tanstack.com/query/v3/) or [SWR](https://swr.vercel.app/). They handle caching, re-fetching, and state management for you, making your code cleaner and more efficient.
146+
</Callout>
147+
148+
The following examples are derived from the [React Docs: You Might Not Need an Effect](https://react.dev/learn/you-might-not-need-an-effect):
153149

154150
### Failing
155151

packages/plugins/eslint-plugin-react-naming-convention/src/rules/component-name.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function MyComponent() {
5454
- `excepts`: (optional) An array of component names that are allowed to not follow the rule.
5555
- `allowAllCaps`: (optional) If `true`, allows all caps component names. Default is `false`.
5656

57-
## Rule Options Examples
57+
## Configuration Examples
5858

5959
```json
6060
{

packages/plugins/eslint-plugin-react-naming-convention/src/rules/component-name.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ type Options = readonly [
1515
| Case
1616
| {
1717
allowAllCaps?: boolean;
18-
/**
19-
* @todo Remove in the next major version
20-
* @deprecated Component names now need to start with an uppercase letter instead of a non-lowercase letter. This means `_Button` or `_component` are no longer valid. (@kassens) in https://github.com/facebook/react/pull/25162
21-
*/
22-
allowLeadingUnderscore?: boolean;
23-
/**
24-
* @todo Remove in the next major version
25-
* @deprecated This option has no actual effect on the rule
26-
*/
27-
allowNamespace?: boolean;
2818
excepts?: readonly string[];
2919
rule?: Case;
3020
},
@@ -50,16 +40,6 @@ const schema = [
5040
additionalProperties: false,
5141
properties: {
5242
allowAllCaps: { type: "boolean" },
53-
/**
54-
* @todo Remove in the next major version
55-
* @deprecated
56-
*/
57-
allowLeadingUnderscore: { type: "boolean" },
58-
/**
59-
* @todo Remove in the next major version
60-
* @deprecated
61-
*/
62-
allowNamespace: { type: "boolean" },
6343
excepts: {
6444
type: "array",
6545
items: { type: "string", format: "regex" },

packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename-extension.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This rule enforces consistent file extensions for JSX files.
3434
- `extensions`: List of file extensions that should be treated as JSX files. Default is `[".jsx", ".tsx"]`.
3535
- `ignoreFilesWithoutCode`: When set to `true`, this rule will ignore files that do not contain JSX syntax. Default is `true`.
3636

37-
## Rule Options Examples
37+
## Configuration Examples
3838

3939
```js title="eslint.config.js"
4040
// ...

packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.mdx

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,106 +20,117 @@ react-naming-convention/filename
2020

2121
## Description
2222

23-
Enforces consistent file naming conventions.
23+
This rule enforces consistent file naming conventions for React components, hooks, and other project files.
24+
25+
By default, this rule enforces `PascalCase`, but it can be configured to support `camelCase`, `kebab-case`, or `snake_case` to match your project's standards.
2426

2527
## Examples
2628

2729
### Failing
2830

29-
```bash title="Terminal" {3}
30-
npx eslint --rule '@eslint-react/naming-convention/filename: ["warn", { "rule": "PascalCase" }]' .
31-
32-
src/components/component.tsx
33-
1:1 error "File name `component.tsx` does not match `PascalCase`. Should rename to `Component.tsx` @eslint-react/naming-convention/filename
31+
If the rule is configured for `PascalCase`, the following filename will trigger a warning:
3432

35-
✖ 1 problems (0 errors, 1 warnings)
33+
```js title="eslint.config.js"
34+
export default [
35+
{
36+
files: ["**/*.tsx"],
37+
rules: {
38+
"@eslint-react/naming-convention/filename": ["warn", { rule: "PascalCase" }],
39+
},
40+
},
41+
];
3642
```
3743

38-
```bash {3}
39-
npx eslint --rule '@eslint-react/naming-convention/filename: ["warn", { "rule": "kebab-case" }]' .
44+
```bash title="src/components/component.tsx"
45+
# File name "component.tsx" does not match PascalCase.
46+
```
4047

41-
src/components/example_component.tsx
42-
1:1 error "File name `example_component.tsx` does not match `kebab-case`. Should rename to `example-component.tsx` @eslint-react/naming-convention/filename
48+
```text title="ESLint Output"
49+
src/components/component.tsx
50+
1:1 error File name `component.tsx` does not match `PascalCase`. Should rename to `Component.tsx` @eslint-react/naming-convention/filename
4351
44-
✖ 1 problems (0 errors, 1 warnings)
52+
✖ 1 problem (1 error, 0 warnings)
4553
```
4654

4755
### Passing
4856

49-
```bash title="Terminal"
50-
npx eslint --rule '@eslint-react/naming-convention/filename: ["warn", { "rule": "PascalCase" }]' .
57+
With the same `PascalCase` configuration, this file name is valid:
5158

52-
src/components/Component.tsx
53-
54-
✨ Done in 0.61s.
59+
```bash title="src/components/Component.tsx"
60+
# Correctly named file.
5561
```
5662

57-
```bash title="Terminal"
58-
npx eslint --rule '@eslint-react/naming-convention/filename: ["warn", { "rule": "kebab-case" }]' .
63+
This file will pass without any warnings.
5964

60-
src/components/example-component.tsx
65+
## Rule Options
6166

62-
✨ Done in 0.61s.
63-
```
67+
The rule accepts an object with the following properties:
6468

65-
## Rule Options
69+
- `rule`: The naming convention to enforce. Default: `"PascalCase"`.
70+
- `"PascalCase"`: `ExampleComponent.tsx`
71+
- `"camelCase"`: `exampleComponent.tsx`
72+
- `"kebab-case"`: `example-component.tsx`
73+
- `"snake_case"`: `example_component.tsx`
74+
- `excepts`: An array of strings or regex patterns to exclude certain file names from this rule. The default exceptions are designed to accommodate common patterns in modern web frameworks:
75+
- `"index"`: Ignores `index` files (e.g., `index.ts`, `index.tsx`).
76+
- `"/^_/"`: Ignores files starting with an underscore (e.g., `_app.tsx`, `_layout.tsx`).
77+
- `"/^\\$/"`: Ignores files starting with a dollar sign (e.g., `$.tsx`).
78+
- `"/^[0-9]+$/"`: Ignores files that are purely numeric (e.g., `404.tsx`).
79+
- `"/^\[[^\]]+\]$/"`: Ignores files with dynamic route segments in brackets (e.g., `[slug].tsx`).
6680

67-
- `rule`: The rule to apply to the file name. Default is `"PascalCase"`. Possible values:
68-
1. `PascalCase`: PascalCase
69-
2. `camelCase`: camelCase
70-
3. `kebab-case`: kebab-case
71-
4. `snake_case`: snake\_case
81+
## Configuration Examples
7282

73-
## Rule Options Examples
83+
### Enforcing `kebab-case`
7484

7585
```js title="eslint.config.js"
76-
// ...
7786
export default [
78-
// ...
7987
{
8088
files: ["**/*.tsx"],
8189
rules: {
82-
"@eslint-react/naming-convention/filename": ["warn", "PascalCase"]
83-
}
90+
"@eslint-react/naming-convention/filename": ["warn", { rule: "kebab-case" }],
91+
},
92+
},
8493
];
8594
```
8695

96+
### Different Rules for Different Files
97+
98+
You can apply different conventions to different parts of your project. For example, use `PascalCase` for components and `camelCase` for hooks.
99+
87100
```js title="eslint.config.js"
88-
// ...
89101
export default [
90-
// ...
91102
{
92-
files: ["**/*.tsx"],
103+
files: ["src/components/**/*.{ts,tsx}"],
104+
rules: {
105+
"@eslint-react/naming-convention/filename": ["warn", { rule: "PascalCase" }],
106+
},
107+
},
108+
{
109+
files: ["src/hooks/**/use*.{ts,tsx}"],
93110
rules: {
94-
"@eslint-react/naming-convention/filename": ["warn", { "rule": "kebab-case" }]
95-
}
111+
"@eslint-react/naming-convention/filename": ["warn", { rule: "camelCase" }],
112+
},
113+
},
96114
];
97115
```
98116

99-
### Applying different rules to different files
117+
### Disabling the Rule for Specific Directories
118+
119+
Framework-specific directories like Next.js's `app` router often have their own naming conventions. You can disable the rule for these directories.
100120

101121
```js title="eslint.config.js"
102-
// ...
103122
export default [
104-
// ...
105123
{
106-
files: ["src/**/*.{ts,tsx}"],
107-
ignore: ["**/index.{ts,tsx}"],
124+
files: ["**/*.{ts,tsx}"],
108125
rules: {
109-
"@eslint-react/naming-convention/filename": ["warn", "PascalCase"],
126+
"@eslint-react/naming-convention/filename": ["warn", { rule: "kebab-case" }],
110127
},
111128
},
112129
{
113-
files: ["src/pages/**/*.{ts,tsx}"],
114-
ignore: ["**/index.{ts,tsx}"],
130+
// Opt-out for files in the 'app' directory
131+
files: ["app/**/*.{ts,tsx}"],
115132
rules: {
116-
"@eslint-react/naming-convention/filename": ["warn", "kebab-case"],
117-
},
118-
},
119-
{
120-
files: ["src/hooks/**/use*.{ts,tsx}"],
121-
rules: {
122-
"@eslint-react/naming-convention/filename": ["warn", "camelCase"],
133+
"@eslint-react/naming-convention/filename": "off",
123134
},
124135
},
125136
];
@@ -129,10 +140,3 @@ export default [
129140

130141
- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.ts)
131142
- [Test source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.spec.ts)
132-
133-
---
134-
135-
## See Also
136-
137-
- [`filename-extension`](./filename-extension):
138-
Enforces consistent use of the JSX file extension.

packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ ruleTester.run(RULE_NAME, rule, {
110110
filename: "snake_case.tsx",
111111
options: [{ rule: "camelCase" }],
112112
},
113+
{
114+
code,
115+
errors: [
116+
{
117+
messageId: "filenameInvalid",
118+
data: {
119+
name: "snake_case.test.tsx",
120+
rule: "camelCase",
121+
suggestion: "snakeCase.test.tsx",
122+
},
123+
},
124+
],
125+
filename: "snake_case.test.tsx",
126+
options: [{ rule: "camelCase" }],
127+
},
113128
],
114129
valid: [
115130
{
@@ -151,5 +166,35 @@ ruleTester.run(RULE_NAME, rule, {
151166
filename: "snake_case.tsx",
152167
options: [{ rule: "snake_case" }],
153168
},
169+
{
170+
code,
171+
filename: "snake_case.test.tsx",
172+
options: [{ rule: "snake_case" }],
173+
},
174+
{
175+
code,
176+
filename: "404.tsx",
177+
options: [{ rule: "PascalCase" }],
178+
},
179+
{
180+
code,
181+
filename: "$.tsx",
182+
options: [{ rule: "PascalCase" }],
183+
},
184+
{
185+
code,
186+
filename: "$id.tsx",
187+
options: [{ rule: "PascalCase" }],
188+
},
189+
{
190+
code,
191+
filename: "_app.tsx",
192+
options: [{ rule: "PascalCase" }],
193+
},
194+
{
195+
code,
196+
filename: "[slug].tsx",
197+
options: [{ rule: "PascalCase" }],
198+
},
154199
],
155200
});

0 commit comments

Comments
 (0)