eslint-plugin-nextfriday 1.6.0 → 1.8.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 +16 -0
- package/README.md +123 -74
- package/docs/rules/BOOLEAN_NAMING_PREFIX.md +102 -0
- package/docs/rules/NO_DIRECT_DATE.md +36 -0
- package/docs/rules/NO_LAZY_IDENTIFIERS.md +106 -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 +812 -217
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +84 -14
- package/lib/index.d.ts +84 -14
- package/lib/index.js +813 -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,139 @@
|
|
|
1
|
+
# prefer-function-declaration
|
|
2
|
+
|
|
3
|
+
Enforce function declarations over arrow functions assigned to variables in `.ts` files for better readability and hoisting.
|
|
4
|
+
|
|
5
|
+
## Rule Details
|
|
6
|
+
|
|
7
|
+
This rule requires using function declarations instead of arrow functions or function expressions when defining named functions in TypeScript utility files. Arrow functions used as callbacks (in `.map()`, `.filter()`, etc.) are still allowed.
|
|
8
|
+
|
|
9
|
+
**Target:** `.ts` files only (not `.tsx`, `.js`, or `.d.ts`)
|
|
10
|
+
|
|
11
|
+
**Incorrect** code for this rule:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
// utils/date.ts
|
|
15
|
+
const formatThaiDate = (date: Date) => {
|
|
16
|
+
return date.toLocaleDateString("th-TH");
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const formatDate = (date: Date) => date.toISOString();
|
|
20
|
+
|
|
21
|
+
export const add = (a: number, b: number) => a + b;
|
|
22
|
+
|
|
23
|
+
const greet = function (name: string) {
|
|
24
|
+
return `Hello ${name}`;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const fetchUser = async (id: string) => {
|
|
28
|
+
return await api.get(`/users/${id}`);
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Correct** code for this rule:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// utils/date.ts
|
|
36
|
+
function formatThaiDate(date: Date) {
|
|
37
|
+
return date.toLocaleDateString("th-TH");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function formatDate(date: Date) {
|
|
41
|
+
return date.toISOString();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function add(a: number, b: number) {
|
|
45
|
+
return a + b;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function greet(name: string) {
|
|
49
|
+
return `Hello ${name}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function fetchUser(id: string) {
|
|
53
|
+
return await api.get(`/users/${id}`);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## What This Rule Allows
|
|
58
|
+
|
|
59
|
+
Arrow functions are still allowed in the following contexts:
|
|
60
|
+
|
|
61
|
+
### Callbacks
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// All of these are allowed
|
|
65
|
+
const years = dates.map((date) => date.getFullYear());
|
|
66
|
+
const active = items.filter((item) => item.active);
|
|
67
|
+
const sorted = items.sort((a, b) => a.name.localeCompare(b.name));
|
|
68
|
+
items.forEach((item) => console.log(item));
|
|
69
|
+
const total = items.reduce((sum, item) => sum + item.price, 0);
|
|
70
|
+
setTimeout(() => console.log("done"), 1000);
|
|
71
|
+
promise.then((result) => result.data);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Object Properties
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const handler = {
|
|
78
|
+
onClick: () => console.log("clicked"),
|
|
79
|
+
onHover: () => setHovered(true),
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Array Elements
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const callbacks = [() => 1, () => 2, () => 3];
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Return Values
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
function createHandler() {
|
|
93
|
+
return () => console.log("handled");
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Conditional/Logical Expressions
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
const fn = condition ? () => valueA : () => valueB;
|
|
101
|
+
const handler = defaultFn || (() => fallback);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### TSX Files
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// components/Button.tsx - Arrow functions allowed
|
|
108
|
+
const Button = () => <button>Click me</button>;
|
|
109
|
+
const handleClick = () => console.log("clicked");
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Benefits
|
|
113
|
+
|
|
114
|
+
- **Hoisting**: Function declarations are hoisted, allowing you to call functions before they're defined
|
|
115
|
+
- **Better readability**: Function declarations are more explicit about intent
|
|
116
|
+
- **Clearer stack traces**: Named function declarations provide better debugging information
|
|
117
|
+
- **Consistent style**: Enforces uniform function definition patterns in utility files
|
|
118
|
+
- **Self-documenting**: `function formatDate()` is clearer than `const formatDate = () =>`
|
|
119
|
+
|
|
120
|
+
## Why Only `.ts` Files?
|
|
121
|
+
|
|
122
|
+
- **`.tsx` files**: Arrow functions are commonly used for React components and event handlers
|
|
123
|
+
- **`.js` files**: JavaScript projects may have different conventions
|
|
124
|
+
- **`.d.ts` files**: Declaration files don't contain implementations
|
|
125
|
+
|
|
126
|
+
## When Not To Use
|
|
127
|
+
|
|
128
|
+
- When you prefer arrow functions for all function definitions
|
|
129
|
+
- In projects where arrow function style is established
|
|
130
|
+
- When you need lexical `this` binding (arrow functions don't have their own `this`)
|
|
131
|
+
|
|
132
|
+
## Configuration
|
|
133
|
+
|
|
134
|
+
This rule has no configuration options.
|
|
135
|
+
|
|
136
|
+
## Related Rules
|
|
137
|
+
|
|
138
|
+
- [func-style](https://eslint.org/docs/rules/func-style) - Built-in ESLint rule for function style
|
|
139
|
+
- [arrow-body-style](https://eslint.org/docs/rules/arrow-body-style) - Enforce arrow function body style
|
|
@@ -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
|