@dloizides/frontend-devtools 1.0.1 → 1.0.2
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/dist/eslint/index.d.mts +77 -0
- package/dist/eslint/index.d.ts +77 -0
- package/dist/eslint/index.js +2000 -0
- package/dist/eslint/index.js.map +1 -0
- package/dist/eslint/index.mjs +1990 -0
- package/dist/eslint/index.mjs.map +1 -0
- package/dist/index.d.mts +71 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +2029 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2017 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +6 -1
- package/.eslintrc.js +0 -34
- package/jest.config.js +0 -24
- package/publish.ps1 +0 -107
- package/src/eslint/index.ts +0 -16
- package/src/eslint/legacy.ts +0 -44
- package/src/eslint/plugin.test.ts +0 -60
- package/src/eslint/plugin.ts +0 -87
- package/src/eslint/rules.behaviour.test.ts +0 -86
- package/src/eslint/rules.d.ts +0 -16
- package/src/eslint/types.ts +0 -27
- package/src/i18n/initI18n.test.ts +0 -101
- package/src/i18n/initI18n.ts +0 -109
- package/src/index.ts +0 -31
- package/src/rules/enforce-function-style.js +0 -278
- package/src/rules/enforce-module-structure.js +0 -323
- package/src/rules/enforce-route-preload.js +0 -288
- package/src/rules/enforce-test-colocation.js +0 -82
- package/src/rules/enum-file-isolation.js +0 -149
- package/src/rules/i18n-interpolation.js +0 -185
- package/src/rules/i18n-param-names.js +0 -119
- package/src/rules/no-barrel-companion-file.js +0 -142
- package/src/rules/no-duplicate-nav-prefix.js +0 -107
- package/src/rules/no-duplicate-shared-patterns.js +0 -166
- package/src/rules/no-generated-models-barrel-value-import.js +0 -70
- package/src/rules/no-null-check.js +0 -171
- package/src/rules/no-optional-undefined.js +0 -99
- package/src/rules/no-product-imports-in-shared.js +0 -164
- package/src/rules/prefer-const-enum.js +0 -127
- package/src/rules/require-stable-hook-args.js +0 -372
- package/src/rules/smart-max-lines.js +0 -344
- package/tsconfig.json +0 -25
- package/tsconfig.test.json +0 -11
- package/tsup.config.ts +0 -14
|
@@ -1,372 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom ESLint Plugin: Require Stable Hook Arguments
|
|
3
|
-
*
|
|
4
|
-
* Detects functions, objects, and arrays declared in React component scope
|
|
5
|
-
* that are passed as arguments to custom hooks (use*) without being wrapped
|
|
6
|
-
* in useCallback or useMemo. These unstable references cause infinite
|
|
7
|
-
* re-render loops when the hook uses them in useEffect dependency arrays.
|
|
8
|
-
*
|
|
9
|
-
* Examples:
|
|
10
|
-
* BAD: const t = (key) => i18nT(key);
|
|
11
|
-
* useQuizForm(data, t); // t is new ref every render
|
|
12
|
-
*
|
|
13
|
-
* GOOD: const t = useCallback((key) => i18nT(key), [i18nT]);
|
|
14
|
-
* useQuizForm(data, t); // t is stable
|
|
15
|
-
*
|
|
16
|
-
* BAD: const config = { theme: 'dark' };
|
|
17
|
-
* useMyHook(config); // config is new ref every render
|
|
18
|
-
*
|
|
19
|
-
* GOOD: const config = useMemo(() => ({ theme: 'dark' }), []);
|
|
20
|
-
* useMyHook(config); // config is stable
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Names of hooks/functions whose return values are considered stable
|
|
25
|
-
* (don't need further wrapping in useCallback/useMemo).
|
|
26
|
-
*/
|
|
27
|
-
const STABLE_INIT_CALLS = new Set([
|
|
28
|
-
'useCallback',
|
|
29
|
-
'useMemo',
|
|
30
|
-
'useRef',
|
|
31
|
-
'useState',
|
|
32
|
-
'useReducer',
|
|
33
|
-
'useContext',
|
|
34
|
-
'useId',
|
|
35
|
-
'useDeferredValue',
|
|
36
|
-
'useTransition',
|
|
37
|
-
'useSyncExternalStore',
|
|
38
|
-
'useActionState',
|
|
39
|
-
'useFormStatus',
|
|
40
|
-
'useOptimistic',
|
|
41
|
-
'StyleSheet.create',
|
|
42
|
-
'Object.freeze',
|
|
43
|
-
]);
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Hooks that should NOT trigger this rule. Includes:
|
|
47
|
-
* - React built-in hooks
|
|
48
|
-
* - Well-known library hooks that safely handle unstable references
|
|
49
|
-
* (they don't put callback arguments into useEffect dependency arrays)
|
|
50
|
-
*/
|
|
51
|
-
const IGNORED_HOOKS = new Set([
|
|
52
|
-
// React built-in
|
|
53
|
-
'useState',
|
|
54
|
-
'useEffect',
|
|
55
|
-
'useLayoutEffect',
|
|
56
|
-
'useCallback',
|
|
57
|
-
'useMemo',
|
|
58
|
-
'useRef',
|
|
59
|
-
'useReducer',
|
|
60
|
-
'useContext',
|
|
61
|
-
'useImperativeHandle',
|
|
62
|
-
'useDebugValue',
|
|
63
|
-
'useId',
|
|
64
|
-
'useDeferredValue',
|
|
65
|
-
'useTransition',
|
|
66
|
-
'useSyncExternalStore',
|
|
67
|
-
'useInsertionEffect',
|
|
68
|
-
'useActionState',
|
|
69
|
-
'useFormStatus',
|
|
70
|
-
'useOptimistic',
|
|
71
|
-
'use',
|
|
72
|
-
// Redux - selector runs on every render by design
|
|
73
|
-
'useSelector',
|
|
74
|
-
'useDispatch',
|
|
75
|
-
'useStore',
|
|
76
|
-
// React Query / TanStack Query - callbacks are not deps
|
|
77
|
-
'useQuery',
|
|
78
|
-
'useMutation',
|
|
79
|
-
'useInfiniteQuery',
|
|
80
|
-
'useQueryClient',
|
|
81
|
-
'useSuspenseQuery',
|
|
82
|
-
'useSuspenseInfiniteQuery',
|
|
83
|
-
'useQueries',
|
|
84
|
-
'usePrefetchQuery',
|
|
85
|
-
'useIsFetching',
|
|
86
|
-
'useIsMutating',
|
|
87
|
-
// React Navigation / Expo Router
|
|
88
|
-
'useNavigation',
|
|
89
|
-
'useRoute',
|
|
90
|
-
'useRouter',
|
|
91
|
-
'usePathname',
|
|
92
|
-
'useSegments',
|
|
93
|
-
'useSearchParams',
|
|
94
|
-
'useLocalSearchParams',
|
|
95
|
-
'useGlobalSearchParams',
|
|
96
|
-
'useFocusEffect',
|
|
97
|
-
'useNavigationState',
|
|
98
|
-
// i18next
|
|
99
|
-
'useTranslation',
|
|
100
|
-
// Zustand - selector runs on every render
|
|
101
|
-
'useStoreWithEqualityFn',
|
|
102
|
-
// React Spring / Animated
|
|
103
|
-
'useSpring',
|
|
104
|
-
'useSprings',
|
|
105
|
-
'useTrail',
|
|
106
|
-
'useChain',
|
|
107
|
-
// Syncfusion
|
|
108
|
-
'useDynamicFormStyles',
|
|
109
|
-
// React Hook Form - options object is safely handled
|
|
110
|
-
'useForm',
|
|
111
|
-
'useFormContext',
|
|
112
|
-
'useController',
|
|
113
|
-
'useWatch',
|
|
114
|
-
'useFieldArray',
|
|
115
|
-
]);
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Check if a node is an unstable initializer (function, object, or array literal
|
|
119
|
-
* NOT wrapped in useCallback/useMemo/other stable call).
|
|
120
|
-
*/
|
|
121
|
-
function isUnstableInit(initNode) {
|
|
122
|
-
if (!initNode) return null;
|
|
123
|
-
|
|
124
|
-
// Arrow functions and function expressions are unstable
|
|
125
|
-
if (
|
|
126
|
-
initNode.type === 'ArrowFunctionExpression' ||
|
|
127
|
-
initNode.type === 'FunctionExpression'
|
|
128
|
-
)
|
|
129
|
-
return 'function';
|
|
130
|
-
|
|
131
|
-
// Object literals are unstable
|
|
132
|
-
if (initNode.type === 'ObjectExpression') return 'object';
|
|
133
|
-
|
|
134
|
-
// Array literals are unstable
|
|
135
|
-
if (initNode.type === 'ArrayExpression') return 'array';
|
|
136
|
-
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Check if a call expression is a stable initializer (useCallback, useMemo, etc.)
|
|
142
|
-
*/
|
|
143
|
-
function isStableCall(initNode) {
|
|
144
|
-
if (initNode.type !== 'CallExpression') return false;
|
|
145
|
-
|
|
146
|
-
const callee = initNode.callee;
|
|
147
|
-
|
|
148
|
-
// Direct call: useCallback(...), useMemo(...)
|
|
149
|
-
if (callee.type === 'Identifier')
|
|
150
|
-
return STABLE_INIT_CALLS.has(callee.name);
|
|
151
|
-
|
|
152
|
-
// Member expression: StyleSheet.create(...), Object.freeze(...)
|
|
153
|
-
if (
|
|
154
|
-
callee.type === 'MemberExpression' &&
|
|
155
|
-
callee.object.type === 'Identifier' &&
|
|
156
|
-
callee.property.type === 'Identifier'
|
|
157
|
-
)
|
|
158
|
-
return STABLE_INIT_CALLS.has(`${callee.object.name}.${callee.property.name}`);
|
|
159
|
-
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Get the callee name from a CallExpression node.
|
|
165
|
-
*/
|
|
166
|
-
function getCalleeName(callNode) {
|
|
167
|
-
const callee = callNode.callee;
|
|
168
|
-
if (callee.type === 'Identifier') return callee.name;
|
|
169
|
-
if (
|
|
170
|
-
callee.type === 'MemberExpression' &&
|
|
171
|
-
callee.object.type === 'Identifier' &&
|
|
172
|
-
callee.property.type === 'Identifier'
|
|
173
|
-
)
|
|
174
|
-
return `${callee.object.name}.${callee.property.name}`;
|
|
175
|
-
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Check if a function name looks like a custom hook (starts with "use" + uppercase).
|
|
181
|
-
*/
|
|
182
|
-
function isCustomHookName(name, extraIgnored, patterns) {
|
|
183
|
-
if (!name || name.length <= 3 || !name.startsWith('use')) return false;
|
|
184
|
-
const charAfterUse = name[3];
|
|
185
|
-
const isUpperCase = charAfterUse === charAfterUse.toUpperCase() && charAfterUse !== charAfterUse.toLowerCase();
|
|
186
|
-
if (!isUpperCase) return false;
|
|
187
|
-
if (IGNORED_HOOKS.has(name)) return false;
|
|
188
|
-
if (extraIgnored && extraIgnored.has(name)) return false;
|
|
189
|
-
if (patterns && patterns.some((re) => re.test(name))) return false;
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Check if a node is inside a React component or custom hook (function scope
|
|
195
|
-
* where this rule applies).
|
|
196
|
-
*/
|
|
197
|
-
function isInsideComponentOrHook(node) {
|
|
198
|
-
let current = node.parent;
|
|
199
|
-
while (current) {
|
|
200
|
-
const isFunction =
|
|
201
|
-
current.type === 'FunctionDeclaration' ||
|
|
202
|
-
current.type === 'FunctionExpression' ||
|
|
203
|
-
current.type === 'ArrowFunctionExpression';
|
|
204
|
-
|
|
205
|
-
if (isFunction) {
|
|
206
|
-
// Check if it's a component (PascalCase) or hook (use* prefix)
|
|
207
|
-
const name = getFunctionName(current);
|
|
208
|
-
if (!name) return false;
|
|
209
|
-
const isPascalCase = name[0] === name[0].toUpperCase() && name[0] !== name[0].toLowerCase();
|
|
210
|
-
const isHook = name.length > 3 && name.startsWith('use');
|
|
211
|
-
return isPascalCase || isHook;
|
|
212
|
-
}
|
|
213
|
-
current = current.parent;
|
|
214
|
-
}
|
|
215
|
-
return false;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Get the name of a function node.
|
|
220
|
-
*/
|
|
221
|
-
function getFunctionName(funcNode) {
|
|
222
|
-
// function Foo() {}
|
|
223
|
-
if (funcNode.id) return funcNode.id.name;
|
|
224
|
-
|
|
225
|
-
// const Foo = () => {} or const Foo = function() {}
|
|
226
|
-
if (
|
|
227
|
-
funcNode.parent &&
|
|
228
|
-
funcNode.parent.type === 'VariableDeclarator' &&
|
|
229
|
-
funcNode.parent.id &&
|
|
230
|
-
funcNode.parent.id.type === 'Identifier'
|
|
231
|
-
)
|
|
232
|
-
return funcNode.parent.id.name;
|
|
233
|
-
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const requireStableHookArgsRule = {
|
|
238
|
-
meta: {
|
|
239
|
-
type: 'problem',
|
|
240
|
-
docs: {
|
|
241
|
-
description:
|
|
242
|
-
'Disallow passing unstable functions/objects/arrays to custom hooks. Wrap in useCallback/useMemo to prevent infinite re-renders.',
|
|
243
|
-
category: 'Possible Errors',
|
|
244
|
-
recommended: true,
|
|
245
|
-
},
|
|
246
|
-
schema: [
|
|
247
|
-
{
|
|
248
|
-
type: 'object',
|
|
249
|
-
properties: {
|
|
250
|
-
ignoreHooks: {
|
|
251
|
-
type: 'array',
|
|
252
|
-
items: { type: 'string' },
|
|
253
|
-
description: 'Additional hook names to ignore (e.g., ["useMyLibraryHook"])',
|
|
254
|
-
},
|
|
255
|
-
ignoreHookPatterns: {
|
|
256
|
-
type: 'array',
|
|
257
|
-
items: { type: 'string' },
|
|
258
|
-
description: 'Regex patterns for hook names to ignore (e.g., ["^useIdentityService", "^useQuestionerWeb"])',
|
|
259
|
-
},
|
|
260
|
-
},
|
|
261
|
-
additionalProperties: false,
|
|
262
|
-
},
|
|
263
|
-
],
|
|
264
|
-
messages: {
|
|
265
|
-
unstableFunction:
|
|
266
|
-
'Unstable function "{{name}}" passed to hook "{{hook}}". Wrap in useCallback() to prevent infinite re-renders.',
|
|
267
|
-
unstableObject:
|
|
268
|
-
'Unstable object "{{name}}" passed to hook "{{hook}}". Wrap in useMemo() to prevent infinite re-renders.',
|
|
269
|
-
unstableArray:
|
|
270
|
-
'Unstable array "{{name}}" passed to hook "{{hook}}". Wrap in useMemo() to prevent infinite re-renders.',
|
|
271
|
-
unstableInlineFunction:
|
|
272
|
-
'Inline function passed to hook "{{hook}}". Extract to a useCallback() variable to prevent infinite re-renders.',
|
|
273
|
-
unstableInlineObject:
|
|
274
|
-
'Inline object passed to hook "{{hook}}". Extract to a useMemo() variable to prevent infinite re-renders.',
|
|
275
|
-
unstableInlineArray:
|
|
276
|
-
'Inline array passed to hook "{{hook}}". Extract to a useMemo() variable to prevent infinite re-renders.',
|
|
277
|
-
},
|
|
278
|
-
},
|
|
279
|
-
|
|
280
|
-
create(context) {
|
|
281
|
-
const options = context.options[0] || {};
|
|
282
|
-
const extraIgnored = options.ignoreHooks ? new Set(options.ignoreHooks) : null;
|
|
283
|
-
const ignorePatterns = (options.ignoreHookPatterns || []).map((p) => new RegExp(p));
|
|
284
|
-
// Track unstable variable declarations: name -> { type, node }
|
|
285
|
-
const unstableVars = new Map();
|
|
286
|
-
|
|
287
|
-
return {
|
|
288
|
-
/**
|
|
289
|
-
* Track variable declarations with unstable initializers.
|
|
290
|
-
* e.g., const t = (key) => i18nT(key);
|
|
291
|
-
*/
|
|
292
|
-
VariableDeclarator(node) {
|
|
293
|
-
if (
|
|
294
|
-
!node.id ||
|
|
295
|
-
node.id.type !== 'Identifier' ||
|
|
296
|
-
!node.init
|
|
297
|
-
)
|
|
298
|
-
return;
|
|
299
|
-
|
|
300
|
-
// Skip if the init is a stable call (useCallback, useMemo, etc.)
|
|
301
|
-
if (isStableCall(node.init)) return;
|
|
302
|
-
|
|
303
|
-
const unstableType = isUnstableInit(node.init);
|
|
304
|
-
if (!unstableType) return;
|
|
305
|
-
|
|
306
|
-
// Only track if inside a component or hook
|
|
307
|
-
if (!isInsideComponentOrHook(node)) return;
|
|
308
|
-
|
|
309
|
-
unstableVars.set(node.id.name, {
|
|
310
|
-
type: unstableType,
|
|
311
|
-
node: node,
|
|
312
|
-
});
|
|
313
|
-
},
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Check call expressions that look like custom hooks.
|
|
317
|
-
* Flag any arguments that reference unstable variables or are inline literals.
|
|
318
|
-
*/
|
|
319
|
-
CallExpression(node) {
|
|
320
|
-
const hookName = getCalleeName(node);
|
|
321
|
-
if (!isCustomHookName(hookName, extraIgnored, ignorePatterns)) return;
|
|
322
|
-
if (!isInsideComponentOrHook(node)) return;
|
|
323
|
-
|
|
324
|
-
for (const arg of node.arguments) {
|
|
325
|
-
// Check identifier arguments against tracked unstable vars
|
|
326
|
-
if (arg.type === 'Identifier') {
|
|
327
|
-
const tracked = unstableVars.get(arg.name);
|
|
328
|
-
if (!tracked) continue;
|
|
329
|
-
|
|
330
|
-
const messageMap = {
|
|
331
|
-
function: 'unstableFunction',
|
|
332
|
-
object: 'unstableObject',
|
|
333
|
-
array: 'unstableArray',
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
context.report({
|
|
337
|
-
node: arg,
|
|
338
|
-
messageId: messageMap[tracked.type],
|
|
339
|
-
data: {
|
|
340
|
-
name: arg.name,
|
|
341
|
-
hook: hookName,
|
|
342
|
-
},
|
|
343
|
-
});
|
|
344
|
-
continue;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// Check inline unstable arguments (arrow functions, objects, arrays)
|
|
348
|
-
const inlineType = isUnstableInit(arg);
|
|
349
|
-
if (inlineType) {
|
|
350
|
-
const inlineMessageMap = {
|
|
351
|
-
function: 'unstableInlineFunction',
|
|
352
|
-
object: 'unstableInlineObject',
|
|
353
|
-
array: 'unstableInlineArray',
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
context.report({
|
|
357
|
-
node: arg,
|
|
358
|
-
messageId: inlineMessageMap[inlineType],
|
|
359
|
-
data: { hook: hookName },
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
},
|
|
364
|
-
};
|
|
365
|
-
},
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
export default {
|
|
369
|
-
rules: {
|
|
370
|
-
'require-stable-hook-args': requireStableHookArgsRule,
|
|
371
|
-
},
|
|
372
|
-
};
|
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom ESLint Plugin: Smart Max Lines Per Function
|
|
3
|
-
*
|
|
4
|
-
* Applies different line limits based on function type:
|
|
5
|
-
* - React components (functions returning JSX): higher limit (default 200)
|
|
6
|
-
* - Regular functions: stricter limit (default 50, warning at 30)
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const COMPONENT_MAX_DEFAULT = 200;
|
|
10
|
-
const FUNCTION_MAX_DEFAULT = 50;
|
|
11
|
-
const FUNCTION_WARN_DEFAULT = 30;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Check if an AST node is a JSX element or fragment
|
|
15
|
-
*/
|
|
16
|
-
function isJSX(node) {
|
|
17
|
-
if (!node) {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
return (
|
|
21
|
-
node.type === 'JSXElement' ||
|
|
22
|
-
node.type === 'JSXFragment'
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Check if an expression could resolve to JSX
|
|
28
|
-
*/
|
|
29
|
-
function expressionReturnsJSX(node) {
|
|
30
|
-
if (!node) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Direct JSX
|
|
35
|
-
if (isJSX(node)) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Conditional expression: condition ? <A/> : <B/>
|
|
40
|
-
if (node.type === 'ConditionalExpression') {
|
|
41
|
-
return expressionReturnsJSX(node.consequent) || expressionReturnsJSX(node.alternate);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Logical expressions: condition && <A/> or condition || <A/>
|
|
45
|
-
if (node.type === 'LogicalExpression') {
|
|
46
|
-
return expressionReturnsJSX(node.left) || expressionReturnsJSX(node.right);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Parenthesized expression
|
|
50
|
-
if (node.type === 'ParenthesizedExpression') {
|
|
51
|
-
return expressionReturnsJSX(node.expression);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Sequence expression (rare but possible)
|
|
55
|
-
if (node.type === 'SequenceExpression') {
|
|
56
|
-
const lastExpr = node.expressions[node.expressions.length - 1];
|
|
57
|
-
return expressionReturnsJSX(lastExpr);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Recursively check if a block contains a return statement with JSX
|
|
65
|
-
*/
|
|
66
|
-
function blockReturnsJSX(blockBody) {
|
|
67
|
-
if (!blockBody || !Array.isArray(blockBody)) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
for (const statement of blockBody) {
|
|
72
|
-
if (statementReturnsJSX(statement)) {
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Check if a statement returns JSX
|
|
82
|
-
*/
|
|
83
|
-
function statementReturnsJSX(statement) {
|
|
84
|
-
if (!statement) {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
switch (statement.type) {
|
|
89
|
-
case 'ReturnStatement':
|
|
90
|
-
return expressionReturnsJSX(statement.argument);
|
|
91
|
-
|
|
92
|
-
case 'IfStatement':
|
|
93
|
-
// Check consequent
|
|
94
|
-
if (statement.consequent) {
|
|
95
|
-
if (statement.consequent.type === 'BlockStatement') {
|
|
96
|
-
if (blockReturnsJSX(statement.consequent.body)) {
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
} else if (statementReturnsJSX(statement.consequent)) {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// Check alternate (else branch)
|
|
104
|
-
if (statement.alternate) {
|
|
105
|
-
if (statement.alternate.type === 'BlockStatement') {
|
|
106
|
-
if (blockReturnsJSX(statement.alternate.body)) {
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
} else if (statementReturnsJSX(statement.alternate)) {
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return false;
|
|
114
|
-
|
|
115
|
-
case 'SwitchStatement':
|
|
116
|
-
for (const switchCase of statement.cases) {
|
|
117
|
-
if (blockReturnsJSX(switchCase.consequent)) {
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return false;
|
|
122
|
-
|
|
123
|
-
case 'TryStatement':
|
|
124
|
-
if (statement.block && blockReturnsJSX(statement.block.body)) {
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
if (statement.handler && statement.handler.body && blockReturnsJSX(statement.handler.body.body)) {
|
|
128
|
-
return true;
|
|
129
|
-
}
|
|
130
|
-
if (statement.finalizer && blockReturnsJSX(statement.finalizer.body)) {
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
return false;
|
|
134
|
-
|
|
135
|
-
case 'BlockStatement':
|
|
136
|
-
return blockReturnsJSX(statement.body);
|
|
137
|
-
|
|
138
|
-
default:
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Check if a function node is a React component (returns JSX)
|
|
145
|
-
*/
|
|
146
|
-
function isReactComponent(node) {
|
|
147
|
-
// Arrow function with implicit JSX return: () => <div/>
|
|
148
|
-
if (node.type === 'ArrowFunctionExpression') {
|
|
149
|
-
if (node.body.type !== 'BlockStatement') {
|
|
150
|
-
return expressionReturnsJSX(node.body);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Function with block body - check for JSX returns
|
|
155
|
-
const body = node.body;
|
|
156
|
-
if (body && body.type === 'BlockStatement') {
|
|
157
|
-
return blockReturnsJSX(body.body);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Count effective lines in a function (optionally skipping blanks/comments)
|
|
165
|
-
*/
|
|
166
|
-
function countLines(node, sourceCode, skipBlankLines, skipComments) {
|
|
167
|
-
const startLine = node.loc.start.line;
|
|
168
|
-
const endLine = node.loc.end.line;
|
|
169
|
-
|
|
170
|
-
if (!skipBlankLines && !skipComments) {
|
|
171
|
-
return endLine - startLine + 1;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const lines = sourceCode.lines.slice(startLine - 1, endLine);
|
|
175
|
-
let count = 0;
|
|
176
|
-
|
|
177
|
-
// Get all comments in the function
|
|
178
|
-
const allComments = sourceCode.getAllComments();
|
|
179
|
-
const functionComments = allComments.filter(
|
|
180
|
-
(comment) => comment.loc.start.line >= startLine && comment.loc.end.line <= endLine
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
// Build a set of line numbers that are purely comment lines
|
|
184
|
-
const commentOnlyLines = new Set();
|
|
185
|
-
if (skipComments) {
|
|
186
|
-
for (const comment of functionComments) {
|
|
187
|
-
for (let line = comment.loc.start.line; line <= comment.loc.end.line; line++) {
|
|
188
|
-
const lineContent = sourceCode.lines[line - 1];
|
|
189
|
-
if (lineContent) {
|
|
190
|
-
const trimmed = lineContent.trim();
|
|
191
|
-
// Check if line is only a comment (starts with // or /* or * for multiline)
|
|
192
|
-
const isOnlyComment =
|
|
193
|
-
trimmed.startsWith('//') ||
|
|
194
|
-
trimmed.startsWith('/*') ||
|
|
195
|
-
trimmed.startsWith('*') ||
|
|
196
|
-
trimmed.endsWith('*/') ||
|
|
197
|
-
trimmed === '';
|
|
198
|
-
if (isOnlyComment) {
|
|
199
|
-
commentOnlyLines.add(line);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
for (let i = 0; i < lines.length; i++) {
|
|
207
|
-
const lineNumber = startLine + i;
|
|
208
|
-
const lineContent = lines[i];
|
|
209
|
-
|
|
210
|
-
// Skip blank lines
|
|
211
|
-
if (skipBlankLines && lineContent.trim() === '') {
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Skip comment-only lines
|
|
216
|
-
if (skipComments && commentOnlyLines.has(lineNumber)) {
|
|
217
|
-
continue;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
count++;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
return count;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* The ESLint rule definition
|
|
228
|
-
*/
|
|
229
|
-
const smartMaxLinesRule = {
|
|
230
|
-
meta: {
|
|
231
|
-
type: 'suggestion',
|
|
232
|
-
docs: {
|
|
233
|
-
description:
|
|
234
|
-
'Enforce different line limits for React components vs regular functions',
|
|
235
|
-
category: 'Best Practices',
|
|
236
|
-
recommended: true,
|
|
237
|
-
},
|
|
238
|
-
schema: [
|
|
239
|
-
{
|
|
240
|
-
type: 'object',
|
|
241
|
-
properties: {
|
|
242
|
-
componentMax: {
|
|
243
|
-
type: 'integer',
|
|
244
|
-
minimum: 1,
|
|
245
|
-
description: 'Maximum lines for React components',
|
|
246
|
-
},
|
|
247
|
-
functionMax: {
|
|
248
|
-
type: 'integer',
|
|
249
|
-
minimum: 1,
|
|
250
|
-
description: 'Maximum lines for regular functions (error threshold)',
|
|
251
|
-
},
|
|
252
|
-
functionWarn: {
|
|
253
|
-
type: 'integer',
|
|
254
|
-
minimum: 1,
|
|
255
|
-
description: 'Warning threshold for regular functions',
|
|
256
|
-
},
|
|
257
|
-
skipBlankLines: {
|
|
258
|
-
type: 'boolean',
|
|
259
|
-
description: 'Skip blank lines when counting',
|
|
260
|
-
},
|
|
261
|
-
skipComments: {
|
|
262
|
-
type: 'boolean',
|
|
263
|
-
description: 'Skip comment lines when counting',
|
|
264
|
-
},
|
|
265
|
-
},
|
|
266
|
-
additionalProperties: false,
|
|
267
|
-
},
|
|
268
|
-
],
|
|
269
|
-
messages: {
|
|
270
|
-
componentTooLong:
|
|
271
|
-
'React component has too many lines ({{count}}). Maximum allowed is {{max}}.',
|
|
272
|
-
functionTooLong:
|
|
273
|
-
'Function has too many lines ({{count}}). Maximum allowed is {{max}}.',
|
|
274
|
-
functionWarning:
|
|
275
|
-
'Function has {{count}} lines which exceeds recommended limit of {{warn}}. Consider refactoring.',
|
|
276
|
-
},
|
|
277
|
-
},
|
|
278
|
-
|
|
279
|
-
create(context) {
|
|
280
|
-
const options = context.options[0] || {};
|
|
281
|
-
const componentMax = options.componentMax ?? COMPONENT_MAX_DEFAULT;
|
|
282
|
-
const functionMax = options.functionMax ?? FUNCTION_MAX_DEFAULT;
|
|
283
|
-
const functionWarn = options.functionWarn ?? FUNCTION_WARN_DEFAULT;
|
|
284
|
-
const skipBlankLines = options.skipBlankLines ?? true;
|
|
285
|
-
const skipComments = options.skipComments ?? true;
|
|
286
|
-
|
|
287
|
-
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
288
|
-
|
|
289
|
-
function checkFunction(node) {
|
|
290
|
-
const lineCount = countLines(node, sourceCode, skipBlankLines, skipComments);
|
|
291
|
-
const isComponent = isReactComponent(node);
|
|
292
|
-
|
|
293
|
-
if (isComponent) {
|
|
294
|
-
// React component - apply componentMax limit
|
|
295
|
-
if (lineCount > componentMax) {
|
|
296
|
-
context.report({
|
|
297
|
-
node,
|
|
298
|
-
messageId: 'componentTooLong',
|
|
299
|
-
data: {
|
|
300
|
-
count: String(lineCount),
|
|
301
|
-
max: String(componentMax),
|
|
302
|
-
},
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
} else {
|
|
306
|
-
// Regular function - apply functionMax (error) and functionWarn (warning)
|
|
307
|
-
if (lineCount > functionMax) {
|
|
308
|
-
context.report({
|
|
309
|
-
node,
|
|
310
|
-
messageId: 'functionTooLong',
|
|
311
|
-
data: {
|
|
312
|
-
count: String(lineCount),
|
|
313
|
-
max: String(functionMax),
|
|
314
|
-
},
|
|
315
|
-
});
|
|
316
|
-
} else if (lineCount > functionWarn) {
|
|
317
|
-
context.report({
|
|
318
|
-
node,
|
|
319
|
-
messageId: 'functionWarning',
|
|
320
|
-
data: {
|
|
321
|
-
count: String(lineCount),
|
|
322
|
-
warn: String(functionWarn),
|
|
323
|
-
},
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return {
|
|
330
|
-
FunctionDeclaration: checkFunction,
|
|
331
|
-
FunctionExpression: checkFunction,
|
|
332
|
-
ArrowFunctionExpression: checkFunction,
|
|
333
|
-
};
|
|
334
|
-
},
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Export the plugin
|
|
339
|
-
*/
|
|
340
|
-
export default {
|
|
341
|
-
rules: {
|
|
342
|
-
'smart-max-lines': smartMaxLinesRule,
|
|
343
|
-
},
|
|
344
|
-
};
|