eslint-plugin-nextfriday 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +120 -74
- package/docs/rules/BOOLEAN_NAMING_PREFIX.md +102 -0
- package/docs/rules/NO_DIRECT_DATE.md +36 -0
- package/docs/rules/NO_SINGLE_CHAR_VARIABLES.md +108 -0
- package/docs/rules/PREFER_FUNCTION_DECLARATION.md +139 -0
- package/docs/rules/REQUIRE_EXPLICIT_RETURN_TYPE.md +130 -0
- package/lib/index.cjs +673 -217
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +70 -14
- package/lib/index.d.ts +70 -14
- package/lib/index.js +674 -218
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/docs/rules/NO_EXPLICIT_RETURN_TYPE.md +0 -71
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# require-explicit-return-type
|
|
2
|
+
|
|
3
|
+
Require explicit return types on functions for better code documentation and type safety.
|
|
4
|
+
|
|
5
|
+
## Rule Details
|
|
6
|
+
|
|
7
|
+
This rule enforces that all functions have explicit return type annotations. Explicit return types serve as documentation, help catch bugs early, and make the codebase more maintainable.
|
|
8
|
+
|
|
9
|
+
**Incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
function getName() {
|
|
13
|
+
return "John Doe";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const getAge = () => {
|
|
17
|
+
return 25;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function processUser() {
|
|
21
|
+
console.log("Processing user");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const calculateTotal = (items: Item[]) => {
|
|
25
|
+
return items.reduce((sum, item) => sum + item.price, 0);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
async function fetchData() {
|
|
29
|
+
return await api.get("/data");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function validateEmail(email: string) {
|
|
33
|
+
return email.includes("@");
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Correct** code for this rule:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
function getName(): string {
|
|
41
|
+
return "John Doe";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const getAge = (): number => {
|
|
45
|
+
return 25;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function processUser(): void {
|
|
49
|
+
console.log("Processing user");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const calculateTotal = (items: Item[]): number => {
|
|
53
|
+
return items.reduce((sum, item) => sum + item.price, 0);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
async function fetchData(): Promise<Data> {
|
|
57
|
+
return await api.get("/data");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function validateEmail(email: string): boolean {
|
|
61
|
+
return email.includes("@");
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Exceptions
|
|
66
|
+
|
|
67
|
+
This rule does NOT require return types for:
|
|
68
|
+
|
|
69
|
+
### Callback Functions
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// All of these are allowed without return types
|
|
73
|
+
items.map((item) => item.name);
|
|
74
|
+
items.filter((item) => item.active);
|
|
75
|
+
items.forEach((item) => console.log(item));
|
|
76
|
+
setTimeout(() => console.log("done"), 1000);
|
|
77
|
+
promise.then((result) => result.data);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Object Properties
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const handler = {
|
|
84
|
+
onClick: () => console.log("clicked"),
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Array Elements
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
const callbacks = [() => 1, () => 2];
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### React Components (PascalCase)
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// React components don't need explicit return types
|
|
98
|
+
const MyComponent = () => <div>Hello</div>;
|
|
99
|
+
|
|
100
|
+
function UserProfile() {
|
|
101
|
+
return <div>User</div>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const Button = () => {
|
|
105
|
+
return <button>Click</button>;
|
|
106
|
+
};
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Benefits
|
|
110
|
+
|
|
111
|
+
- **Self-documenting code**: Return types serve as documentation for function contracts
|
|
112
|
+
- **Early error detection**: Type mismatches are caught at compile time
|
|
113
|
+
- **Better IDE support**: Explicit types improve autocomplete and refactoring
|
|
114
|
+
- **API clarity**: Public functions have clear type signatures
|
|
115
|
+
- **Maintainability**: Easier to understand function behavior at a glance
|
|
116
|
+
|
|
117
|
+
## When Not To Use
|
|
118
|
+
|
|
119
|
+
- When you prefer TypeScript's type inference for all functions
|
|
120
|
+
- In rapid prototyping where explicit types slow down development
|
|
121
|
+
- When the inferred type is exactly what you want
|
|
122
|
+
|
|
123
|
+
## Configuration
|
|
124
|
+
|
|
125
|
+
This rule has no configuration options.
|
|
126
|
+
|
|
127
|
+
## Related Rules
|
|
128
|
+
|
|
129
|
+
- [@typescript-eslint/explicit-function-return-type](https://typescript-eslint.io/rules/explicit-function-return-type/) - Similar rule from typescript-eslint
|
|
130
|
+
- [@typescript-eslint/explicit-module-boundary-types](https://typescript-eslint.io/rules/explicit-module-boundary-types/) - Requires explicit types on exported functions
|